CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2001
    Posts
    4

    dynamic menu and controls

    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.








  2. #2
    Join Date
    Apr 2001
    Posts
    19

    Re: dynamic menu and controls

    Go to http://www.it-lang-vb.net/ and search "crtmenu" (search for file name), I think it's what you search.

    Cristiano Larghi


  3. #3
    Join Date
    May 2001
    Posts
    4

    Re: dynamic menu and controls

    this site is in spanish. I need the code to be in english
    Thanks.


  4. #4
    Join Date
    Apr 2001
    Posts
    19

    Re: dynamic menu and controls

    It's an italian site, but the code is universal...



  5. #5
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: dynamic menu and controls

    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

  6. #6
    Join Date
    Jun 2001
    Posts
    1

    Re: dynamic menu and controls

    http://www.geocities.com/CapeCanaver...0/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.


  7. #7
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: dynamic menu and controls

    '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
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured