Assigning event TextChanged to all textboxes in Form

后端 未结 3 1775
一个人的身影
一个人的身影 2020-12-11 14:11

Hello I was wondering how can I keep an eye on all textboxes in Form whether in any of them was changed value. I saw some code here

private voi         


        
3条回答
  •  -上瘾入骨i
    2020-12-11 14:34

    You need another loop for the groupbox and panels, you can use this code:

    private void addEvents(Control.ControlCollection ct)
    {
        foreach (Control ctrl in ct)
        {
            if (ctrl is TextBox)
            {
                TextBox tb = (TextBox)ctrl;
                tb.TextChanged += new EventHandler(tb_TextChanged);
            }
            else if (ctrl is GroupBox || ctrl is Panel) addEvents(ctrl.Controls);
        }
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        addEvents(this.Controls);
    }
    
    void tb_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)sender;
        tb.Tag = "CHANGED"; // or whatever
    }
    

提交回复
热议问题