создать калькулятор с графическим интерфейсом Powershell

2586
Sandro21

Привет, ребята, я пытаюсь создать калькулятор. Пока что у меня есть графический интерфейс. Но мне нужна помощь с фоновыми функциями. Пример: я печатаю на кнопках 5 + 6. Теперь, когда я нажимаю «=», он должен установить ввод TextBox в переменную, а затем вычислить его. У кого-нибудь есть идея, как я могу это сделать. (Я очень хорошо знал Пауэршелла). Спасибо и извините за мой плохой инглиш. Привет Сандро

 [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() 
0
Если вы выполняете эти трюки с PowerShell, просто напишите «настоящую» программу на C #. это будет меньше головной боли, и использование Visual Studio будет означать, что у вас будет много помощи по GUI для многих вещей (IntelliSense и т. д.). Что вам нужно сделать, так это привязать событие к кнопкам и позволить функции, которую вы привязали к событию, изменить текст для ввода. Seth 7 лет назад 1

1 ответ на вопрос

0
JosefZ

The following improvement of my previous answer to your almost the same question could help however such patchwork would conduce you to perdition (as well as me). Good luck!

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()
Привет JosefZ спасибо за ваш ответ. Не могли бы вы объяснить, что именно делает Funtion Operation? Sandro21 7 лет назад 0

Похожие вопросы