所以我想编写一个简单的计算器。我有一个非常简单的问题。我如何将按钮的内容复制到文本框中?zB 将按钮1的内容复制到我的文本框中
这是我的代码:
$Taschenrechner = New-Object System.Windows.Forms.Form
$Taschenrechner.StartPosition = "CenterScreen"
$Taschenrechner.Size = New-Object System.Drawing.Size(700,600)
$Taschenrechner.Text = "Taschenrechner"
#Titel
$Titel = New-Object System.Windows.Forms.Label
$Titel.Location = New-Object System.Drawing.Size(200,10)
$Titel.Size = New-Object System.Drawing.Size(320,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)
#Textbox
$Textfeld= New-Object System.Windows.Forms.TextBox
$Textfeld.Location = New-Object System.Drawing.Size(125,100)
$Textfeld.Size = New-Object System.Drawing.Size(100,100)
$Textfeld.Width = (300)
$Taschenrechner.Controls.Add($Textfeld)
#1
$button1 = New-Object System.Windows.Forms.Button
$button1.Location = New-Object System.Drawing.Size(125,120)
$button1.Size = New-Object System.Drawing.Size(100,50)
$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(225,120)
$button2.Size = New-Object System.Drawing.Size(100,50)
$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(325,120)
$button3.Size = New-Object System.Drawing.Size(100,50)
$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(125,170)
$button4.Size = New-Object System.Drawing.Size(100,50)
$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(225,170)
$button5.Size = New-Object System.Drawing.Size(100,50)
$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(325,170)
$button6.Size = New-Object System.Drawing.Size(100,50)
$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(125,220)
$button7.Size = New-Object System.Drawing.Size(100,50)
$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(225,220)
$button8.Size = New-Object System.Drawing.Size(100,50)
$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(325,220)
$button9.Size = New-Object System.Drawing.Size(100,50)
$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(125,270)
$button0.Size = New-Object System.Drawing.Size(100,50)
$button0.Text = "0"
$button0.Name = "0"
$Taschenrechner.Controls.Add($button0)
[void] $Taschenrechner.ShowDialog()
答案1
我的幼稚做法是:
# unchanged code here
#1
$button1 = New-Object System.Windows.Forms.Button
$button1.Location = New-Object System.Drawing.Size(125,120)
$button1.Size = New-Object System.Drawing.Size(100,50)
$button1.Text = "1"
$button1.Name = "1"
$button1.Add_Click({$Textfeld.Text+='1'})
$Taschenrechner.Controls.Add($button1)
# change #2 .. #9 analogously to #1 and #0
#0
$button0 = New-Object System.Windows.Forms.Button
$button0.Location = New-Object System.Drawing.Size(125,270)
$button0.Size = New-Object System.Drawing.Size(100,50)
$button0.Text = "0"
$button0.Name = "0"
$button0.Add_Click({$Textfeld.Text+='0'})
$Taschenrechner.Controls.Add($button0)
# unchanged code here