【问题标题】:Custom textbox control [duplicate]自定义文本框控件 [重复]
【发布时间】:2012-02-12 09:29:10
【问题描述】:

是否可以像这样在 Visual Studio 中创建一个文本框:

【问题讨论】:

  • 如果您使用的是winforms,您可以在您的情况下将水印(用户名)添加到文本框...
  • 点击SO的搜索框之类的框后文字会消失吗?
  • 很高兴它对你有用 :)

标签: c# .net winforms custom-controls


【解决方案1】:

其实。更好的解决方案是简单地使用文本框的 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”属性设置为您想要的初始值即可!

【讨论】:

  • 不错的提示,但对新手来说不太复杂 :)
  • 这样你就不会修改文本框的'Text'属性,这可能会导致意想不到的结果。在我的解决方案中,文本仅绘制在文本框区域上,您可以毫无顾虑地使用 Text 属性
  • 我个人喜欢你的解决方案 :)
  • 我喜欢你的方法,虽然你应该总是调用Dispose方法来释放FromHwnd方法创建的Graphics和相关资源。所以我建议代码using (Graphics g = Graphics.FromHwnd(m.HWnd)) { this.OnPaint(new PaintEventArgs(g, this.ClientRectangle)); }
【解决方案2】:

您只需要处理三个 TextBox 事件,在设计器中将 TextBox 的文本设置为“用户名”并使其字体斜体,然后将 TextBox BackColor 设置为 LightYellow,其余由事件处理程序处理...

     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";
    }

我已经检查过了,它工作正常 :)

【讨论】:

  • 你可以像TextBoxToOriginal()一样折射代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 2012-11-03
  • 1970-01-01
相关资源
最近更新 更多