kolibree53
asked on
Disable ToolStripMenuItem within MenuStrip in VS2005
How can I disable some ToolStripMenuItems within a MenuStrip of VS2005?
The following approach gives me only the Headlines of each Menu.
Dim menu As ToolStripMenuItem
For Each menu In MenuStrip.Items
Next
Thanks
The following approach gives me only the Headlines of each Menu.
Dim menu As ToolStripMenuItem
For Each menu In MenuStrip.Items
Next
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I need to keep the solution a little more flexible then direct addressing of my menuitems.
I have a DB table with the names of my Menus and a User level. After a User is logging in I verify his User level and go through the table with the Menuitem names and enable/disable the menuitems accordingly.
The following worked for me in VS2003 and I need a solution for VS2005.
menu was for the Menu Headlines and m_itm for the DropDownItems.
Dim menu As MenuItem
For Each menu In Me.MainMenu1.MenuItems
If menu.Text.Equals(TextResul t) Then
If PCUserRights(LoggedPCUserR ight) = 1 Then
menu.Enabled = True
Else
menu.Enabled = False
End If
Else
Dim m_itm As MenuItem
For Each m_itm In menu.MenuItems
te = m_itm.Text
If m_itm.Text.Equals(TextResu lt) Then
If PCUserRights(LoggedPCUserR ight) = 1 Then
m_itm.Enabled = True
Else
m_itm.Enabled = False
End If
End If
Next
End If
Next
I have a DB table with the names of my Menus and a User level. After a User is logging in I verify his User level and go through the table with the Menuitem names and enable/disable the menuitems accordingly.
The following worked for me in VS2003 and I need a solution for VS2005.
menu was for the Menu Headlines and m_itm for the DropDownItems.
Dim menu As MenuItem
For Each menu In Me.MainMenu1.MenuItems
If menu.Text.Equals(TextResul
If PCUserRights(LoggedPCUserR
menu.Enabled = True
Else
menu.Enabled = False
End If
Else
Dim m_itm As MenuItem
For Each m_itm In menu.MenuItems
te = m_itm.Text
If m_itm.Text.Equals(TextResu
If PCUserRights(LoggedPCUserR
m_itm.Enabled = True
Else
m_itm.Enabled = False
End If
End If
Next
End If
Next
Why can't you use the same approach, if it worked for you in VS 2003?
Anyway, you could just create a class and derive from the MenuItem class, and add a property in your class that holds some value(s) which represent the user groups that are allowed to use the menu item. ...then enable disable accordingly.
If that doesn't help...what exactly needs to change from your old solution in VS 2003?
Anyway, you could just create a class and derive from the MenuItem class, and add a property in your class that holds some value(s) which represent the user groups that are allowed to use the menu item. ...then enable disable accordingly.
If that doesn't help...what exactly needs to change from your old solution in VS 2003?
ASKER
My problem is that I just can't find a way to get to the DropDownItems.
The VS2005 version below throws an exception at :
For Each m_itm In menu.DropDownItems
because of a cast error from ToolStripSeperator to ToolStripMenuItem.
I hoped there is another way of addressing the ToolStripMenuitems with the find method but I didn't get it to work (me.menustrip.items("Print ",True)).
Instead of "Print" I could use a variable filled from my table.
VS2005 version:
Dim menu As ToolStripMenuItem
For Each menu In MenuStrip.Items
te = menu.Text
If menu.Text.Equals(TextResul t) Then
If PCUserRights(LoggedPCUserR ight) = 1 Then
menu.Enabled = True
Else
menu.Enabled = False
End If
Else
Dim m_itm As ToolStripMenuItem
For Each m_itm In menu.DropDownItems
te = m_itm.Text
If m_itm.Text.Equals(TextResu lt) Then
If PCUserRights(LoggedPCUserR ight) = 1 Then
m_itm.Enabled = True
Else
m_itm.Enabled = False
End If
End If
Next
End If
Next
The VS2005 version below throws an exception at :
For Each m_itm In menu.DropDownItems
because of a cast error from ToolStripSeperator to ToolStripMenuItem.
I hoped there is another way of addressing the ToolStripMenuitems with the find method but I didn't get it to work (me.menustrip.items("Print
Instead of "Print" I could use a variable filled from my table.
VS2005 version:
Dim menu As ToolStripMenuItem
For Each menu In MenuStrip.Items
te = menu.Text
If menu.Text.Equals(TextResul
If PCUserRights(LoggedPCUserR
menu.Enabled = True
Else
menu.Enabled = False
End If
Else
Dim m_itm As ToolStripMenuItem
For Each m_itm In menu.DropDownItems
te = m_itm.Text
If m_itm.Text.Equals(TextResu
If PCUserRights(LoggedPCUserR
m_itm.Enabled = True
Else
m_itm.Enabled = False
End If
End If
Next
End If
Next
Instead, loop through the items as ToolStripItems, and then check to see if the item is a ToolStripSeparator, or a ToolStripMenuItem...some sample code is below.
foreach (ToolStripItem tsItem in tsItems)
{
if (tsItem.Available)
{
if (tsItem is ToolStripSeparator)
{
// hide all separators
tsItem.Visible = false;
// only set in a separator that has a chance to be shown later
// if no items have been displayed, no chance
if (itemDisplayed) lastSeparator = tsItem;
}
else if (lastSeparator != null)
{
// show it when valid
lastSeparator.Visible = true;
}
else
{
itemDisplayed = true;
}
}
}
foreach (ToolStripItem tsItem in tsItems)
{
if (tsItem.Available)
{
if (tsItem is ToolStripSeparator)
{
// hide all separators
tsItem.Visible = false;
// only set in a separator that has a chance to be shown later
// if no items have been displayed, no chance
if (itemDisplayed) lastSeparator = tsItem;
}
else if (lastSeparator != null)
{
// show it when valid
lastSeparator.Visible = true;
}
else
{
itemDisplayed = true;
}
}
}
That code was C#, but they have the same objects...just change the syntax.
ASKER
I got the find method to work.
That seems to be the right way because it searches for the control name in the whole menustrip with one line of code.
Dim tsi() As ToolStripItem = Me.MenuStrip.Items.Find("F ileMenu", True)
If tsi.Length = 1 Then
If PCUserRights(LoggedPCUserR ight) = 1 Then
tsi(0).Enabled = True
Else
tsi(0).Enabled = False
End If
End If
Thanks for your help.
That seems to be the right way because it searches for the control name in the whole menustrip with one line of code.
Dim tsi() As ToolStripItem = Me.MenuStrip.Items.Find("F
If tsi.Length = 1 Then
If PCUserRights(LoggedPCUserR
tsi(0).Enabled = True
Else
tsi(0).Enabled = False
End If
End If
Thanks for your help.
thx for nuthin....i mean...if we knew you just wanted to Find the menu item by name...we would have come up with the same option....i hate wasting my time to help point nazis
ASKER
Watch the language geodan7!
Learn to read! I did ask for a solution using the Find method earlier.
"I hoped there is another way of addressing the ToolStripMenuitems with the find method but I didn't get it to work (me.menustrip.items("Print ",True)).
Instead of "Print" I could use a variable filled from my table."
So if you want to offend anybody, do it to your second self since you like to address yourelf as we. LOL.
Learn to read! I did ask for a solution using the Find method earlier.
"I hoped there is another way of addressing the ToolStripMenuitems with the find method but I didn't get it to work (me.menustrip.items("Print
Instead of "Print" I could use a variable filled from my table."
So if you want to offend anybody, do it to your second self since you like to address yourelf as we. LOL.
haha, i love you too. Those points are so mighty precious.
menu.Enabled = false;