How to convert a Control into a ToolStripButton
-
Hello, I have a routine that loops through all the controls on my form. The purpose is to set the Edit Mode or Read Only mode. To find textboxes and buttons inside a TabPage or GroupBox the routine runs recursive. To convert a Windows.Forms.Control in to a Texbox I validate and use .. if (c is TextBox) { ToggleTextBox((c as TextBox), flgEditMode); } So Far So Good. If I do the same for a ToolStripButton I get the message: Cannot convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.ToolStripButton' Please can you help me with a work around? Kind regards Arjen
-
Hello, I have a routine that loops through all the controls on my form. The purpose is to set the Edit Mode or Read Only mode. To find textboxes and buttons inside a TabPage or GroupBox the routine runs recursive. To convert a Windows.Forms.Control in to a Texbox I validate and use .. if (c is TextBox) { ToggleTextBox((c as TextBox), flgEditMode); } So Far So Good. If I do the same for a ToolStripButton I get the message: Cannot convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.ToolStripButton' Please can you help me with a work around? Kind regards Arjen
Hello, The
ToolStripButton
class doesn't Inherit fromControl
. Inheritance Hierarchy: System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.ToolStripItem System.Windows.Forms.ToolStripButton So you have to look for theToolStrip
class (which inherits fromControl
) an than iterate over theItems
property. Like this:foreach (Control c in this.Controls)
{
if (c is ToolStrip)
{
ToolStrip ts = c as ToolStrip;
foreach (ToolStripItem tsi in ts.Items)
{
ToolStripButton tsb = tsi as ToolStripButton;
if(tsb != null)
{
...
}
}
}
}Hope it helps!
All the best, Martin
-
Hello, The
ToolStripButton
class doesn't Inherit fromControl
. Inheritance Hierarchy: System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.ToolStripItem System.Windows.Forms.ToolStripButton So you have to look for theToolStrip
class (which inherits fromControl
) an than iterate over theItems
property. Like this:foreach (Control c in this.Controls)
{
if (c is ToolStrip)
{
ToolStrip ts = c as ToolStrip;
foreach (ToolStripItem tsi in ts.Items)
{
ToolStripButton tsb = tsi as ToolStripButton;
if(tsb != null)
{
...
}
}
}
}Hope it helps!
All the best, Martin
Dear Martin, Thank you for your quick reply and solution! With kind regards, Arjen
-
Dear Martin, Thank you for your quick reply and solution! With kind regards, Arjen