繁体   English   中英

如何在C#中的textbox1_TextChange事件中分离用户触发和程序触发

[英]How can i separate the user triggered and program triggered on textbox1_TextChange Event in C#

我正在使用C#.net 4.0 VS 2010。

我正在尝试创建一个模拟Facebook行为的文本框,特别是在文本框中有“+ Enter Message”并且是灰色的。 我还交换了tabindex,因此默认情况下没有选中文本框(破坏幻觉)。

据说当用户点击文本框时,textbox.text消失,然后Forecolor返回黑色。

会发生什么,它检测到我放在Form_Load上的程序更改,并在显示之前运行事件。

如何在textbox1_TextChange事件中分离用户触发和程序触发。

这是我的代码:

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {           
        //facebook illusion
        this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
        this.textBox1.Text = "+Enter Message";

    }

    //when the user clicks on the textbox
    private void textBox1_TextChanged(object sender, EventArgs e)
    {           
        if (this.textBox1.Text.Trim() == "+Enter Message")
        {
            this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
            this.textBox1.Text = "";
        }
    }

仅供参考,这是最终的工作代码-------------

    private void Form1_Load(object sender, EventArgs e)
    {           
        //facebook illusion
        this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);
        this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
        this.textBox1.Text = "+Enter Message";
        this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
        this.textBox1.Click += new System.EventHandler(this.textBox1_Click);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (this.textBox1.Text.Trim() == "+Enter Message")
        {
            this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
            this.textBox1.Text = "";
        }
    }

    private void textBox1_Click(object sender, EventArgs e)
    {
        if (this.textBox1.Text.Trim() == "+Enter Message")
        {
            this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
            this.textBox1.Text = "";
        }
    }

您可以在初始化后订阅TextChanged事件,例如:

private void Form1_Load(object sender, EventArgs e)
{           
    //facebook illusion
    this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
    this.textBox1.Text = "+Enter Message";
    this.textBox1.TextChanged += textBox1_TextChanged;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{      
    this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
    this.textBox1.Text = "";        
}

并将其从设计师中删除。 或者,您可以直接在设计器中设置ForeColorText “+ Enter消息”,这样初始化就在TextChanged事件订阅之前完成。

您可以先删除句柄来抑制事件

this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);

然后再添加它或者只是在form_load中更改文本后添加事件

this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM