Toolbar and Windows Forms

  • Thread starter Thread starter Alfredo Barrientos
  • Start date Start date
A

Alfredo Barrientos

Hi,

I have a little trouble trying to assign a Toolbar control to another
toolbar variable control.

I am getting my forms controls with this:

for (int j = 0; j <= frmChild.Controls.Count - 1; j++)
{
// Some operations

if (ctrl.GetType().ToString() ==
"System.Windows.Forms.ToolBar")
{
MessageBox.Show("paso");
System.Windows.Forms.ToolBar toolBarX = ctrl;
}
}

I just want to access to the "buttons" property of my toolbar control,
in order to enabled or disabled toolbar buttons getting their tag
names.

Is it possible?

Thanks,

Alfredo Barrientos
 
I’m guessing that you are getting a compile error, something to the effect of:

Cannot implicitly convert type 'System.Windows.Forms.Control' to
'System.Windows.Forms.ToolBar'?

To fix that, you need to cast the reference you’ve got to your toolbar to an
actual toolbar... to do so, we’d change your line from:


System.Windows.Forms.ToolBar toolBarX = ctrl;

to

System.Windows.Forms.ToolBar toolBarX = (System.Windows.Forms.ToolBar)ctrl;

Note: You can drop the ‘System.Windows.Forms’ part if you’ve already got
that namespace being used.

Also, a couple of other quick thoughts... rather than compare each control
based on the types name... you could simply interrogate the object to
determine if it is the desired type ala:

if (ctrl is System.Windows.Forms.ToolBar)

Brendan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top