自定义文本框控件 [英] Custom textbox control

查看:154
本文介绍了自定义文本框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to create a textbox in Visual Studio like this:

推荐答案

您将只需要处理三个TextBox的事件,在设计器中设置的文本文本框为用户名,并使其字体斜体然后设置文本框背景色为浅黄,其余由事件处理程序...

You would just need to handle three TextBox events, in the Designer set the text of the TextBox to "username" and make it Font Italics then set TextBox BackColor to LightYellow,the rest is handled by Event handlers...

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

我已经检查它,它工作得很好:)

I have checked it and it works fine :)

这篇关于自定义文本框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆