大家好,我正在尝试创建一个计算器。到目前为止,我已经完成了 GUI。但我需要一些有关背景功能的帮助。例如:我在按钮 5+6 上输入。现在,当我按下“=”时,它应该将 TextBox 的输入设置为变量,然后计算出来。有人知道我该怎么做吗?(我对 Powershell 非常了解)。谢谢,抱歉我的英语不好。问候 Sandro
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Platfrom
$Taschenrechner = New-Object System.Windows.Forms.Form
$Taschenrechner.StartPosition = "CenterScreen"
$Taschenrechner.Size = New-Object System.Drawing.Size(470,400)
$Taschenrechner.Text = "Taschenrechner"
#Titel
$Titel = New-Object System.Windows.Forms.Label
$Titel.Location = New-Object System.Drawing.Size(70,10)
$Titel.Size = New-Object System.Drawing.Size(360,50)
$Font = New-Object System.Drawing.Font("Arial",30,[System.Drawing.FontStyle]::Regular)
$Titel.Font = $Font
$Titel.Text = "Taschenrechner"
$Titel.Name = "Taschenrechner"
$Taschenrechner.Controls.Add($Titel)
#Textfeld
$Textfeld= New-Object System.Windows.Forms.TextBox
$Textfeld.Location = New-Object System.Drawing.Size(25,100)
$Textfeld.Size = New-Object System.Drawing.Size(100,100)
$Textfeld.Width = (400)
$Taschenrechner.Controls.Add($Textfeld)
#1
$button1 = New-Object System.Windows.Forms.Button
$button1.Location = New-Object System.Drawing.Size(25,120)
$button1.Size = New-Object System.Drawing.Size(100,50)
$button1.Add_Click({$Textfeld.Text+='1'})
$button1.Text = "1"
$button1.Name = "1"
$Taschenrechner.Controls.Add($button1)
#2
$button2 = New-Object System.Windows.Forms.Button
$button2.Location = New-Object System.Drawing.Size(125,120)
$button2.Size = New-Object System.Drawing.Size(100,50)
$button2.Add_Click({$Textfeld.Text+='2'})
$button2.Text = "2"
$button2.Name = "2"
$Taschenrechner.Controls.Add($button2)
#3
$button3 = New-Object System.Windows.Forms.Button
$button3.Location = New-Object System.Drawing.Size(225,120)
$button3.Size = New-Object System.Drawing.Size(100,50)
$button3.Add_Click({$Textfeld.Text+='3'})
$button3.Text = "3"
$button3.Name = "3"
$Taschenrechner.Controls.Add($button3)
#4
$button4 = New-Object System.Windows.Forms.Button
$button4.Location = New-Object System.Drawing.Size(25,170)
$button4.Size = New-Object System.Drawing.Size(100,50)
$button4.Add_Click({$Textfeld.Text+='4'})
$button4.Text = "4"
$button4.Name = "4"
$Taschenrechner.Controls.Add($button4)
#5
$button5 = New-Object System.Windows.Forms.Button
$button5.Location = New-Object System.Drawing.Size(125,170)
$button5.Size = New-Object System.Drawing.Size(100,50)
$button5.Add_Click({$Textfeld.Text+='5'})
$button5.Text = "5"
$button5.Name = "5"
$Taschenrechner.Controls.Add($button5)
#6
$button6 = New-Object System.Windows.Forms.Button
$button6.Location = New-Object System.Drawing.Size(225,170)
$button6.Size = New-Object System.Drawing.Size(100,50)
$button6.Add_Click({$Textfeld.Text+='6'})
$button6.Text = "6"
$button6.Name = "6"
$Taschenrechner.Controls.Add($button6)
#7
$button7 = New-Object System.Windows.Forms.Button
$button7.Location = New-Object System.Drawing.Size(25,220)
$button7.Size = New-Object System.Drawing.Size(100,50)
$button7.Add_Click({$Textfeld.Text+='7'})
$button7.Text = "7"
$button7.Name = "7"
$Taschenrechner.Controls.Add($button7)
#8
$button8 = New-Object System.Windows.Forms.Button
$button8.Location = New-Object System.Drawing.Size(125,220)
$button8.Size = New-Object System.Drawing.Size(100,50)
$button8.Add_Click({$Textfeld.Text+='8'})
$button8.Text = "8"
$button8.Name = "8"
$Taschenrechner.Controls.Add($button8)
#9
$button9 = New-Object System.Windows.Forms.Button
$button9.Location = New-Object System.Drawing.Size(225,220)
$button9.Size = New-Object System.Drawing.Size(100,50)
$button9.Add_Click({$Textfeld.Text+='9'})
$button9.Text = "9"
$button9.Name = "9"
$Taschenrechner.Controls.Add($button9)
#0
$button0 = New-Object System.Windows.Forms.Button
$button0.Location = New-Object System.Drawing.Size(25,270)
$button0.Size = New-Object System.Drawing.Size(100,50)
$button0.Add_Click({$Textfeld.Text+='0'})
$button0.Text = "0"
$button0.Name = "0"
$Taschenrechner.Controls.Add($button0)
#Dezimalstelle
$Punkt = New-Object System.Windows.Forms.Button
$Punkt.Location = New-Object System.Drawing.Size(125,270)
$Punkt.Size = New-Object System.Drawing.Size(100,50)
$Punkt.Add_Click({$Textfeld.Text+='.'})
$Punkt.Text = "."
$Punkt.Name = "."
$Taschenrechner.Controls.Add($Punkt)
#Gleich
$gleich = New-Object System.Windows.Forms.Button
$gleich.Location = New-Object System.Drawing.Size(225,270)
$gleich.Size = New-Object System.Drawing.Size(100,50)
$gleich.Text = "="
$gleich.Name = "="
$Taschenrechner.Controls.Add($gleich)
#Durch
$durch = New-Object System.Windows.Forms.Button
$durch.Location = New-Object System.Drawing.Size(325,120)
$durch.Size = New-Object System.Drawing.Size(100,50)
$durch.Text = "/"
$durch.Name = "/"
$Taschenrechner.Controls.Add($durch)
#Mal
$mal = New-Object System.Windows.Forms.Button
$mal.Location = New-Object System.Drawing.Size(325,170)
$mal.Size = New-Object System.Drawing.Size(100,50)
$mal.Text = "x"
$mal.Name = "x"
$Taschenrechner.Controls.Add($mal)
#Minus
$minus = New-Object System.Windows.Forms.Button
$minus.Location = New-Object System.Drawing.Size(325,220)
$minus.Size = New-Object System.Drawing.Size(100,50)
$minus.Text = "-"
$minus.Name = "-"
$Taschenrechner.Controls.Add($minus)
#Plus
$plus = New-Object System.Windows.Forms.Button
$plus.Location = New-Object System.Drawing.Size(325,270)
$plus.Size = New-Object System.Drawing.Size(100,50)
$plus.Text = "+"
$plus.Name = "+"
$Taschenrechner.Controls.Add($plus)
[void] $Taschenrechner.ShowDialog()
答案1
以下是我之前对您问题的回答的改进几乎同样的问题可以提供帮助,但是这种拼凑可能会让你(和我一样)走向毁灭。祝你好运!
Remove-Variable script:Part* -ErrorAction SilentlyContinue
$script:PartialResult = ''
$script:PartOperation = ''
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Platfrom
### unchanged code here (cca 110 lines)
#Dezimalstelle
$Punkt = New-Object System.Windows.Forms.Button
$Punkt.Location = New-Object System.Drawing.Size(225,270)
$Punkt.Size = New-Object System.Drawing.Size(100,50)
$Punkt.Add_Click({$Textfeld.Text+='.'})
$Punkt.Text = "."
$Punkt.Name = "."
$Taschenrechner.Controls.Add($Punkt)
#Gleich
$gleich = New-Object System.Windows.Forms.Button
$gleich.Location = New-Object System.Drawing.Size(325,270)
$gleich.Size = New-Object System.Drawing.Size(100,50)
$gleich.Text = "="
$gleich.Name = "="
$Taschenrechner.Controls.Add($gleich)
#Durch
$durch = New-Object System.Windows.Forms.Button
$durch.Location = New-Object System.Drawing.Size(425,120)
$durch.Size = New-Object System.Drawing.Size(100,50)
$durch.Text = "/"
$durch.Name = "/"
$Taschenrechner.Controls.Add($durch)
#Mal
$mal = New-Object System.Windows.Forms.Button
$mal.Location = New-Object System.Drawing.Size(425,170)
$mal.Size = New-Object System.Drawing.Size(100,50)
$mal.Text = "×"
$mal.Name = "*"
$Taschenrechner.Controls.Add($mal)
#Minus
$minus = New-Object System.Windows.Forms.Button
$minus.Location = New-Object System.Drawing.Size(425,220)
$minus.Size = New-Object System.Drawing.Size(100,50)
$minus.Text = "-"
$minus.Name = "-"
$Taschenrechner.Controls.Add($minus)
#Plus
$plus = New-Object System.Windows.Forms.Button
$plus.Location = New-Object System.Drawing.Size(425,270)
$plus.Size = New-Object System.Drawing.Size(100,50)
$plus.Text = "+"
$plus.Name = "+"
$Taschenrechner.Controls.Add($plus)
Function Operation {
param([string] $Ope)
### you need to show partial result an operation!!!
if ( $Textfeld.Text -ne '' ) {
if ($script:PartialResult -ne '' ) {
$aux = [string] $script:PartialResult + $script:PartOperation + $script:Textfeld.Text
$script:PartialResult = [string] $( Invoke-Expression $aux )
} else {
$script:PartialResult = $Textfeld.Text
}
if ( $Ope -eq '=' ) {
$Textfeld.Text = '' + [string] $script:PartialResult
$script:PartialResult = ''
$script:PartOperation = ''
} else {
$Textfeld.Text = ''
$script:PartOperation = $Ope
}
}
}
$gleich.Add_Click( { Operation '=' } )
$mal. Add_Click( { Operation '*' } ) # Name = "×"
$plus. Add_Click( { Operation '+' } ) # Name = "+"
$minus. Add_Click( { Operation '-' } ) # Name = "-"
$durch. Add_Click( { Operation '/' } ) # Name = "/"
[void] $Taschenrechner.ShowDialog()