我有一个查询。 我有这个Powershell表单,带有一个文本框,一个按钮和一个标签! 当我按下按钮时,它将在表单上创建一个标签,其中包含有关我在文本框中输入内容的信息。那行得通。 现在,我现在要实现的目标是:再次按下该按钮时,我希望将信息替换为新信息(例如,如果我输入到文本框中的文本已更新, ) 但是我无法解决该问题。
第二次按下按钮后,我同时尝试了SELECT *
FROM messages
WHERE priority >= 2
AND (priority = 2 AND id > 1000 OR priority > 2)
ORDER BY priority asc, id asc
和$main_form.Remove($mylabel)
-但都没有成功!
$main_form.Refresh()
上面的示例不包含Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = "Testing"
$main_form.Width = 715
$main_form.height = 300
$main_form.FormBorderStyle = "FixedDialog"
$main_form.AutoSize = $true
$main_form.MaximizeBox = $false
$main_form.MinimizeBox = $false
$CheckInfo = New-Object System.Windows.Forms.TextBox
$CheckInfo.Width = 200
$CheckInfo.Height = 100
$CheckInfo.Location = New-Object System.Drawing.Point(250,53)
$CheckInfo.AutoSize = $true
$main_form.Controls.Add($CheckInfo)
$CheckButton = New-Object System.Windows.Forms.Button
$CheckButton.Location = New-Object System.Drawing.Point(250,100)
$CheckButton.Size = New-Object System.Drawing.Size(100,20)
$CheckButton.Text = "Check Information test"
$CheckButton.AutoSize = $true
$main_form.Controls.Add($CheckButton)
$CheckButton.Add_Click({
$CheckButtonLabelsuccess = New-Object System.Windows.Forms.Label
$CheckButtonLabelsuccess.Text = "$CheckInfo.Text"
$CheckButtonLabelsuccess.Location = New-Object System.Drawing.Point(250,150)
$CheckButtonLabelsuccess.ForeColor = "#50ed07"
$CheckButtonLabelsuccess.AutoSize = $true
$main_form.Controls.Add($CheckButtonLabelsuccess)
})
$main_form.Topmost = $true
$main_form.ShowDialog()
或$main_form.Remove()
代码,因为我可能将它们添加到错误的位置以使它们无法正常工作,所以我暂时将其删除
所以我的问题是:我应该在哪里输入这些代码($main_form.Refresh()
,.Remove()
),它们如何工作?我尝试四处搜寻,但没有找到适合我情况的解决方案。很抱歉,以前是否有人问过这个问题,但找不到任何相似的地方
任何帮助我解决此问题的帮助,我们都感激不尽:)
答案 0 :(得分:3)
如果我对问题的理解正确,那么您希望在用户第二次单击按钮时就删除标签。
您可以通过给标签动态创建一个名称来执行此操作,并使用该名称来测试标签是否已添加到表单中。
将您的Click事件更改为:
$CheckButton.Add_Click({
if ($main_form.Controls.ContainsKey("MyInfoLabel")) {
# the label is already present. Remove it
$main_form.Controls.RemoveByKey("MyInfoLabel")
}
else {
# no control with that name found, so create it
$CheckButtonLabelsuccess = New-Object System.Windows.Forms.Label
$CheckButtonLabelsuccess.Name = "MyInfoLabel"
$CheckButtonLabelsuccess.Text = "Info blablabla"
$CheckButtonLabelsuccess.Location = New-Object System.Drawing.Point(250,150)
$CheckButtonLabelsuccess.ForeColor = "#50ed07"
$CheckButtonLabelsuccess.AutoSize = $true
$main_form.Controls.Add($CheckButtonLabelsuccess)
}
})
希望有帮助