give same property to all textbox controls

前端 未结 4 954
悲&欢浪女
悲&欢浪女 2021-01-24 02:02

how to give same property to all textboxes present in the same form.

      foreach (var textbox in this.Controls.OfType())
        {
            t         


        
4条回答
  •  抹茶落季
    2021-01-24 02:33

    Just use the recursion to go through all controls subcollections:

    void SetControl(ContextMenu menu, Control control)
    {
        if (control is TextBox)
            control.ContextMenu = menu;
        else
        {
            foreach (Control c in control.Controls)
                SetControl(menu, c);
        }
    }
    

    It will find all the textboxes and set one and the same context menu to all of them.

    You mal call it,say, from form's OnLoad event handler. While it's assumed that you have yourContextMenu defined for the form.

        private void Form1_Load(object sender, EventArgs e)
        {
            SetControl(yourContextMenu, this);
        }
    

提交回复
热议问题