Results 1 to 11 of 11
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Popup Menu for Control (2000)

    I've just started using a TreeView control in a project, and was hoping to be able to implement a popup menu to appear when a node is right-clicked. I have not seen this kind of functionality on userform controls, so I presume that, at best, I'd have to program it in from scratch. Ideally, I'd like to be able to activate both a tree node and the popup menu using a right-click event. Again, this event is not an inbuilt property, so I suppose I'd use the keypress event to try to capture it.

    The popup menu would link to simple commands like "Add node below", "Add child node", "Delete sub-tree"... At the moment, these can be achieved by command buttons on the form, but I feel it would be less confusing for the user to have the right-click/ popup functionality there.

    Any suggestions appreciated.

    Alan

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Popup Menu for Control (2000)

    Well, here's a way to capture the button that was clicked, but as for displaying the shortcut menu... since this is the first time I'm lookin' at this control, I won't venture a guess.

    <pre>Private btnNum As Integer

    Private Sub TreeView1_Click()
    MsgBox "You clicked the " & IIf(btnNum = 1, "left", "right") & " mouse button!"
    End Sub

    Private Sub TreeView1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, _
    ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
    btnNum = Button
    End Sub</pre>

    Hope this helps.

  3. #3
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Popup Menu for Control (2000)

    The MouseDown event also captures which button is depressed, and is better as it gives the appearance of reacting morequickly than a mousedown. There are some occasions though where a mouseup has to be used.

    The menu builder can be used for a popup menu. Sorry, I don't have code for that just now.

    Where I work, there is a standard that any menu option is alos avaliable from the keyboard. So a right click menu will also be refelected in a menu available from the main menubar.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Popup Menu for Control (2000)

    Hi Alan,

    Here is some sample code to create the commandbars:

    Sub AddFormCommandbars()
    Dim cCommandbar As CommandBar
    On Error Resume Next
    RemoveCommandbars
    Set cCommandbar = CommandBars("YourMenu Popup")
    If cCommandbar Is Nothing Then
    With CommandBars.Add(Name:="NamesMenu Popup", Position:=msoBarPopup, temporary:=True)
    With .Controls.Add(Type:=msoControlButton)
    .Caption = "Hide"
    .OnAction = "HandleNamesMenuClick"
    End With
    With .Controls.Add(Type:=msoControlButton)
    .Caption = "Unhide"
    .OnAction = "HandleNamesMenuClick"
    End With
    With .Controls.Add(Type:=msoControlButton)
    .Caption = "Delete"
    .OnAction = "HandleNamesMenuClick"
    End With
    With .Controls.Add(Type:=msoControlButton)
    .Caption = "Localise"
    .OnAction = "HandleNamesMenuClick"
    End With
    With .Controls.Add(Type:=msoControlButton)
    .Caption = "Globalise"
    .OnAction = "HandleNamesMenuClick"
    End With
    With .Controls.Add(Type:=msoControlButton)
    .Caption = "Evaluate"
    .OnAction = "HandleNamesMenuClick"
    End With
    With .Controls.Add(Type:=msoControlButton)
    .Caption = "Is Used?"
    .OnAction = "HandleNamesMenuClick"
    End With
    End With
    End If
    Set cCommandbar = Nothing
    End Sub
    'JKP Feb 21 2003: rightclick menues for form
    Sub RemoveCommandbars()
    On Error Resume Next
    CommandBars("YourMenu Popup").Delete
    End Sub

    Sub HandleNamesMenuClick()
    Select Case Application.Caller(1)
    Case 1
    Hide_Click
    Case 2
    Unhide_Click
    Case 3
    Delete_Click
    Case 4
    Localise_Click
    Case 5
    Globalise_Click
    Case 6
    Evaluate_Click
    Case 7
    Used_Click
    End Select
    End With
    End Sub

    And for the control that needs the menu:

    Private Sub lbxNames_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Button = 2 Then
    Application.CommandBars("YourMenu Popup").ShowPopup
    End If
    End Sub

    One disadvantgae: the selection in the control that gets the menu does NOT change.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Popup Menu for Control (2000)

    Thank you gentlemen, for all of the benefit of all your collective expertise. There's plenty to get me started here, and it's information I've had great difficulty sourcing on the net. All I need now is to find some more info on the TreeView control itself.

    I feel it's an opportune time to say thank you to all of the wonderful folk who make such valuable contributions in this unique lounge. I'm relatively new to VBA, although I've had a programming background for a number of years, and I'm certainly no expert when it comes to individual Office products. However, as a result of the quality help I've received here, I've been able to solve most problems and undertake developments, that I'd have had little/no hope of tackling "alone in the deep end" (which is where the management threw me <img src=/S/cauldron.gif border=0 alt=cauldron width=20 height=20> ). The people here really are amazing.

    Thank you all <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>.

    Alan

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Popup Menu for Control (2000)

    There is a sample spreadsheet about the treeview control here .

    Please note, that the rightclick menu may crash Excel 97, which is why I discontinued trying to use it on my Name manager utility.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Popup Menu for Control (2000)

    Thanks Jan. I've checked out the sample you mentioned, and have also found a very good "step-by-step" article here. The right click is starting to become problematic already (without crashing things even) so I may end up using an alternative. Still, I'd like to follow it through anyway, as a general method.

    cheers

    Alan

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Popup Menu for Control (2000)

    <hr>Private Sub lbxNames_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Button = 2 Then
    Application.CommandBars("YourMenu Popup").ShowPopup
    End If
    End Sub

    One disadvantgae: the selection in the control that gets the menu does NOT change.<hr>
    That (trying to change the selection programmatically seems impossible) really irritated me, too, so I went with the Click event code, storing the button pressed in the MouseUp event. As Geoff pointed out, I could have stored the value in the MouseDown event, too. Aside from getting the right selection, another reason to use Click rather than MouseDown to trigger the menu is that the user might have dragged the entry (or dragged the mouse off the control), in which case you wouldn't want the Popup to pop up.

    P.S. Thanks for posting your menu code. I'll need to do this sort of thing someday soon, I'm sure!

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Popup Menu for Control (2000)

    Thanks for investigating. Could you post the relevant piece of code? I might use this someday...
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Popup Menu for Control (2000)

    I think I threw away my test document, but the best part was in <post#=237081>post 237081</post#>.

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Popup Menu for Control (2000)

    Thanks.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Posting Permissions

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