Results 1 to 8 of 8

Thread: [RESOLVED] Make a button right or left click sensitive

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    43

    Resolved [RESOLVED] Make a button right or left click sensitive

    hi everyone
    as my title says i need to know for example how to make button only work when it is right clicked, and another button only when it is left clicked. is this possible??
    help greatly appreciated
    thanks

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Make a button right or left click sensitive

    Command buttons only work on left click anyway
    You could use something like the following to qualify a right click (returns true if the mouse is over the control) BUT the button will still only move up and down on Left clicks. The only way I can think of to make a button visually respond to right clicks would be to subclass the window and capture the mouse click before the window gets it, not shown here.
    Code:
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If InRangeClick(Button, X, Y, Command1.Width, Command1.Height, vbRightButton) Then
            Debug.Print "Right click!"
        End If
    End Sub
    
    
    Private Function InRangeClick(Button As Integer, ByVal X As Single, ByVal Y As Single, pWidth As Single, pHeight As Single, Optional ButtonPref As Integer = &HFFFF) As Boolean
    'generic function to cover all controls
        If Button And ButtonPref Then
           Select Case X
             Case Is < 0
             Case Is <= pWidth
                Select Case Y
                  Case Is < 0
                  Case Is <= pHeight: InRangeClick = True
                End Select
           End Select
        End If
    End Function

  3. #3
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Make a button right or left click sensitive

    It is actually possible..

    Simply use the Mouse_Down event

    Code:
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then MsgBox "Right Click"
    If Button = 1 Then MsgBox "Left Click"
    End Sub
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    43

    Re: Make a button right or left click sensitive

    oh dude you're a genius!!
    thank you so much!!

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Make a button right or left click sensitive

    Quote Originally Posted by some1uk03
    It is actually possible..

    Simply use the Mouse_Down event
    I was trying to simulate a mouse click which occurs as you release the mouse, immediately before mouse up and then only if the mouse is over the control. The code I show does this but the command button only visually shows a click if you use the left button, which is my point about sub-classing.

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    43

    Re: [RESOLVED] Make a button right or left click sensitive

    i needed this for a label, i had to click on it and change into 2 different colors. using Mouse_Down event it may not show it visually...but i dont think it ever does with labels does it?
    if i have to do the same thing again in future but with a button, i will probably use your method as it looks better when it shows it with both left and right click.

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: [RESOLVED] Make a button right or left click sensitive

    My method does not show the button going up and down on right click. For that you need to subclass.

    Am I repeating myself?

    Anyhow for what you want Someluke03 is your man.

  8. #8
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: [RESOLVED] Make a button right or left click sensitive

    Just keep in mind that as far as VB is concerned the command button has not been clicked when using the MouseDown event. In other words if you write the code below anywhere in your app (including the MouseDown event) it will return False. So if you need to check the Command1.Value elsewhere in your app (which happens often) then you will have to write a work around.

    Code:
    Print Command1.Value
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

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