gpt4 book ai didi

winforms - Powershell 文本框占位符

转载 作者:行者123 更新时间:2023-12-03 01:28:35 41 4
gpt4 key购买 nike

我创建了我的 from,定义了我的 TextBoxes,并且对于我的占位符文本,我使用了以下代码:

$AssetText.Add_MouseClick({ $AssetText.text = “” })
$ErrorText.Add_MouseClick({ $ErrorText.text = “” })
$IssueText.Add_MouseClick({ $IssueText = “” })
$TestTagText.Add_MouseClick({ $TestTagText.text = “” })
$TroubleshootText.Add_MouseClick({ $TroubleshootText.text = “” })
$ResolutionText.Add_MouseClick({ $ResolutionText.text = “” })

它可以从 TextBox 中删除文本,但是如果我在任何 TextBox 中键入大量文本,然后在其外部单击,然后再返回它,它会删除我正在处理的文本。

我可以使用另一个比当前方法更好的功能吗?因此,最初我可以单击 $TextBox 以使文本消失,但是当在 $TextBox 内外单击后,在框中编写自己的文本时不会消失?

最佳答案

这是为文本框设置占位符文本的另一种方法。该方法依赖于处理 WM_PAINT 消息并已解释和实现here .
这种方法与其他方法(发送 EM_SETCUEBANNER )之间的区别在于这种方法也适用于多行 TextBox。
enter image description here

using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing
$assemblies = "System.Windows.Forms", "System.Drawing"
$code = @"
using System.Drawing;
using System.Windows.Forms;
public class ExTextBox : TextBox
{
string hint;
public string Hint
{
get { return hint; }
set { hint = value; this.Invalidate(); }
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf)
{
if (!this.Focused && string.IsNullOrEmpty(this.Text)
&& !string.IsNullOrEmpty(this.Hint))
{
using (var g = this.CreateGraphics())
{
TextRenderer.DrawText(g, this.Hint, this.Font,
this.ClientRectangle, SystemColors.GrayText , this.BackColor,
TextFormatFlags.Top | TextFormatFlags.Left);
}
}
}
}
}
"@
#Add the SendMessage function as a static method of a class
Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $code -Language CSharp

# Create an instance of MyForm.
$form = [Form] @{
ClientSize = [Point]::new(400,100);
Text = "Placeholder Sample";
}
$form.Controls.AddRange(@(
($textBox1 = [ExTextBox] @{Location = [Point]::new(10,10); Hint = "Start typing!" })
($textBox2 = [ExTextBox] @{Location = [Point]::new(10,40); Hint = "Start typing!";
MultiLine = $true; Height = 50; })
))
$null = $form.ShowDialog()
$form.Dispose()

关于winforms - Powershell 文本框占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60175542/

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