控件得到焦点和失去焦点触发事件的实现(C#)

January 27, 2012 作者:eseeker 分类:编程相关 No Comments

    public Form1()
    {
        InitializeComponent();
        textBox1.Enter += new EventHandler(textBox1_Enter);  //获得焦点事件
        textBox1.Leave += new EventHandler(textBox1_Leave);  //失去焦点事件。  
    }

    void textBox1_Enter(object sender, EventArgs e)
    {
        MessageBox.Show("获得了焦点");
    }

    void textBox1_Leave(object sender, EventArgs e)
    {
        MessageBox.Show("失去了焦点");
    }

一般知道双击控件,得到Changed事件,然后把事件改成想要的事件就行,比如把_TextChanged改成_Leave事件,"文件名.Designer.cs"中事件的声明也要改过来。

标签: C#, VS

添加新评论 »

千万别勾我,否则我就不告诉你“伊妹儿”有人踩你。