give same property to all textbox controls

前端 未结 4 960
悲&欢浪女
悲&欢浪女 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:32

    How about an extension method to do it, called from your tabcontrol container...

    public static class ControlExtensions
    {
        public static void SetContextMenuOnChildTextBoxes(this Control control)
        {
            if (control is TextBox)
            {
                control.ContextMenu = new ContextMenu();
            }
            if (control.Controls != null)
            {
                foreach (Control controlChild in control.Controls)
                {
                    controlChild.SetContextMenuOnChildTextBoxes();
                }
            }
        }
    }
    

    This could be put in a common area of code so that it could be called from any parents that wanted this functionality.

提交回复
热议问题