Solved

Converting a Control to a ToolStripButton Object

Posted on 2007-11-14
5
2,602 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

730 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