Results 1 to 4 of 4

Thread: MenuStrip [This should be easy]

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2003
    Posts
    38

    MenuStrip [This should be easy]

    Being stuck in this for 1 day... in VB6, when we cant to invoke an object method, having the name of it in a string, we could do something like:
    VB Code:
    1. Dim Description as Sring
    2.  
    3. Description = "Menu1"
    4.  
    5. FrmStart(Description).Eabled = False

    Now, on Vb.net, we cant simply do the same, as it throws an error, because the menu is inside an other object. But, if i call the Menu1.enabled, on the code, it allows me with no problem. The problem is the "Menu1" is inside a string. I think this must have an easy solution, but i've been strugling all day and no clue

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: MenuStrip [This should be easy]

    Here is one way to do it... Just loop thru all controls on the form to find the right control using control name, then disable it when found
    VB Code:
    1. 'This is for a Form
    2.         Dim controlName As String = "Button1"
    3.         For Each ctrl As Control In Me.Controls
    4.             If ctrl.Name = controlName Then
    5.                 ctrl.Enabled = False
    6.             End If
    7.         Next
    8.  
    9.  
    10.        'And this is for a MenuStrip
    11.         Dim controlName As String = "FileToolStripMenuItem"
    12.         For Each ctrl As ToolStripItem In Me.MenuStrip1.Items
    13.             If ctrl.Name = controlName Then
    14.                 ctrl.Enabled = False
    15.             End If
    16.         Next
    Last edited by stanav; Nov 9th, 2006 at 01:01 PM.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2003
    Posts
    38

    Re: MenuStrip [This should be easy]

    The problem of that code, code i have, is the slowness of it. I must cicle all the menus, or part of them, for each menu item i've. The program is working that way atm, just wanted do make ir quicker, but it seems not easy.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: MenuStrip [This should be easy]

    Instead of looping through the controls you can go directly at it using:

    VB Code:
    1. Dim c As Control = Me.Controls.Item(controlName)
    2.         c.Enabled = False

    and for the menu:

    VB Code:
    1. Try
    2.             Dim c As ToolStripItem = MenuStrip1.Items.Item(controlName)
    3.             c.Enabled = False
    4.         Catch ex As Exception
    5.             MessageBox.Show(ex.Message)
    6.         End Try
    Last edited by Atheist; Nov 9th, 2006 at 01:21 PM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width