Solved

Disabling Menu in a menustrip + vb.net 2008

Posted on 2012-03-12
7
1,943 Views
Last Modified: 2012-06-21
Hi Experts,

I have menu name names stored in a SQL server table. I need to fetch this menus, loop through them and disable on the main form. I have been unable to to do so. Please assist.
 Private Sub Disablemenus()
        Dim cnn As New SqlConnection(My.Settings.mycString)
        Dim cmd As New SqlCommand
        Dim dr2 As SqlDataReader
        ' Dim cmenuname As ToolStripMenuItem
        cnn.Open()
        cmd.Connection = cnn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "select MenuName from table1 where Allowed=0)"
        dr2 = cmd.ExecuteReader
        Do While dr2.Read = True
            'disable the menu
        Loop

    End Sub

Open in new window

0
Comment
Question by:Peter Kiprop
  • 4
  • 3
7 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 37709183
You probably only have the menu-name in sql-server

Do While dr2.Read = True

    Dim menuStrip As Contexmenu= DirectCast(Form1.Controls.Find(dr2.items["MenuName"]
, True)(0), Contexmenu)
     menuStrip.Enable=True
loop

or

Do While dr2.Read = True
            DisableMenu dr2.items["MenuName"]
Loop



private function DisableMenu(dim menuStrip as string)
        For Each cntrl As Control In Me.components.Components
            If cntrl.Name = menuStrip Then
                'do what ever you need to do
                Exit For
            End If
        Next
end function
0
 
LVL 6

Author Comment

by:Peter Kiprop
ID: 37709277
Hi Dhaest,

None of the above is working. any another suggestion?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 37709303
And what is not working ? Do you get an error ? How is your menu added to your form ?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 6

Author Comment

by:Peter Kiprop
ID: 37709361
value type form.control cannot be converted to form.contextmenu
0
 
LVL 6

Author Comment

by:Peter Kiprop
ID: 37709447
Hi Dhaest,

i am trying to disable the sub menus and not the main menus. The menus were added manually on the main form using a menustrip control.

Thanks,
Pthepebble.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 37709802
Did you add a name to each menuitem (below example is based on the text of the subitem)

    Private Sub DisableMenuItem(menuItem As String)
        For Each mnu As ToolStripMenuItem In MenuStrip1.Items
            For Each submnu As ToolStripMenuItem In mnu.DropDownItems
                If (submnu.Text = menuItem) Then
                    submnu.Enabled = False
                End If
            Next
        Next
    End Sub
0
 
LVL 6

Author Closing Comment

by:Peter Kiprop
ID: 37709881
Exactly what i needed. Many thanks
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question