-1

I am trying to loop through all ToolStripButtons and find all ToolStripButtons. My Code is:

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

            else
            {
                break;
            }
        }
        return c;
    }

But it doesn't yield expected result.enter image description here

Khalid
  • 1
  • 4
  • 2
    `foreach (ToolStrip ctrl in parent.Owner.Items.ToString())`? `.ToString()` seems to be erroneous here – Dmitry Bychenko Sep 04 '18 at 09:57
  • 1
    Side note: `ctrl.GetType().ToString() == typeof(ToolStrip).ToString()` - there's no need in `ToString()`: `ctrl.GetType() == typeof(ToolStrip)` – Dmitry Bychenko Sep 04 '18 at 10:00
  • Possible duplicate? [How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)](https://stackoverflow.com/q/3419159/982149)? Or [Get all controls of a specific type](https://stackoverflow.com/q/4630391/982149) – Fildor Sep 04 '18 at 10:00
  • `ToolStripButton` is not a control, so you can not find it in controls collection. It's not clear what you are looking for. Add more description and make it clear what the method is supposed to do, what input and what output do you expect? – Reza Aghaei Sep 04 '18 at 14:41

3 Answers3

6

Using Linq .OfType<T>(), you can greatly simplify finding the desired controls:

var toolstrips = parent.Owner.Items.OfType<ToolStripButton>();
Filburt
  • 17,626
  • 12
  • 64
  • 115
3

To simplify this you can write a little helper function like so:

public static IEnumerable<Control> AllControls(Control parent)
{
    if (parent == null)
        throw new ArgumentNullException();

    return implementation();

    IEnumerable<Control> implementation()
    {
        foreach (Control control in parent.Controls)
        {
            foreach (Control child in AllControls(control))
            {
                yield return child;
            }

            yield return control;
        }
    }
}

And then you can find all controls of a particular type like so:

var allToolstripButtons = AllControls(parent).OfType<ToolStripButton>();

This is a recursive solution that will find all child controls regardless of how deeply they are nested within the control structure - if you only need to find ToolStripButton items that are immediate children of a given parent, you can just use @Filburt's simple answer.

(Note: The reason for the curious inline implementation() function is to ensure that the parent argument is checked for nullness before the Enumerable result is evaluated.)

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • 1
    Thank you, it list everything else but ToolStripButtons, see the pic I added to the original question. And also just to clarify I am accessing other forms controls from a SysAdmin form. – Khalid Sep 04 '18 at 11:50
0

What I meant was to list all controls of a form into one ListBox in order to be able to set properties (for control based security), I finally figured it out.

Control Based Security

So I figured it with this code"

private void ShowControls(Control.ControlCollection controlCollection)
    {
        foreach (Control c in controlCollection)
        {
            if (c.Controls.Count > 0)
            {
                ShowControls(c.Controls);
            }
            if (c is MenuStrip)
            {
                MenuStrip menuStrip = c as MenuStrip;
                ShowToolStripItems(menuStrip.Items);
            }
            if (c is Panel)
            {
                formToolTip2.SetToolTip(c, c.Name);
                PageControls.Items.Add(c.Name);
            }
            if (c is Button || c is ComboBox || c is TextBox ||
                c is ListBox || c is DataGridView || c is RadioButton ||
                c is RichTextBox || c is TabPage || c is CheckBox || c is ToolStrip)
            {
                var toolstrips = controlCollection.OfType<ToolStrip>();
                foreach (ToolStrip ts in toolstrips)
                {
                    foreach (ToolStripItem tsi in ts.Items)
                    {
                        if (ts.Items.Count > 0)
                        {
                            if (tsi is ToolStripButton)
                            {
                                ToolStripButton tb = tsi as ToolStripButton;
                                tb.ToolTipText = tb.Name;
                                if (!PageControls.Items.Contains(tb.Name))
                                {
                                    PageControls.Items.Add(tb.Name);
                                }
                            }
                        }
                    }
                }
                formToolTip2.SetToolTip(c, c.Name);
                PageControls.Items.Add(c.Name);
            }
        }
    }
Khalid
  • 1
  • 4