• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2870
  • Last Modified:

Converting a Control to a ToolStripButton Object

I have a reference to a control that I know is of type ToolStripButton, but when I try to cast it to an object of type ToolStripButton I get a compiler error

Error      2      Cannot convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.ToolStripButton'      

Here is the code

    foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(System.Windows.Forms.ToolStripButton))        
                {
                    ToolStripButton t = (System.Windows.Forms.ToolStripButton)c;
                    // ToolStripButton t = c as System.Windows.Forms.ToolStripButton;
                    SetToolstripButton(t);
                }                
            }
       
How can I do this?
0
purplesoup
Asked:
purplesoup
  • 2
1 Solution
 
Nash2334Commented:
If it's passing the type check you should be able to cast it fine, where does the problem occur?
0
 
Nash2334Commented:
Note that you may need to recurse through the controls if you are calling this method from your base form; the form's control references are only to its direct children.

private void FindToolStripButton(Control.ControlCollection controlCollection)
        {
            foreach (Control control in controlCollection)
            {
                if (control.Controls.Count > 0)
                {
                        FindToolStripButton(control.Controls);
                }
                else
                {
                         if (control.GetType().Equals(typeof(ToolStripButton))
                         {
                                 ToolStripButton t = control as ToolStripButton;
                                 SetToolStripButton(t);
                         }
                }
0
 
purplesoupAuthor Commented:
The real cause of the problem was because ToolStripButtons don't appear in the list of controls even if you recurse through them as above, instead you have to look at the Items collection of stackStrip controls - which do appear in the controls collection.

            foreach (ToolStripItem t in this.stackStrip.Items)
            {
                if (t.GetType() == typeof(ToolStripButton))
                {
                    SetToolstripButton((ToolStripButton) t);
                }
            }

I guess this was why the cast wasn't working - because the ToolStripButton object isn't derived from the control class.
0
 
Vee_ModCommented:
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now