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 / October 2006

Tip: Looking for answers? Try searching our database.

ContextMenuStrip and submenus - virtual impossibility??? huh?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ron M. Newman - 30 Oct 2006 20:04 GMT
I have a context menu strip. I can Add elements to its "Items", but there is
nothing in there to add a sub context menu strip.

Menu
   Item 1
   Item 2
       Sub Item 1   <-- impossible to add!

How do I achieve that? There seems no API's to do that and the internet is
simply "empty" when it comes to that. All newsgroups questions are
unanswered. Does. Are we witnessing the death of the sub-menu here?

Ron
Bill B - 30 Oct 2006 20:49 GMT
Are you using the VS 2005 designer?  When you click on a menu item (at
the top level - i.e. Item 2), you should see a submenu item with "Type
Here" as the text.

If you want to do this programatically, you can via the
.MenuItems.Add() method of of the top level menu.  See the link below
for more info:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbts
kAddingContextMenusToForm.asp


Bill
Chris Dunaway - 31 Oct 2006 16:53 GMT
> I have a context menu strip. I can Add elements to its "Items", but there is
> nothing in there to add a sub context menu strip.
[quoted text clipped - 7 lines]
> simply "empty" when it comes to that. All newsgroups questions are
> unanswered. Does. Are we witnessing the death of the sub-menu here?

In order to have sub items, you need to use a ToolStripMenuItem class
to do it:

private void button1_Click(object sender, EventArgs e)
{
  ContextMenuStrip cs = new ContextMenuStrip();
  cs.Items.Add("Menu Item 1");
  cs.Items.Add("Menu Item 2");

  //This is the item we want to have sub items, so we need to create
it manually
  //so that we can add items to it later
  ToolStripMenuItem tm = new ToolStripMenuItem("Menu Item with Sub
Items");
  cs.Items.Add(tm);

  //Here we add sub items to the ToolStripMenuItem that we added
earlier
  tm.DropDownItems.Add("Sub Item 1");
  tm.DropDownItems.Add("Sub Item 2");

  ToolStripMenuItem tm1 = new ToolStripMenuItem("More Sub Items");
  tm.DropDownItems.Add(tm1);

  tm1.DropDownItems.Add("Sub Sub Item 1");
  tm1.DropDownItems.Add("Sub Sub Item 2");

  //Set the buttons ContextMenuStrip property to the context menu we
just created.
  button1.ContextMenuStrip = cs;
}

Incidentally, if you drag a ContextMenuStrip to the form, you can
design the menu visually and then look at the generated code to see how
it is done.

Chris

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.