Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Control to specific object

Status
Not open for further replies.

jpreciado

MIS
Feb 7, 2004
36
MX
I'm trying to get all Controls in a form.
If it is a specific type of Control, I need to convert it to that type so I can change some properties.
thanks in advanced.

here is the code:
public class Enable_Disable
{
public void ChangeProperties(Form myform)
{
foreach (Control c in myform.Controls)
{
if (c.GetType()==System.Windows.Forms.MenuStrip)
{
// I have the error Message at this Line
System.Windows.Forms.MenuStrip m = c;
}
else
c.Enabled = false;
}
}
}
 
You need to cast the object like this:

MenuStip m = (MenuStrip)c;

or simply use

((MenuStrip)c).Someproperty = whatever;
 
JurkMonkey's code is great, but if c is not a MenuStrip the cast will throw an exception. which may be what you want.

otherwise:

Code:
MenuStrip m = c as MenuStrip;
if ( m != null )
{
 m.SomeProperty = whatever;
}


mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top