How to loop through all controls in a Windows Forms form or how to find if a particular control is a container control?

前端 未结 4 1609
野趣味
野趣味 2021-02-19 21:38

I will tell my requirement. I need to have a keydown event for each control in the Windows Forms form. It\'s better to do so rather than manually doing it for all c

4条回答
  •  无人及你
    2021-02-19 22:37

    A simple recursive function should do it.

    private void AddEvent(Control parentCtrl)
    {
      foreach (Control c in parentCtrl.Controls)
      {
        c.KeyDown += new KeyEventHandler(c_KeyDown);
        AddEvent(c);
      }
    }
    

提交回复
热议问题