Link to home
Start Free TrialLog in
Avatar of kolibree53
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
SOLUTION
Avatar of geodan7
geodan7
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SORRY, i meant to say:

menu.Enabled = false;
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kolibree53
kolibree53

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(TextResult) Then
                        If PCUserRights(LoggedPCUserRight) = 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(TextResult) Then
                                If PCUserRights(LoggedPCUserRight) = 1 Then
                                    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?
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(TextResult) Then
                        If PCUserRights(LoggedPCUserRight) = 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(TextResult) Then
                                If PCUserRights(LoggedPCUserRight) = 1 Then
                                    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;
        }
      }
    }

That code was C#, but they have the same objects...just change the syntax.
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("FileMenu", True)
                If tsi.Length = 1 Then
                    If PCUserRights(LoggedPCUserRight) = 1 Then
                        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
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.


haha, i love you too. Those points are so mighty precious.