Powershell 文本框占位符 [英] Powershell Textbox placeholder

查看:97
本文介绍了Powershell 文本框占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$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 内外后在框中编写自己的文本时不会消失?

解决方案

这是为 Textbox 设置占位符文本的另一种方法.该方法依赖于处理

使用汇编 System.Windows.Forms使用命名空间 System.Windows.Forms使用命名空间 System.Drawing$assemblies = "System.Windows.Forms", "System.Drawing";$code = @"使用 System.Drawing;使用 System.Windows.Forms;公共类 ExTextBox : 文本框{字符串提示;公共字符串提示{得到 { 返回提示;}设置 { 提示 = 值;this.Invalidate();}}protected override void WndProc(ref Message m){base.WndProc(ref m);如果(短信= 0xf){if (!this.Focused && string.IsNullOrEmpty(this.Text)&&!string.IsNullOrEmpty(this.Hint)){使用 (var g = this.CreateGraphics()){TextRenderer.DrawText(g, this.Hint, this.Font,this.ClientRectangle, SystemColors.GrayText , this.BackColor,TextFormatFlags.Top |TextFormatFlags.Left);}}}}}"@#添加SendMessage函数作为类的静态方法添加类型 -ReferencedAssemblies $assemblies -TypeDefinition $code -Language CSharp# 创建一个 MyForm 的实例.$form = [表格] @{ClientSize = [Point]::new(400,100);Text = "Placeholder Sample";}$form.Controls.AddRange(@(($textBox1 = [ExTextBox] @{Location = [Point]::new(10,10); Hint = 开始输入!"})($textBox2 = [ExTextBox] @{Location = [Point]::new(10,40); Hint = 开始打字!";多行 = $true;高度 = 50;})))$null = $form.ShowDialog()$form.Dispose()

I create my from, define my TextBoxes, and for my placeholder text I'm using the following code:

$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 = "" })

It works to remove text from the TextBox, but if I type a fair amount of text in any TextBox and then click outside of it, then come back to it, it erases the text I was working on.

Is there another function I can use that would work better than this current method? So that initially I can click the $TextBox to make the text disappear, but when writing my own text in the box wont disappear after clicking in and outside of the $TextBox?

解决方案

Here is another approach on setting Placeholder text for Textbox. The approach relies on handling WM_PAINT message and has been explained and implemented here.

The difference between this approach and the other approach (sending EM_SETCUEBANNER) is this approach works for multi-line TextBox as well.

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()

这篇关于Powershell 文本框占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆