• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 463
  • Last Modified:

vb .net Add a new menu item dynamically

In VB .Net 2005,

How can I dynamically setup a new menu item in the code?

I want to add a new item in the drop down menus, whenever a certain event happens in the program.  I've tried to look through the objects myself, and I've searched around, I just can't seem to get anything clear...


Shahzad
0
shahzadq
Asked:
shahzadq
  • 5
  • 3
  • 3
1 Solution
 
Bob LearnedCommented:
1) What type of menu are you looking for?  MenuStrip?  MainMenu?  ContextMenu?  ContextMenuStrip?

2) What menu items are you adding?

Bob
0
 
shahzadqAuthor Commented:
I don't need a top level menu, I want a menu item on a drop down from the top.  Next to File, I have Chat, underneath I want Window (As in Chat->Window).  For each new form I instantiate, I want a Window 1, and window 2, and window 3 to appear in the menu that can pull up each form.

Any ideas how to pull that off?

I'm tinkering around with it right now, so many options, and so many errors...


Shahzad
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
In VB.Net 2005, the standard Menu is the MenuStrip.  When you add things across the top, they are ToolStripMenuItems.  If you have "Window" under "Chat", then you actually have a control on your form called "WindowToolStripMenuItem" (and one called "ChatToolStripMenuItem" as well).

Use code like below to add items to the "Window" menu and to add an EventHandler for the Click event.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tsi As ToolStripItem = WindowToolStripMenuItem.DropDownItems.Add("Window1")
        AddHandler tsi.Click, AddressOf Me.ToolStripItem_Click
    End Sub

    Private Sub ToolStripItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim tsi As ToolStripItem = CType(sender, ToolStripItem)
        MsgBox(tsi.Text)
 
        ' You can use "tsi.Text" in a Select Case statement here,
        ' or possibly use "tsi.Text" as the key to look something up in a HashTable...

    End Sub

End Class
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
shahzadqAuthor Commented:
What namespace does WindowToolStripMenuItem come from?

(Sorry for the dumb question)
0
 
shahzadqAuthor Commented:
It doesn't seem to give me the "DropDownItems" option
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's actually ToolStripMenuItem from:

    System.Windows.Forms.ToolStripItem

If you have named the entry under "Chat", "Window", then the control ON THE FORM is called "WindowToolStripMenuItem".

You are probaly using the old .Net 1.1 menu system based on the MainMenu control.

In the IDE, look at your form with all the controls on it.  Down BELOW the form you should see a placeholder for your Menu system.

+---------------------------------+
| Form1                                |
+---------------------------------+
|                                          |
|                                          |
|                                          |
|                                          |
+---------------------------------+

+--------------+      +--------------+
| MenuStrip1 |      | MainMenu1 |
+--------------+      +--------------+

Which do you see?   "MenuStrip1" or "MainMenu1"

0
 
Bob LearnedCommented:
Hey, Mikey, I see this comment:

>>I don't need a top level menu

That speaks of a ContextMenuStrip.

Bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I interpreted this line:

    "I don't need a top level menu, I want a menu item on a drop down from the top."

As, I already have top level menus defined at design-time, and I want to add menu items below those existings.

His next statement supports that with:

    "Next to File, I have Chat, underneath I want Window (As in Chat->Window)."

Finally, I interpret "Swan Lake Ballet" as "graceful but boring dancers that make loud noises on the floor when they leap about".

But I could be wrong...     =)
0
 
Bob LearnedCommented:
I agree with the Swan Lake assessment...  Now "The Nutcracker" is a different story ;)

If this is an MDI form, with sub-forms, you can specify MdiList for the menu item.

Bob
0
 
shahzadqAuthor Commented:
+---------------------------------+
| Form1                                |
+---------------------------------+
|                                          |
|                                          |
|                                          |
|                                          |
+---------------------------------+

+--------------+      +--------------+
| MenuStrip1 |      | MainMenu1 |
+--------------+      +--------------+

It says MenuStrip1
I'm using .net 2.0 with VS 2005

Let me try what you said and see what happens...

Shahzad
0
 
shahzadqAuthor Commented:
Thanks everyone...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now