[WinForm] sets the watermark text for TextBox, winformtextbox
Key code:
Using System; using System. runtime. interopServices; using System. windows. forms; namespace WinFormUtilHelpV2 {/// <summary> /// based on. NET 2.0 TextBox tool class // </summary> public static class TextBoxToolV2 {private const int EM_SETCUEBANNER = 0x1501; [DllImport ("user32.dll", CharSet = CharSet. auto)] private static extern Int32 SendMessage (IntPtr hWnd, int msg, int wParam, [financialas (UnmanagedType. LPWStr)] string lParam ); /// <summary> /// set the watermark text for TextBox /// </summary> /// <param name = "textBox"> TextBox </param> /// <param name = "watermark"> watermark text </param> public static void SetWatermark (this TextBox textBox, string watermark) {SendMessage (textBox. handle, EM_SETCUEBANNER, 0, watermark );} /// <summary> /// clear the watermark text /// </summary> /// <param name = "textBox"> TextBox </param> public static void ClearWatermark (this TextBox textBox) {SendMessage (textBox. handle, EM_SETCUEBANNER, 0, string. empty );}}}
Test code:
Using System; using System. windows. forms; using WinFormUtilHelpV2; namespace variables {public partial class WinTextBoxToolV2Test: Form {public WinTextBoxToolV2Test () {InitializeComponent ();} private void WinTextBoxToolV2Test_Load (object sender, EventArgs e) {textBox1.SetWatermark ("Enter the user name .... "); textBox2.SetWatermark (" enter your password .... ");} private void button#click (object sender, EventArgs e) {textBox1.ClearWatermark (); textBox2.ClearWatermark ();}}}
Test results:
Hope this is helpful!
In C # winform, how to set the color of Some fonts in the text box, that is, to change the color of Some fonts in the text box according to the conditions in programming
Modify all text colors in the text box, that is, use the ForeColor attribute.
If only some text colors are modified, the common text box cannot be modified. RichTextBox is used.
Problems with the font color of winform textbox
The TextBox Control does not have this function. We recommend that you use the RichTextBox Control,
Set the SelectionStart and SelectionLength attributes to determine the selected text, and then
RichTextBox1.SelectionColor = System. Drawing. Color. Red;
To set the color of the selected words.