Controls within panel not accessible

rahul kumar 605 Reputation points
2023-06-28T11:28:00.5533333+00:00

Hi i am trying to get all the textboxes on my form and trying to attach a eventhandler to all of them but i found out that since the textboxes are spread in multiple panels on my form, my existing code cannot access them . please suggest a solution

   private void StockitemCDA_Load(object sender, EventArgs e)
        {  
         // add all controls having type starting from customtextbox bind to same textbox 
           Controls
           .OfType<customtextbox>()
           .ToList()
           .ForEach(b => { b.TriggerEvent += OnTriggerEvent; });
            
        


        }
    

        private void OnTriggerEvent(bool last, string name)

        {
            if (last)
            {
                       //    do something here 



            }
        }
Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-06-29T03:47:06.9366667+00:00

    Here is an example using a standard TextBox. Descendants gets all controls on a form no matter where they are and TextBoxList in this case filters down to TextBox controls so model one for your custom control.

    public static class ControlExtensions
    {
    
        public static IEnumerable<T> Descendants<T>(this Control control) where T : class
        {
            foreach (Control child in control.Controls)
            {
                if (child is T thisControl)
                {
                    yield return (T)thisControl;
                }
    
                if (child.HasChildren)
                {
                    foreach (T descendant in Descendants<T>(child))
                    {
                        yield return descendant;
                    }
                }
            }
        }
        public static List<TextBox> TextBoxList(this Control control) 
            => control.Descendants<TextBox>().ToList();
    }
    

    In a form

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.TextBoxList().ForEach(tb => 
                tb.Click += delegate (object tb, EventArgs args) {
                Debug.WriteLine(((TextBox)tb).Name);
            });
        }
    }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-06-29T03:12:10.9633333+00:00

    Hi @rahul kumar , Welcome to Microsoft Q&A.

    All the textboxes in the program are considered in the code example, and you can add only the controls in the panel using a similar method.

    What I added here is the click event as a demonstration, you can add the corresponding event you want.

    For example:

    foreach (Control childControl in control.Controls)
    {
        if (childControl is Panel)
        {
            foreach (Control control1 in childControl.Controls)
            {
                if (control1 is CustomTextBox)
                {
                    CustomTextBox textBox = (CustomTextBox)childControl;
                    textBox.Click += OnTriggerEvent;
                }
            }
        }
    }
    

    Here's the full code:

    using _6_xx_uc2;
    using System;
    using System.Windows.Forms;
    
    namespace _6_xx_1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                AttachEventHandlers(this);
            }
    
            private void AttachEventHandlers(Control control)
            {
                foreach (Control childControl in control.Controls)
                {
                    if (childControl is CustomTextBox)
                    {
                        CustomTextBox textBox = (CustomTextBox)childControl;
                        textBox.Click += OnTriggerEvent;
                    }
                    else if (childControl.HasChildren)
                    {
                        AttachEventHandlers(childControl);
                    }
                }
            }
    
            private void OnTriggerEvent(object sender, EventArgs e)
            {
                CustomTextBox customTextBox = (CustomTextBox)sender;
                customTextBox.Text = customTextBox.Name;
            }
    
        }
    }
    

    6-29

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.