Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add placeholder text to ToolStripTextBox?

In a WinForms project, I know how to add placeholder text to a regular textbox. But the ToolStripTextBox doesn't appear to be a regular textbox. For one, it doesn't expose the handle (which is what's required to set the placeholder text via Win API).

So, how do I either set the placeholder text on a ToolStripTextBox or get its .Handle property?

like image 559
AngryHacker Avatar asked Oct 20 '25 05:10

AngryHacker


2 Answers

ToolStripTextBox hosts a ToolStripTextBoxControl inside which is derived from TextBox and you can access the the hosted control using its TextBox or its Control property. So you can write such code:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

[ToolboxBitmap(typeof(ToolStripTextBox))]
public class MyToolStripTextBox : ToolStripTextBox
{
    private const int EM_SETCUEBANNER = 0x1501;
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern Int32 SendMessage(IntPtr hWnd, int msg,
        int wParam, string lParam);
    public MyToolStripTextBox()
    {
        this.Control.HandleCreated += Control_HandleCreated;
    }
    private void Control_HandleCreated(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(cueBanner))
            UpdateCueBanner();
    }
    string cueBanner;
    public string CueBanner
    {
        get { return cueBanner; }
        set
        {
            cueBanner = value;
            UpdateCueBanner();
        }
    }
    private void UpdateCueBanner()
    {
        SendMessage(this.Control.Handle, EM_SETCUEBANNER, 0, cueBanner);
    }
}
like image 75
Reza Aghaei Avatar answered Oct 21 '25 20:10

Reza Aghaei


Have not tried myself.

But the Remarks section indicates you can manipulate the TextBox control directly.

ToolStripTextBox is the TextBox optimized for hosting in a ToolStrip. A subset of the hosted control's properties and events are exposed at the ToolStripTextBox level, but the underlying TextBox control is fully accessible through the TextBox property.

like image 39
kennyzx Avatar answered Oct 21 '25 20:10

kennyzx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!