?
Solved

Creating Dynamic Menus IN Visual Basic

Posted on 2003-03-15
10
Medium Priority
?
261 Views
Last Modified: 2013-11-25
Can anyone tell me how to make dynamic menus in VB, I mean I don't need the code...i need a hint...Thanks in Advance
0
Comment
Question by:cryptosid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 1

Expert Comment

by:hazgod
ID: 8143515
what type of thing do you want?
0
 
LVL 4

Accepted Solution

by:
jmfairchild earned 1000 total points
ID: 8143725
Use the "Load" and "Unload" methods to Add and Delete menu items "dynamically" (from your code):

Load Form1.mnuFileItem(1)

or

Unload Form1.mnuFileItem(1).

- Jim
0
 

Expert Comment

by:jpirozzolo
ID: 8143746
Hi cryptosid
Try this item from Planet Source code

http://www.planet-source-code.com/xq/ASP/txtCodeId.7566/lngWId.1/qx/vb/scripts/ShowCode.htm

It gives an idea on what needs to be done,

Hope this is of use.. Joe.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Expert Comment

by:jpirozzolo
ID: 8143751
Hi cryptosid
Try this item from Planet Source code

http://www.planet-source-code.com/xq/ASP/txtCodeId.7566/lngWId.1/qx/vb/scripts/ShowCode.htm

It gives an idea on what needs to be done,

Hope this is of use.. Joe.
0
 
LVL 3

Expert Comment

by:QJohnson
ID: 8143786
Their code is correct, but you may need the hint if you haven't worked with control arrays before.

You can create a control array in two ways: copy and existing control at design time and answer yes to the dialog from vb when you paste it.  The new control will have an index of one and the original will be zero.

Alternatively, you can set the index of any control you choose to zero.  This makes it the zeroth item of a control array and this is the strategy to use for your dynamic menus.

When you use the load statement at run time (as shown in the other responses), you'll have a new menu item with exactly the same properties as your zeroth item, but it will be invisible.  

With control arrays, the event procedures add an argument called Index to their declarations which allows you to act on events for a particular member of the array.  Typically we exploit this by using Select Case statements.  

So your menu array's click even might look like:

private sub mnuArray_Click (Index as Integer)
      Select Case Index
            Case 0    '   our design-time menu item
            Case 1    '   the first one we added
                  Do Stuff for first new menu
      End Select
end sub

I typically add all of the items at once and it is important to note that if the zeroth item may not be needed (and so it is set to invisible at design time), you need to handle it differently from the others.

So you need something like
   If intNewMenuCount > 0 then
      mnuArray(0).Visible = True    
      For i = 1 to intNewMenuCount
            mnuArray(i).Visible = True
      Next

Their fun and easy and are pretty impressive to most users.  Have a blast.
0
 
LVL 2

Expert Comment

by:Sweat
ID: 8144874
0
 
LVL 3

Expert Comment

by:QJohnson
ID: 8145109
oops... I guess that last snippet of code I offered would have been a lot more useful with the line I left out by mistake (and if we addressed one more issue)

  If intNewMenuCount > 0 then
     mnuArray(0).Visible = True    
     For i = 1 to intNewMenuCount
           Load mnuArray(i)       '    the line I left out above
           mnuArray(i).Visible = True
     Next

Of course, changing the caption so the user can tell the menu items apart is important and unless you put the text items in an array before you start, you can't really assign them in a loop like this.  So perhaps we really want to move towards something like:

   '  Read the datasource that holds the items we want to show in our dynamic
   '  menus and assume the zeroth item is invisible and has no useful caption.
   '  Call code to populate a string array of menu captions named as mastrMenuCaption().
   '  With this strategy, your code would only be executing if there were at
   '  least one of the items needed.

   '  Set caption and show the first one
      mnuArray(0).Caption = mastrMenuCaption(0)
      mnuArray(0).Visible = True
   '  Now show the rest if there are more than one
      If Ubound(mastrMenuCaption) > 0 Then
            For i = 1 To Ubound(mastrMenuCaption)
                  Load mnuArray(i)
                  mnuArray(i).Caption = mastrMenuCaption(i)
                  mnuArray(i).Visible = True
            Next    '  any more to add?
      End If   '  did we have more than one dyanamic menu item?

I hope this is more useful.
0
 
LVL 4

Expert Comment

by:jmfairchild
ID: 8145394
Depending on how "dynamic" you need the menu to be, API calls are unnecessary. Since your question said that you didn't need the VB code, just a hint, I figured you could get the rest using the Load and/or Unload methods hint. Anyway, since the others here seem to think you need the help, I'll follow suit and give you an example:

1. Start a new Standard.Exe project.
2. In the menu editor for Form1, add the first menu option as "File" (or whatever else you like). You do this in the Caption textbox. Name it (in the Name textbox) mnuFile (or mnu plus whatever). Click the NEXT button, then the indent button (right arrow above listbox where you see "File"). Make this a separator by Entering a hyphen ("-") as the caption. Name it mnuFileItem, and make it index 0. Click OK to create menu. (Usually you would have menu items above the separator (hence the REASON for a separator, but for expedience sake, we'll leave them out here).
3. Add two Command Buttons (Command1 & Command2).
4. Copy/Paste the below code into the Form1 Code Window. Press F5 to run.
5. Clicking the Add menu item button runs the code you would run to add a menu item (Could be a filename, option or whatever else you wish). Pressing the Delete menu item button, runs the code to delete the last menu item added, and so on.
6. This is a VERY BASIC example, but gets you started. You can unload items in the middle of the "list" by Unloading the correct item number. Enjoy.

<----- Start Code Here ----->

Option Explicit

Dim intTotalMenuItems As Integer

Private Sub Form_Load()

 Command1.Caption = "Add menu item"
 Command2.Caption = "Delete menu item"
 Command2.Enabled = False

 intTotalMenuItems = 2

End Sub

Private Sub Command1_Click()

 Load mnuFileItem(intTotalMenuItems)
 'This would be where you would load the menu item name
 mnuFileItem(intTotalMenuItems).Caption = "Menu Item"
 intTotalMenuItems = intTotalMenuItems + 1
 'Enable delete button
 Command2.Enabled = True

End Sub

Private Sub Command2_Click()

 If intTotalMenuItems > 2 Then
  Unload mnuFileItem(intTotalMenuItems - 1)
  intTotalMenuItems = intTotalMenuItems - 1
 Else
  'Disable delete button
  Command2.Enabled = False
 End If

End Sub

<----- End Code Here ----->

You can also make the separator visible or hidden when there are or aren't menu items to display below it as long as there is at least one "indented" menu item visible, again, another reason for menu items above the separator.

Anyway, enjoy.

- Jim
0
 
LVL 5

Author Comment

by:cryptosid
ID: 8160970
thank you all...had forgot about the 'Index' i got it...since imfairchild responded first he gets the points thanks all of you again...

cryptosid
0
 
LVL 4

Expert Comment

by:jmfairchild
ID: 8163131
Glad to be of help, thanks.

- Jim
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question