Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

Dynamic MenuStrip Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave Coate - 22 Feb 2007 20:53 GMT
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
mangist@gmail.com - 23 Feb 2007 03:57 GMT
On Feb 22, 3:53 pm, "Dave Coate"
<David.CoateNoS...@alteontrainingNoSpam.com> wrote:
> Hi
>
[quoted text clipped - 50 lines]
>
>     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)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.