using the string variable?

kasimacsys

Member
Joined
Jul 8, 2009
Messages
10
Programming Experience
1-3
hi,

i need to call mdi form toolstripmenuitem name using the string variable.
for ex,
mdi form is Form1.
toolstripmenuitem is customer master
if i do like that "Form1.customer master.Enabled=True" its working

but my issue is i stored customer master in a string variable like
mystring="customer master"
i need to enabled using the string variable like
Form1.mystring.enabled=True

please letme know if any one knows
 

Robert_Zenz

Well-known member
Joined
Jun 3, 2008
Messages
503
Location
Vienna, Austria
Programming Experience
3-5
Hello.

Have a look at Reflection, it's one way to take this one...the other would be to use the MenuStrip.Items(String) Property which takes the name of the ToolStripMenuItem.

Bobby
 

kasimacsys

Member
Joined
Jul 8, 2009
Messages
10
Programming Experience
1-3
hello robert,

i tried as u told like

"Dim menuname As String = "Menu_106"
Form1.MenuStrip1.Items(menuname).Enabled = True"

but it shows the error like

"object reference not set to an instance of an object"

what should i do for that?

thanks
 

Robert_Zenz

Well-known member
Joined
Jun 3, 2008
Messages
503
Location
Vienna, Austria
Programming Experience
3-5
You're sure you named it that way? Remember that you have to use the Name-Property, not the Text-Property. Or is it a sub-item of another menu-item?

Though, you can also add a Breakpoint at that Form1.MenuStrip1-Line and have a closer look at the Items-Collection, to determine what objects are in there.

Bobby
 

kasimacsys

Member
Joined
Jul 8, 2009
Messages
10
Programming Experience
1-3
Reg toolstripmenu

Hi,

here is the procedure

MDI(Form1)-->File (under the file menu)
||
||
Config (i assigned "config" name property is menu_101 (not text property) to String variable)
Quit
......
.......
Dim str as string="menu_101"
if i use Form1.menu_101.enabled="True" (its working)

but i neet to use the string variable like Form1.(string variable).enabled="True"

hope u understand my requirement

thanks
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
MenuStrip1.Items doesn't have a 'config' item, it has a 'file' item. The 'file' item has a 'config' item in its DropDownItems collection.
VB.NET:
FileToolStripMenuItem.DropDownItems("ConfigToolStripMenuItem").Text = "changed"
 

kasimacsys

Member
Joined
Jul 8, 2009
Messages
10
Programming Experience
1-3
Dear John,

i done what u say, but it shows "object not set to an instance of an object"

my code is

Dim menuname As String = "Config"
Form1.FileToolStripMenuItem.DropDownItems("menuname").Enabled = True

thanks
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Ok i didn't read it from beginning but according your last reply you are quoting string variable and that's why it won't work ... probably there is no sucha menu item called menuname. Rather you should refer the Config e.g.

Form1.FileToolStripMenuItem.DropDownItems("Config").Enabled = True
 

kasimacsys

Member
Joined
Jul 8, 2009
Messages
10
Programming Experience
1-3
Dear Jhon,

I struggled again,

when i work under the file menu its ok.
now am moving to next menu like 'Edit'.

i can use like below..
Form1.EditToolStripMenuItem.DropDownItems("Paste" ).Enabled = True

but at runtime i dont know which toolstipmenuitem am working..
so what can i do for this time?

thanks
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
ToolStripItemCollection does have a Find method, which also has a parameter to include child collections in search. You can use this method on any of the item collections, including the root Items collection of the MenuStrip itself.
ToolStripItemCollection.Find Method (System.Windows.Forms)
 
Top Bottom