Have event fire whenever any changes made to textboxes, comboboxs, etc. inside form

后端 未结 4 1739
别跟我提以往
别跟我提以往 2021-01-13 03:52

I\'m working with a C# WinForm. It has more than a dozen text boxes, combo boxes, and check boxes. The winform displays information that is retrieved from a database. There

4条回答
  •  半阙折子戏
    2021-01-13 04:26

    You have to go through through the controls and attach change event to every control. This article discuss the similar situation.

    private void AssignHandlersForControlCollection(
           Control.ControlCollection coll)
      {
          foreach (Control c in coll)
          {
              if (c is TextBox)
                  (c as TextBox).TextChanged 
                    += new EventHandler(SimpleDirtyTracker_TextChanged);
    
              if (c is CheckBox)
                  (c as CheckBox).CheckedChanged 
                    += new EventHandler(SimpleDirtyTracker_CheckedChanged);
    
              // ... apply for other desired input types similarly ...
    
              // recurively apply to inner collections
              if (c.HasChildren)
                  AssignHandlersForControlCollection(c.Controls);
          }
      }
    

提交回复
热议问题