File->New->Website.
Delete the default UserControl1.cs
Add New Item
Add New Class.
write the following code in class as shown below.
public class TextBoxControl:TextBox
{
Color waterMarkColor = Color.Gray;
Color forecolor;
Font font;
string waterMarkText = "Any Text";
public TextBoxControl()
{
base.Text = this.waterMarkText;
this.forecolor = this.ForeColor;
this.ForeColor = this.waterMarkColor;
this.font = this.Font;
this.TextChanged += new EventHandler(txtTextBox_TextChanged);
this.KeyPress += new KeyPressEventHandler(txtTextBox_KeyPress);
this.LostFocus += new EventHandler(txtTextBox_TextChanged);
}
void txtTextBox_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.Text))
{
this.ForeColor = this.forecolor;
this.Font = this.font;
}
else
{
this.TextChanged -= new EventHandler(txtTextBox_TextChanged);
base.Text = this.waterMarkText;
this.TextChanged += new EventHandler(txtTextBox_TextChanged);
this.ForeColor = this.waterMarkColor;
}
}
void txtTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
string str = base.Text.Replace(this.waterMarkText, "");
this.TextChanged -= new EventHandler(txtTextBox_TextChanged);
this.Text = str;
this.TextChanged += new EventHandler(txtTextBox_TextChanged);
}
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets Watermark color")]
[DisplayName("WaterMark Color")]
public Color WaterMarkColor
{
get
{
return this.waterMarkColor;
}
set
{
this.waterMarkColor = value;
base.OnTextChanged(new EventArgs());
}
}
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets TextBox text")]
[DisplayName("Text")]
/// <summary>
/// Property to get Text at runtime(hides base Text property)
/// </summary>
public new string Text
{
get
{
//required for validation for Text property
return base.Text.Replace(this.waterMarkText, string.Empty);
}
set
{
base.Text = value;
}
}
/// <summary>
/// Property to set/get Watermark text at design/runtime
/// </summary>
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets Watermark Text")]
[DisplayName("WaterMark Text")]
public string WaterMarkText
{
get
{
return this.waterMarkText;
}
set
{
this.waterMarkText = value;
base.OnTextChanged(new EventArgs());
}
}
}
Click on Browse Button and add the dll of the custom control library.
And drag that control in the Form as shown Below.
Click on Control (Right Click and Select Properties)
Delete the default UserControl1.cs
Add New Item
Add New Class.
write the following code in class as shown below.
public class TextBoxControl:TextBox
{
Color waterMarkColor = Color.Gray;
Color forecolor;
Font font;
string waterMarkText = "Any Text";
public TextBoxControl()
{
base.Text = this.waterMarkText;
this.forecolor = this.ForeColor;
this.ForeColor = this.waterMarkColor;
this.font = this.Font;
this.TextChanged += new EventHandler(txtTextBox_TextChanged);
this.KeyPress += new KeyPressEventHandler(txtTextBox_KeyPress);
this.LostFocus += new EventHandler(txtTextBox_TextChanged);
}
void txtTextBox_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.Text))
{
this.ForeColor = this.forecolor;
this.Font = this.font;
}
else
{
this.TextChanged -= new EventHandler(txtTextBox_TextChanged);
base.Text = this.waterMarkText;
this.TextChanged += new EventHandler(txtTextBox_TextChanged);
this.ForeColor = this.waterMarkColor;
}
}
void txtTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
string str = base.Text.Replace(this.waterMarkText, "");
this.TextChanged -= new EventHandler(txtTextBox_TextChanged);
this.Text = str;
this.TextChanged += new EventHandler(txtTextBox_TextChanged);
}
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets Watermark color")]
[DisplayName("WaterMark Color")]
public Color WaterMarkColor
{
get
{
return this.waterMarkColor;
}
set
{
this.waterMarkColor = value;
base.OnTextChanged(new EventArgs());
}
}
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets TextBox text")]
[DisplayName("Text")]
/// <summary>
/// Property to get Text at runtime(hides base Text property)
/// </summary>
public new string Text
{
get
{
//required for validation for Text property
return base.Text.Replace(this.waterMarkText, string.Empty);
}
set
{
base.Text = value;
}
}
/// <summary>
/// Property to set/get Watermark text at design/runtime
/// </summary>
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets Watermark Text")]
[DisplayName("WaterMark Text")]
public string WaterMarkText
{
get
{
return this.waterMarkText;
}
set
{
this.waterMarkText = value;
base.OnTextChanged(new EventArgs());
}
}
}
Now, Build the solution the dll , will be generated. and then add new project- and add reference of this dll to it.
Go to Tool Box.
Click on Browse Button and add the dll of the custom control library.
And drag that control in the Form as shown Below.
Click on Control (Right Click and Select Properties)
No comments:
Post a Comment
Thank you for visiting my blog