Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically generate submenu items

Status
Not open for further replies.

Amesville

Programmer
Oct 10, 2011
93
0
0
US
Greetings!

I am working on a program that will allow a user to set up scenarios for different companies. The basic company data (Name, ID #) are stored in a DB table.

I have a menu item under the File menu that says "Select Company", and I'd like to create submenu entries for each of the companies in the table under that. When they select one, I'll store the Company ID and use it to differentiate the query data for the company selected.

I can easily get the data into a DataTable object as I'm loading up the main form, but I'm not sure how to use it to build the submenu items. Anyone have an example I can follow?

Oh and in case it matters I'm using VB.NET 2008.
Thanks

Craig
 
Code:
Dim mi As ToolStripMenuItem = New ToolStripMenuItem(ServicesName)
AddHandler mi.Click, AddressOf ServicesTypeToolStripMenuItem_Click
ServiceTypesToolStripMenuItem.DropDownItems.Add(mi)

The above is a snippet from a Services Management program that I have written.

Line 1 creates a new TolStripMenuItem (mi) and gives it a Caption (ServicesName) (Which is a string passed in to this procedure)
*****Line 2 sets up an OnClick handler for the menu item
Line 3 adds this menu item to the DropDownItems list of the appropriate parent menu.

*****The easiest way to do this is to create an OnClick handler for an existing menu item, change all the specific names and remove the Handles Clause. This will leave you with a generic handler for a menu item. In my code there is no menu item "ServicesTypeToolStripMenuItem", however its name is descriptive and it has all the correct parameters.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top