C# 自定义文本框控件

C# 自定义文本框控件,c#,.net,winforms,custom-controls,C#,.net,Winforms,Custom Controls,是否可以在Visual Studio中创建如下文本框: 实际上。更好的解决方案是简单地使用文本框的Paint事件来绘制字符串 代码如下: class CueTextBox : TextBox { public event EventHandler CueTextChanged; private string _cueText; public string CueText { get { return _cueText; } set

是否可以在Visual Studio中创建如下文本框:


实际上。更好的解决方案是简单地使用文本框的Paint事件来绘制字符串

代码如下:

class CueTextBox : TextBox
{
    public event EventHandler CueTextChanged;
    private string _cueText;

    public string CueText
    {
        get { return _cueText; }
        set
        {
            value = value ?? string.Empty;
            if (value != _cueText)
            {
                _cueText = value;
                OnCueTextChanged(EventArgs.Empty);
            }
        }
    }

    public CueTextBox()
        : base()
    {
        _cueText = string.Empty;
    }

    protected virtual void OnCueTextChanged(EventArgs e)
    {
        this.Invalidate(true);
        if (this.CueTextChanged != null)
            this.CueTextChanged(this, e);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (string.IsNullOrEmpty(this.Text.Trim()) && !string.IsNullOrEmpty(this.CueText) && !this.Focused)
        {
            Point startingPoint = new Point(0, 0);
            StringFormat format = new StringFormat();
            Font font = new Font(this.Font.FontFamily.Name, this.Font.Size, FontStyle.Italic);
            if (this.RightToLeft == RightToLeft.Yes)
            {
                format.LineAlignment = StringAlignment.Far;
                format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
            }
            e.Graphics.DrawString(CueText, font, Brushes.Gray, this.ClientRectangle, format);
        }
    }

    const int WM_PAINT = 0x000F;
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_PAINT)
        {
            this.OnPaint(new PaintEventArgs(Graphics.FromHwnd(m.HWnd), this.ClientRectangle));
        }
    }
}

现在,您只需将“CueText”属性设置为所需的初始值即可

您只需要处理三个TextBox事件,在设计器中,将TextBox的文本设置为“username”,并将其设置为斜体,然后将TextBox BackColor设置为浅黄色,其余由事件处理程序处理

     private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
            ChangeTextBoxtoWatermark();
    }

    private void textBox1_MouseEnter(object sender, EventArgs e)
    {
        if (textBox1.Text == "username")
        {
            textBox1.Text = "";
            textBox1.Font = new Font(this.Font, FontStyle.Regular);
            textBox1.BackColor = Color.White;
        }
    }

    private void textBox1_MouseLeave(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
            ChangeTextBoxtoWatermark();
    }

    private void ChangeTextBoxtoWatermark()
    {
        textBox1.Font = new Font(this.Font, FontStyle.Italic);
        textBox1.BackColor = Color.LightYellow;
        textBox1.Text = "username";
    }

我已经检查过了,效果很好:)

如果您使用winforms,您可以在文本框中添加水印(用户名)…文本是否会像SO的搜索框一样在单击框后消失?我很高兴它对您起作用:)您可以折射文本框Tooriginal()这样的代码好提示,但对新手来说有点复杂:)这样您就不会修改textbox的“Text”属性,这可能会导致意外的结果。在我的解决方案中,文本仅绘制在文本框区域上,您可以使用text属性而不必担心我个人喜欢您的解决方案:)我喜欢您的方法,尽管您应该始终调用
Dispose
方法来释放
FromHwnd
方法创建的图形和相关资源。因此,我建议代码
使用(Graphics g=Graphics.FromHwnd(m.HWnd)){this.OnPaint(new PaintEventArgs(g,this.ClientRectangle));}