Dynamic MenuStrip Question


D

Dave Coate

Hi

With the following code, I can create a MenuStrip with one item, "Admins".
When the user clicks on Admins, it dynamically adds items to the menu
Admins. (In this case "Dave" And "Elizabeth")

When I run this form, the first time I click on "Admins", nothing happens.
Everytime I click on Admins after that I get the expected behavior.

Can someone tell me what I can do so that the menu item works properly the
first time?

Thanks
Dave Coate

Code:

Private MainMenu As MenuStrip

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

MainMenu = New MenuStrip

'Add menu items to the Main Menu Strip
'The event handlers will populate each menu when the top level
'menu item is clicked
Me.MainMenu.Items.Add("Admins", Nothing, _
New System.EventHandler(AddressOf AdminsMenu_OnClick))

'Add the main menu to the controls collection of the form
Me.Controls.Add(MainMenu)

End Sub

Private Sub AdminsMenu_OnClick(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim cms As New ContextMenuStrip()
Dim Admins() As String = {"Dave", "Elizabeth"}

For Each Admin As String In Admins
cms.Items.Add(Admin, Nothing, _
New System.EventHandler(AddressOf
SelectedAdminMenu_OnClick))
Next

Dim tsi As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
tsi.DropDown = cms
End Sub

Private Sub SelectedAdminMenu_OnClick(ByVal sender As Object, ByVal e As
System.EventArgs)

End Sub
 
Ad

Advertisements

M

mangist

Hi

With the following code, I can create a MenuStrip with one item, "Admins".
When the user clicks on Admins, it dynamically adds items to the menu
Admins. (In this case "Dave" And "Elizabeth")

When I run this form, the first time I click on "Admins", nothing happens.
Everytime I click on Admins after that I get the expected behavior.

Can someone tell me what I can do so that the menu item works properly the
first time?

Thanks
Dave Coate

Code:

Private MainMenu As MenuStrip

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

MainMenu = New MenuStrip

'Add menu items to the Main Menu Strip
'The event handlers will populate each menu when the top level
'menu item is clicked
Me.MainMenu.Items.Add("Admins", Nothing, _
New System.EventHandler(AddressOf AdminsMenu_OnClick))

'Add the main menu to the controls collection of the form
Me.Controls.Add(MainMenu)

End Sub

Private Sub AdminsMenu_OnClick(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim cms As New ContextMenuStrip()
Dim Admins() As String = {"Dave", "Elizabeth"}

For Each Admin As String In Admins
cms.Items.Add(Admin, Nothing, _
New System.EventHandler(AddressOf
Selected7AdminMenu_OnClick))
Next

Dim tsi As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
tsi.DropDown = cms
End Sub

Private Sub SelectedAdminMenu_OnClick(ByVal sender As Object, ByVal e As
System.EventArgs)

End Sub



private ToolStripMenuItem adminMenuItem;

void CreateMenu() {
// Create your menu and Admin menuItem here

// Keep a reference to the Admin menu you added
adminMenuItem = MainMenu.Items.Add ("admin");
}


// Handle the OnClick event of adminMenuItem
MenuItem_OnClick() {

adminMenuItem.DownItems.Add ("Dave");
adminMenuItem.DropDownItems.Add ("Elizabeth");

adminMenuItem.ShowDropDown();

}

Hope this helps (in C# sorry, my VB isn't up to scratch)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top