Click to See Complete Forum and Search --> : dynamic menu and controls


szd
May 22nd, 2001, 12:57 PM
HI,
I am a beginner in VB. I am trying to create controls on a form dynamically durng run time. Does anyone have some sample code I can look at or suggestions for websites that use this technology? Is it possible to create a menu dynamically on run time? Please post sample code The data to create the treeview comes from a database and can differ from user to user.
Thanks in advance.

clarghi
May 22nd, 2001, 01:57 PM
Go to http://www.it-lang-vb.net/ and search "crtmenu" (search for file name), I think it's what you search.

Cristiano Larghi

szd
May 22nd, 2001, 02:08 PM
this site is in spanish. I need the code to be in english
Thanks.

clarghi
May 23rd, 2001, 02:13 AM
It's an italian site, but the code is universal...

John G Duffy
May 23rd, 2001, 09:24 AM
Http://www.Planet-Source-Code.com/vb has a whole lot of examples on how to create controls and menus at Run time and other things. Go there and Browse to your hearts content.

John G

ljprivara
June 22nd, 2001, 10:29 AM
http://www.geocities.com/CapeCanaveral/6740/allfiles.htm

Subject: Shows how to add and remove MRU list (i.e., example of creating a dynamic menu at run-time)

download MRU-ADD.ZIP file and review code.

Iouri
June 22nd, 2001, 01:25 PM
'Class cPopupMenu
'------------------
Private Type POINT
x As Long
y As Long
End Type
'
Private Const MF_ENABLED = &H0&
Private Const MF_SEPARATOR = &H800&
Private Const MF_STRING = &H0&
Private Const TPM_RIGHTBUTTON = &H2&
Private Const TPM_LEFTALIGN = &H0&
Private Const TPM_NONOTIFY = &H80&
Private Const TPM_RETURNCMD = &H100&
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal sCaption As String) As Long
Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, nIgnored As Long) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
'
Public Function Popup(ParamArray param()) As Long
Dim iMenu As Long
Dim hMenu As Long
Dim nMenus As Long
Dim p As POINT

' get the current cursor pos in screen coordinates
GetCursorPos p

' create an empty popup menu
hMenu = CreatePopupMenu()

' determine # of strings in paramarray
nMenus = 1 + UBound(param)

' put each string in the menu
For iMenu = 1 To nMenus
' the AppendMenu function has been superseeded by the InsertMenuItem
' function, but it is a bit easier to use.
If Trim$(CStr(param(iMenu - 1))) = "-" Then
' if the parameter is a single dash, a separator is drawn
AppendMenu hMenu, MF_SEPARATOR, iMenu, ""
Else
AppendMenu hMenu, MF_STRING + MF_ENABLED, iMenu, CStr(param(iMenu - 1))
End If
Next iMenu

' show the menu at the current cursor location;
' the flags make the menu aligned to the right (!); enable the right button to select
' an item; prohibit the menu from sending messages and make it return the index of
' the selected item.
' the TrackPopupMenu function returns when the user selected a menu item or cancelled
' the window handle used here may be any window handle from your application
' the return value is the (1-based) index of the menu item or 0 in case of cancelling
iMenu = TrackPopupMenu(hMenu, TPM_RIGHTBUTTON + TPM_LEFTALIGN + TPM_NONOTIFY + TPM_RETURNCMD, p.x, p.y, 0, GetForegroundWindow(), 0)

' release and destroy the menu (for sanity)
DestroyMenu hMenu

' return the selected menu item's index
Popup = iMenu

End Function

'form
'-------
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim oMenu As cPopupMenu
Dim lMenuChosen As Long

If Button = vbRightButton Then
Set oMenu = New cPopupMenu

' Pass in the desired menu, use '-' for a separator

lMenuChosen = oMenu.Popup("Menu 1", "Menu 2", "Menu 3", _
"-", "Menu 4")
'or lMenuChosen = oMenu.Popup("Menu 1", "Menu 2", "Menu 3", "Menu 4", "Menu 5")
MsgBox "You chose menu " & lMenuChosen

End If
End Sub



Iouri Boutchkine
iouri@hotsheet.com