Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Creating Dynamic Menus IN Visual Basic

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
cryptosid
Asked:
cryptosid
  • 3
  • 2
  • 2
  • +3
1 Solution
 
hazgodCommented:
what type of thing do you want?
0
 
jmfairchildCommented:
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
 
jpirozzoloCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jpirozzoloCommented:
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
 
QJohnsonCommented:
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
 
SweatCommented:
0
 
QJohnsonCommented:
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
 
jmfairchildCommented:
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
 
cryptosidAuthor Commented:
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
 
jmfairchildCommented:
Glad to be of help, thanks.

- Jim
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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