我正在实现用于远程访问控制的GUI。我的文本框带有标签,现在我想将一些KeyEvent添加到文本框中,以便可以验证用户Input。为了进行测试,我想知道如何添加一些KeyEvent,例如在MessageBox中打印“ HelloWorld”。我尝试使用KeyEventHandler,KeyCode和KeyPressEventHandler没有成功。有人可以帮忙吗? 这是我的示例代码:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Function MakenewForm {
$form.Close()
$form.Dispose()
MakeForm
}
Function MakeForm {
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Fernwartungssoftware '
$form.Size = New-Object System.Drawing.Size(1600,800)
$form.StartPosition = 'CenterScreen'
$itext = New-Object System.Windows.Forms.Label
$itext.Location = New-Object System.Drawing.Point(4,15)
$itext.Size = New-Object System.Drawing.Size(500,42)
$itext.Font = New-Object System.Drawing.Font($itext.Font.Name, 28);
$itext.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 28, [System.Drawing.FontStyle]::Bold);
$itext.Text = 'Fernwartung'
$form.Controls.Add($itext)
#sbutn
$sbutn = New-Object System.Windows.Forms.Button
$sbutn.Location = New-Object System.Drawing.Point(09,180)
$sbutn.Size = New-Object System.Drawing.Size(110,30)
$sbutn.Text = 'Send'
$form.AcceptButton = $sbutn
$form.Controls.Add($sbutn)
#Headline
$lbl = New-Object System.Windows.Forms.Label
$lbl.Location = New-Object System.Drawing.Point(08,73)
$lbl.Size = New-Object System.Drawing.Size(390,28)
$lbl.Text = 'Bitte ausfüllen:'
$lbl.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 13, [System.Drawing.FontStyle]::Regular);
$form.Controls.Add($lbl)
#Problem
$pr = New-Object System.Windows.Forms.Label
$pr.Location = New-Object System.Drawing.Point(08,105)
$pr.Size = New-Object System.Drawing.Size(90,20)
$pr.Text = 'Problem:'
#$label.Font = New-Object System.Drawing.Font($pr.Font.Name, 13);
$pr.Font = New-Object System.Drawing.Font($pr.Font.Name, 9);
$form.Controls.Add($pr)
#Textbox
$txtb = New-Object System.Windows.Forms.TextBox
$txtb.Location = New-Object System.Drawing.Point(105,105)
$txtb.Size = New-Object System.Drawing.Size(320,20)
#$form.Add_Shown({$form.Activate(); $txtb.focus()})
$form.Controls.Add($txtb)
#$textBox.Add_KeyDown({
#if ($_.KeyCode -eq "Enter") {
#logic
# $textBox.Text | Out-Host
# }
#})
$form.ShowDialog()
}
MakeForm
答案 0 :(得分:2)
在PrimalForms中快速添加一些内容,以使您的GUI代码看起来不那么糟糕。如果要快速一致地粉碎一些GUI,则强烈建议。
在文本框中寻找的功能是$txtb.add_KeyDown($handler_txtb_KeyDown)
,然后调用$_.KeyCode
来检查其是否为Enter
完整的工作代码供您在这里使用。
#Form Function
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
#region Form Objects
$form = New-Object System.Windows.Forms.Form
$sbutn = New-Object System.Windows.Forms.Button
$pr = New-Object System.Windows.Forms.Label
$txtb = New-Object System.Windows.Forms.TextBox
$lbl = New-Object System.Windows.Forms.Label
$itext = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$InvalidInput = "Invalid Input, try again"
#endregion Form Objects
#Provide Code for events
$handler_sbutn_Click=
{
If(($txtb.Text -eq '') -or ($txtb.Text -eq $InvalidInput)){
$txtb.Text = $InvalidInput
} Else {
$txtb.Text = "Congratulations, you didn't break it"
}
}
$handler_txtb_KeyDown=
{
if ($_.KeyCode -eq 'Enter'){
&$handler_sbutn_Click
}
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 265
$System_Drawing_Size.Width = 495
$form.ClientSize = $System_Drawing_Size
$form.DataBindings.DefaultDataSourceUpdateMode = 0
$form.Name = "form"
$form.Text = "Fernwartungssoftware"
$sbutn.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 190
$sbutn.Location = $System_Drawing_Point
$sbutn.Name = "sbutn"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 46
$System_Drawing_Size.Width = 125
$sbutn.Size = $System_Drawing_Size
$sbutn.TabIndex = 4
$sbutn.Text = "Send"
$sbutn.UseVisualStyleBackColor = $True
$sbutn.add_Click($handler_sbutn_Click)
$form.Controls.Add($sbutn)
$pr.AutoSize = $True
$pr.DataBindings.DefaultDataSourceUpdateMode = 0
$pr.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9,0,3,0)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 117
$pr.Location = $System_Drawing_Point
$pr.Name = "pr"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 15
$System_Drawing_Size.Width = 57
$pr.Size = $System_Drawing_Size
$pr.TabIndex = 3
$pr.Text = "Problem:"
$form.Controls.Add($pr)
$txtb.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 75
$System_Drawing_Point.Y = 117
$txtb.Location = $System_Drawing_Point
$txtb.Name = "txtb"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 381
$txtb.Size = $System_Drawing_Size
$txtb.TabIndex = 2
$txtb.add_KeyDown($handler_txtb_KeyDown)
$form.Controls.Add($txtb)
$lbl.AutoSize = $True
$lbl.DataBindings.DefaultDataSourceUpdateMode = 0
$lbl.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12.75,1,3,0)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 73
$lbl.Location = $System_Drawing_Point
$lbl.Name = "lbl"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 137
$lbl.Size = $System_Drawing_Size
$lbl.TabIndex = 1
$lbl.Text = "Bitte ausfüllen:"
$form.Controls.Add($lbl)
$itext.AutoSize = $True
$itext.DataBindings.DefaultDataSourceUpdateMode = 0
$itext.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",27.75,1,3,0)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 9
$itext.Location = $System_Drawing_Point
$itext.Name = "itext"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 42
$System_Drawing_Size.Width = 239
$itext.Size = $System_Drawing_Size
$itext.TabIndex = 0
$itext.Text = "Fernwartung"
$form.Controls.Add($itext)
#endregion Form Code
#Save the initial state of the form
$InitialFormWindowState = $form.WindowState
#Init the OnLoad event to correct the initial state of the form
$form.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm