Solved

Converting a Control to a ToolStripButton Object

Posted on 2007-11-14
5
2,483 Views
Last Modified: 2013-12-17
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
Comment
Question by:purplesoup
  • 2
5 Comments
 
LVL 10

Expert Comment

by:Nash2334
ID: 20281129
If it's passing the type check you should be able to cast it fine, where does the problem occur?
0
 
LVL 10

Expert Comment

by:Nash2334
ID: 20281166
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
 

Accepted Solution

by:
purplesoup earned 0 total points
ID: 20297467
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
 
LVL 1

Expert Comment

by:Vee_Mod
ID: 20319464
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now