我为我的项目编写了一个可重用的自定义控件类。
也许它可以帮助需要在项目中实现多个占位文本框的人。
这是C#和vb.net版本:
C#:
我为我的项目编写了一个可重用的自定义控件类。
也许它可以帮助需要在项目中实现多个占位文本框的人。
这是C#和vb.net版本:
namespace reusebleplaceholdertextbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CustomPlaceHolderTextbox myCustomTxt = new CustomPlaceHolderTextbox(
"Please Write Text Here...", Color.Gray, new Font("ARIAL", 11, FontStyle.Italic)
, Color.Black, new Font("ARIAL", 11, FontStyle.Regular)
);
myCustomTxt.Multiline = true;
myCustomTxt.Size = new Size(200, 50);
myCustomTxt.Location = new Point(10, 10);
this.Controls.Add(myCustomTxt);
}
}
class CustomPlaceHolderTextbox : System.Windows.Forms.TextBox
{
public string PlaceholderText { get; private set; }
public Color PlaceholderForeColor { get; private set; }
public Font PlaceholderFont { get; private set; }
public Color TextForeColor { get; private set; }
public Font TextFont { get; private set; }
public CustomPlaceHolderTextbox(string placeholdertext, Color placeholderforecolor,
Font placeholderfont, Color textforecolor, Font textfont)
{
this.PlaceholderText = placeholdertext;
this.PlaceholderFont = placeholderfont;
this.PlaceholderForeColor = placeholderforecolor;
this.PlaceholderFont = placeholderfont;
this.TextForeColor = textforecolor;
this.TextFont = textfont;
if (!string.IsNullOrEmpty(this.PlaceholderText))
{
SetPlaceHolder(true);
this.Update();
}
}
private void SetPlaceHolder(bool addEvents)
{
if (addEvents)
{
this.LostFocus += txt_lostfocus;
this.Click += txt_click;
}
this.Text = PlaceholderText;
this.ForeColor = PlaceholderForeColor;
this.Font = PlaceholderFont;
}
private void txt_click(object sender, EventArgs e)
{
RemovePlaceHolder();
this.GotFocus += txt_focus;
this.Click -= txt_click;
}
private void RemovePlaceHolder()
{
this.Text = "";
this.ForeColor = TextForeColor;
this.Font = TextFont;
}
private void txt_lostfocus(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.Text))
{
SetPlaceHolder(false);
}
}
private void txt_focus(object sender, EventArgs e)
{
if (this.Text == PlaceholderText)
{
RemovePlaceHolder();
}
}
}
}
VB.NET:
Namespace CustomControls
Public Class PlaceHolderTextBox
Inherits System.Windows.Forms.TextBox
Public Property PlaceholderText As String
Public Property PlaceholderForeColor As Color
Public Property PlaceholderFont As Font
Public Property TextForeColor As Color
Public Property TextFont As Font
Public Sub New(ByVal placeholdertext As String, ByVal placeholderforecolor As Color, ByVal placeholderfont As Font, ByVal txtboxbackcolor As Color, ByVal textforecolor As Color, ByVal textfont As Font)
Me.PlaceholderText = placeholdertext
Me.PlaceholderFont = placeholderfont
Me.PlaceholderForeColor = placeholderforecolor
Me.PlaceholderFont = placeholderfont
Me.TextForeColor = textforecolor
Me.TextFont = textfont
Me.BackColor = txtboxbackcolor
If Not String.IsNullOrEmpty(Me.PlaceholderText) Then
SetPlaceHolder(True)
Me.Update()
End If
End Sub
Private Sub SetPlaceHolder(ByVal addEvents As Boolean)
If addEvents Then
AddHandler Me.LostFocus, AddressOf txt_lostfocus
AddHandler Me.Click, AddressOf txt_click
End If
Me.Text = PlaceholderText
Me.ForeColor = PlaceholderForeColor
Me.Font = PlaceholderFont
End Sub
Private Sub txt_click(ByVal sender As Object, ByVal e As EventArgs)
RemovePlaceHolder()
AddHandler Me.GotFocus, AddressOf txt_focus
RemoveHandler Me.Click, AddressOf txt_click
End Sub
Private Sub RemovePlaceHolder()
Me.Text = ""
Me.ForeColor = TextForeColor
Me.Font = TextFont
End Sub
Private Sub txt_lostfocus(ByVal sender As Object, ByVal e As EventArgs)
If String.IsNullOrEmpty(Me.Text) Then
SetPlaceHolder(False)
End If
End Sub
Private Sub txt_focus(ByVal sender As Object, ByVal e As EventArgs)
If Me.Text = PlaceholderText Then
RemovePlaceHolder()
End If
End Sub
End Class
End Namespace