?
Solved

Converting a Control to a ToolStripButton Object

Posted on 2007-11-14
5
Medium Priority
?
2,714 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 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