C# .NET - how to find toolstrip button on a form by name?

Asked By Tridip Bhattacharjee on 04-Nov-10 03:15 AM
suppose i have form where toolstrip is there and that toolstrip contain many toolstrip button and other control.  how can i find is there any toolstrip button exist or not on the form just by name. can i write any function like findcontrol where i pass the control name means toolstrip button name as string to that function and that function will iterate in all the controls on the form and check any toolstrip button exist having that name which i pass to that function. please help me with sample code.

i have written a code snippet which is not able to find out the toolstrip button. my code as follows

public static System.Windows.Forms.Control FindControl(string id, System.Windows.Forms.Form.ControlCollection col)
      {
        System.Windows.Forms.Control child = null;
        child = FindControlRecursive(c, id);
          if (child != null)
            return child;
        
        return null;
      }

      private static System.Windows.Forms.Control FindControlRecursive(System.Windows.Forms.Control root, string id)
      {
        if (root.Name != null && root.Name == id)
          return root;

        foreach (System.Windows.Forms.Control c in root.Controls)
        {
          System.Windows.Forms.Control rc = FindControlRecursive(c, id);
          if (rc != null)
            return rc;
        }
        return null;
      }
Jatin Prajapati replied to Tridip Bhattacharjee on 04-Nov-10 05:40 AM
Hi Tridip, try this below method.

private Control FindControl(Control parent, string controlName)
        {
            Control c = null;
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl.GetType().ToString() == typeof(ToolStrip).ToString())
                {
                    foreach (ToolStripItem item in ((ToolStrip)ctrl).Items)
                    {
                        if (item.GetType().ToString() == typeof(ToolStripButton).ToString())
                        {
                            if (item.Name.Equals(controlName))
                            {
                                c = ctrl;
                                return c;
                            }
                        }
                    }
                }
                if (c == null)
                    c = FindControl(ctrl, controlName);
                else
                    break;
            }
            return c;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Control c = FindControl(this, "toolStripButton1");
        }


Please let me know the results.
Tridip Bhattacharjee replied to Jatin Prajapati on 04-Nov-10 09:00 AM
thanks a lot for your answer. but ctrl is ToolStrip. i need to iterate in all the child control of ToolStrip and convert them a normal control just to make the enable or disable. from your code i am getting ToolStrip instance instead of ToolStrip button instance. basically i need to extract ToolStrip button from ToolStrip and convert them as control and return the control from that function. plzz help me to do it. thanks a lot for your effort once again.
Jatin Prajapati replied to Tridip Bhattacharjee on 04-Nov-10 01:05 PM
Oops. Little mistake... please refer the below updated code.

private Control FindControl(Control parent, string controlName)
        {
            Control c = null;
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl.GetType().ToString() == typeof(ToolStrip).ToString())
                {
                    foreach (ToolStripItem item in ((ToolStrip)ctrl).Items)
                    {
                        if (item.GetType().ToString() == typeof(ToolStripButton).ToString())
                        {
                            if (item.Name.Equals(controlName))
                            {
                                c = item;
                                return c;
                            }
                        }
                    }
                }
                if (c == null)
                    c = FindControl(ctrl, controlName);
                else
                    break;
            }
            return c;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Control c = FindControl(this, "toolStripButton1");
        }


I've make the bold line for your idea. If you have carefully seen that, then you could also have solved it. Btw, let me know the results.