Create MenuItems from an Access Database

weismana81

Member
Joined
Mar 8, 2005
Messages
10
Programming Experience
Beginner
Hello all,

I'm trying to load menu items from an access database, I guess similar to how internet explorer loads your favorites onto the menu (but from a database instead of a directory).

I'm not reall sure how to get started with this. I figure I will need to create the menu items while looping through the database rows. I still don't completely understand how to create controls with a loop and what not. Anyway, if someone could point me in the right direction, or possibly provide some sample code, it would be very much appreciated. Even if someone could point me to a good tutorial about creating controls in code and with loops, it would be a great help.

Thanks!!!!
 
The first thing you need to do is write a procedure that will handle the Click event for your menu items. It must match the signature of a MenuItem.Click event handler. You will almost certainly want to write a single procedure to handle all menu items, so it must be able to determine which menu item was selected and what to do about it. Assuming that each item has a URL as its text, you would need something like this:
VB.NET:
[color=Blue]Private Sub[/color] OpenFavouritesItem([color=Blue]ByVal[/color] sender [color=Blue]As[/color] System.Object, [color=Blue]ByVal[/color] e [color=Blue]As[/color] System.EventArgs)
  Process.Start([color=Blue]DirectCast[/color](sender, MenuItem).Text)
[color=Blue] End Sub[/color]
Let's say that the text of the menu item is a friendly name. Assuming that the friendly name is the primary key of your DataTable and the actual address is in a column called URL, your procedure would look like this:
VB.NET:
[color=Blue]Private Sub[/color] OpenFavouritesItem([color=Blue]ByVal[/color] sender [color=Blue]As[/color] System.Object, [color=Blue]ByVal[/color] e [color=Blue]As[/color] System.EventArgs)
  Process.Start([color=Blue]Me[/color].favouritesTable.Rows.Find([color=Blue]DirectCast[/color](sender, MenuItem).Text)("URL"))
[color=Blue] End Sub[/color]
Now to add the menu items, you should already have added a MainMenu to your form and a top-level MenuItem to act as parent. Maintaining the "favourites" example, assuming you have already retrieved the data from the database into a table named favouritesTable, you would add the menu items at run-time something like this:
VB.NET:
[color=Blue]For Each[/color] favouritesRow [color=Blue]As[/color] DataRow [color=Blue]In[/color] favouritesTable.Rows
[color=Green]   'Add a new menu item to the desired parent with text from the Name column and the desired Click event handler.[/color]
[color=Blue]   Me[/color].mainFavouritesMenuItem.MenuItems.Add(favouritesRow("Name"), [color=Blue]New[/color] EventHandler([color=Blue]AddressOf Me[/color].OpenFavouritesItem))
[color=Blue] Next[/color]
 
Back
Top