简体   繁体   中英

How can I loop thru all controls (including ToolStripItems) C#

I need to save and restore settings for specific controls on a form.

I loop thru all controls and return the one whose name matches the one I want, like so:

private static Control GetControlByName(string name, Control.ControlCollection Controls)
{
  Control thisControl = null;
  foreach (Control c in Controls)
  {
    if (c.Name == name)
    {
      thisControl = c;
      break;
    }
    if (c.Controls.Count > 0)
    {
        thisControl = GetControlByName(name, c.Controls);
      if (thisControl != null)
      {
        break;
      }
    }
  }
  return thisControl;
}

From this I can determine the type of control and therefore the property that should be / has been stored.

This works well unless the control is one of the ToolStrip family which has been added to a toolstrip. eg

this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.lblUsername,  // ToolStripLabel 
        this.toolStripSeparator1,
        this.cbxCompany}); // ToolStripComboBox 

In this case I can see the control I'm interested in (cbxCompany) when debugging, but the name property has no value so the code does not match to it.

Any suggestions on how I can get to these controls too?

Cheers, Murray

Thanks for your help guys.

Pinichi set me on the right track, I was checking toolStrip.Controls - should have been toolStrip.Items

The code below now works perfectly for me:

private static Control GetControlByName(string controlName, Control.ControlCollection parent)
{
  Control c = null;
  foreach (Control ctrl in parent)
  {
    if (ctrl.Name.Equals(controlName))
    {
      c = ctrl;
      return c;
    }

    if (ctrl.GetType() == typeof(ToolStrip))
    {
      foreach (ToolStripItem item in ((ToolStrip)ctrl).Items)
      {
        if (item.Name.Equals(controlName))
        {
          switch (item.GetType().Name)
          {
            case "ToolStripComboBox":
              c = ((ToolStripComboBox)item).Control;
              break;
            case "ToolStripTextBox":
              c = ((ToolStripTextBox)item).Control;
              break;
          }
          if (c != null)
          {
            break;
          }
        }
      }
    }
    if (c == null)
      c = GetControlByName(controlName, ctrl.Controls);
    else
      break;
  }
  return c;
}

Try This:

//for toolstrip
            if (ctrl is ToolStrip)
            {
                ToolStrip ts = ctrl as ToolStrip;
                foreach (ToolStripItem it in ts.Items)
                {
                    if (it is ToolStrienter code herepSeparator)
                    {
                        //-------------------------
                    }
                    else
                    {
                        //do something
                    }

                }
            }//---------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM