[WinForm] sets the watermark text for TextBox, winformtextbox

Source: Internet
Author: User

[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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.