Winform watermark Textbox (almost perfect)

Source: Internet
Author: User
Public   Partial   Class Watertextbox: textbox { Private   Readonly Label lblwatertext = New Label (); Public Watertextbox () {initializecomponent (); lblwatertext. borderstyle = borderstyle. None; lblwatertext. Enabled = False ; Lblwatertext. backcolor = color. White; lblwatertext. autosize = False ; Lblwatertext. Top = 1; lblwatertext. Left = 2; lblwatertext. flatstyle = flatstyle. system; controls. Add (lblwatertext);} [category ( "Extended attributes" ), Description ( "Displayed prompt information" )] Public   String Watertext {get { Return Lblwatertext. Text;} set {lblwatertext. Text = Value ;}} Public   Override   String Text {set {lblwatertext. Visible =Value = String . Empty; Base . Text = Value ;} Get { Return   Base . Text ;}} Protected   Override   Void Onsizechanged (eventargs e ){ If (Multiline & (scrollbars = scrollbars. Vertical | scrollbars = scrollbars. Both) lblwatertext. width = width-20; Else Lblwatertext. width = width; lblwatertext. Height = height-2; Base . Onsizechanged (E );} Protected   Override   Void Ontextchanged (eventargs e) {lblwatertext. Visible = Base . Text = String . Empty; Base . Ontextchanged (E );} Protected   Override   Void Onmousedown (mouseeventargs e) {lblwatertext. Visible =False ; Base . Onmousedown (E );} Protected   Override   Void Onmouseleave (eventargs e) {lblwatertext. Visible = Base . Text = String . Empty; Base . Onmouseleave (E );} // Protected override void onenter (eventargs E)      //{      // Lblwatertext. Visible = false;      // Base. onenter (E );     //}      // Protected override void onleave (eventargs E)      //{      // If (string. isnullorempty (base. Text ))      // Lblwatertext. Visible = true;      // Base. onleave (E );      //} }

Add a method (for conversion ):

Public partial class prompttextbox: textbox
{
Private string promptstring = string. empty;
Private bool isempty = true;
Public prompttextbox ()
{
Initializecomponent ();
}
Public prompttextbox (string prompt)
{
Initializecomponent ();
Promptstring = prompt;
}
/**/
/// <Summary>
/// Prompt text
/// </Summary>
Public String promptstring
{
Get {return promptstring ;}
Set {promptstring = value ;}
}
Protected override void onenter (eventargs E)
{
If (isempty) {clear ();}
}
Protected override void onleave (eventargs E)
{
If (text = string. Empty)
{
TEXT = promptstring;
Isempty = true;
}
Else {isempty = false ;}
}
Public override string text
{
Get
{
If (isempty) {return string. Empty ;}
Else {return base. Text ;}
}
Set
{
If (value = string. Empty)
{
Base. Text = promptstring;
Isempty = true;
}
Else
{
Isempty = (value = promptstring );
Base. Text = value;
}
}
}
Protected override void oncreatecontrol ()
{
If (text = string. Empty)
{
TEXT = promptstring;
Isempty = true;
}
Else {isempty = false ;}
}
}

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.