purplesoup
asked on
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.Cont rol' to 'System.Windows.Forms.Tool StripButto n'
Here is the code
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(System.Windows.Form s.ToolStri pButton))
{
ToolStripButton t = (System.Windows.Forms.Tool StripButto n)c;
// ToolStripButton t = c as System.Windows.Forms.ToolS tripButton ;
SetToolstripButton(t);
}
}
How can I do this?
Error 2 Cannot convert type 'System.Windows.Forms.Cont
Here is the code
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(System.Windows.Form
{
ToolStripButton t = (System.Windows.Forms.Tool
// ToolStripButton t = c as System.Windows.Forms.ToolS
SetToolstripButton(t);
}
}
How can I do this?
If it's passing the type check you should be able to cast it fine, where does the problem occur?
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(Contro l.ControlC ollection controlCollection)
{
foreach (Control control in controlCollection)
{
if (control.Controls.Count > 0)
{
FindToolStripButton(contro l.Controls );
}
else
{
if (control.GetType().Equals( typeof(Too lStripButt on))
{
ToolStripButton t = control as ToolStripButton;
SetToolStripButton(t);
}
}
private void FindToolStripButton(Contro
{
foreach (Control control in controlCollection)
{
if (control.Controls.Count > 0)
{
FindToolStripButton(contro
}
else
{
if (control.GetType().Equals(
{
ToolStripButton t = control as ToolStripButton;
SetToolStripButton(t);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
Vee_Mod
Community Support Moderator