1

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?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
user10850
  • 49
  • 7
  • You could use tab to go back rather than mouse but why do this anyway? It's standard Windows convention that to replace text a user selects it and deletes or types over it. – Scepticalist Feb 11 '20 at 19:12
  • 1
    Inside each of the Click events, check if the text box contains the placeholder text and nothing else. Only erase when that is the case. – Theo Feb 11 '20 at 19:14
  • But he's using blank as placeholder - that's never going to work at all :) – Scepticalist Feb 11 '20 at 19:16

2 Answers2

3

If you would like to have placeholder in native OS way, you can send EM_SETCUEBANNER to TextBox to set the placeholder text:

enter image description here

using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing
$code = @"
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd, 
    int msg, IntPtr wParam, string lParam);  
public const int EM_SETCUEBANER = 0x1501;
"@
$Win32Helpers = Add-Type -MemberDefinition $code -Name "Win32Helpers" -PassThru
$form = [Form] @{
    ClientSize = [Point]::new(400,100);
    Text = "Placeholder Text";
}    
$form.Controls.AddRange(@(
    ($textBox1 = [TextBox] @{Location = [Point]::new(10,10) })
    ($textBox2 = [TextBox] @{Location = [Point]::new(10,40) })
))
$textBox1.add_HandleCreated({
    $Win32Helpers::SendMessage($textBox1.Handle,$Win32Helpers::EM_SETCUEBANER`
         , [IntPtr]0, "Start typing ...")
    $Win32Helpers::SendMessage($textBox2.Handle,$Win32Helpers::EM_SETCUEBANER`
         , [IntPtr]0, "Start typing ...")
})
$null = $form.ShowDialog()
$form.Dispose()
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Is it scary? this is a [well-known](https://stackoverflow.com/a/4902969/3110834) OS supported way of setting placeholder text. However there is [another solution](https://stackoverflow.com/a/36534068/3110834) which is used in .NET CORE, but it will be more scary – Reza Aghaei Feb 12 '20 at 19:52
  • And in general I'd say setting/clearing `Text` is not a good idea because it raises `TextChanged` event, also the value goes to underlying data sources while it's invalid. – Reza Aghaei Feb 12 '20 at 19:57
  • Thanks, this works. But 2 of my forms are multiline. How do I implement cuebanner with a multiline = $true ? – user10850 Feb 13 '20 at 00:36
  • multiline + this would literally solve my last problem in my code – user10850 Feb 13 '20 at 00:38
2

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.

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()
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398