gpt4 book ai didi

powershell - 如何在Powershell中删除 “items”表单

转载 作者:行者123 更新时间:2023-12-02 23:18:31 25 4
gpt4 key购买 nike

我有一个查询。
我有这个Powershell表单,带有一个文本框,一个按钮和一个标签!
当我按下按钮时,它会在表单上创建一个标签,其中包含有关我在文本框中输入内容的信息。那行得通。
现在,我现在要实现的目标是:再次按下按钮时,我希望将信息替换为新信息(例如,如果我输入到文本框中的文本已更新, )
但我无法解决如何实现这一目标。

第二次按下按钮后,我同时尝试了$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().Refresh())以及它们如何工作?我尝试四处搜寻,但没有找到适合我情况的解决方案。很抱歉,以前是否有人问过这个问题,但找不到任何相似的地方

任何帮助我解决此问题的帮助,我们将不胜感激:)

最佳答案

如果我对问题的理解正确,那么您希望在用户第二次单击按钮时就删除标签。

您可以通过为标签动态创建名称来执行此操作,并使用该名称来测试标签是否已添加到表单中。

将您的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)
}
})

希望能有所帮助

关于powershell - 如何在Powershell中删除 “items”表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58202435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com