locked
right button mouse click event detect in visual basic 2010 express

    Question

  • Hi,

    I've just started using VB express 2010 and am trying to detect a right button mouse click.  I've looked at similar questions and usewd the code but can only detect left button clicks.  My code srgment is below

    Thanks

    PKDJ

     

     

    Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick

     

     

    If e.Button = Windows.Forms.MouseButtons.Right Then

     

     

    MessageBox.Show("Right button was clicked")

     

     

    Else

     

     

    MessageBox.Show("Left button was clicked")

     

     

    End If

     

     

    End Sub

    Tuesday, October 19, 2010 10:10 PM

Answers

  • Hi pkdj,
    
    Not all methods support also right Mouse button Click.
    The code you are using is correct. The Event seems to be wrong. I test it with VB 2008 Express actually but I had the same problem a long time before.
    The solution is, use Button1_MouseDown instead if MouseClick and you’ll succeed. 
    
    Public Class Form1
    
      Private Sub Button1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        'Works with both buttons
        If e.Button = Windows.Forms.MouseButtons.Right Then
          MsgBox("Right Mouse Button.")
        Else
          MsgBox("Another Mouse Button.")
        End If
      End Sub
    
      Private Sub Button1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
        'Works only with left button
        If e.Button = Windows.Forms.MouseButtons.Right Then
          MsgBox("Right Mouse Button.")
        Else
          MsgBox("Another Mouse Button.")
        End If
      End Sub
    End Class
    
    
    


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Wednesday, October 20, 2010 6:22 AM
  • Not all controls raise the mouseclick event on a right click.  You can see which do here:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.click.aspx

    Apart from the fact that the button is one of the controls that doesn't raise the event your code is ok - but would be better if you used the code block tool to post it.

    If you change the handle so it handles Me.MouseClick you will find it works ok when you click on a form.

    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Tuesday, October 19, 2010 10:42 PM
  • Hi Dave299

    Thanks for thew info. sorry to be a pain but when you say change the handle to Me.MouseClick I'm not sure what you mean can you show me an example?

    Thanks

    PKDJ

     

     

    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Tuesday, October 19, 2010 10:50 PM
  • In the first line of your code you have " Handles Button1.MouseClick".  That means the method will be called whenever the button raises the mouse click event.  If you change that to Handles Me.MouseClick it will be called whenever the form is clicked.
    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Tuesday, October 19, 2010 11:05 PM
  • Hi Dave299

    Tried that and now it does not recognise a left or right button click!   I'm sure it's goiung to be something simple but I can't spot it at the moment. Have inserted code below.

     Private Sub Button4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
          MessageBox.Show("Right button was clicked")
        Else
          MessageBox.Show("Left button was clicked")
        End If
      End Sub
    

     

    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Tuesday, October 19, 2010 11:19 PM
  • Works for me - you are clicking the form?
    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Wednesday, October 20, 2010 12:44 AM
  • Hi Giftzwockel (and Dave 999)

    You guys are stars!

    Have changed event to be MouseDown rather than MouseClick and it works a treat.  Really appreciate the quick responses and the help, was pulling my hair out trying to do this simple task!

    Now all I have to do is right the code that I want to be associated with the event. Watch this space as I am bound to be asking loads of questions!!!!!

    Regards

    PKDJ

     

     

    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Wednesday, October 20, 2010 8:17 AM
  • Hi pkdj,

    Fine to read this. So I suggest marking Daves and my answer as answer.

    Of course we are always visites of the forum and will assist where we can. Please use for each other question a separate thread.

     


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Wednesday, October 20, 2010 8:59 AM

All replies

  • Not all controls raise the mouseclick event on a right click.  You can see which do here:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.click.aspx

    Apart from the fact that the button is one of the controls that doesn't raise the event your code is ok - but would be better if you used the code block tool to post it.

    If you change the handle so it handles Me.MouseClick you will find it works ok when you click on a form.

    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Tuesday, October 19, 2010 10:42 PM
  • Hi Dave299

    Thanks for thew info. sorry to be a pain but when you say change the handle to Me.MouseClick I'm not sure what you mean can you show me an example?

    Thanks

    PKDJ

     

     

    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Tuesday, October 19, 2010 10:50 PM
  • In the first line of your code you have " Handles Button1.MouseClick".  That means the method will be called whenever the button raises the mouse click event.  If you change that to Handles Me.MouseClick it will be called whenever the form is clicked.
    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Tuesday, October 19, 2010 11:05 PM
  • Hi Dave299

    Tried that and now it does not recognise a left or right button click!   I'm sure it's goiung to be something simple but I can't spot it at the moment. Have inserted code below.

     Private Sub Button4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
          MessageBox.Show("Right button was clicked")
        Else
          MessageBox.Show("Left button was clicked")
        End If
      End Sub
    

     

    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Tuesday, October 19, 2010 11:19 PM
  • Works for me - you are clicking the form?
    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Wednesday, October 20, 2010 12:44 AM
  • Hi pkdj,
    
    Not all methods support also right Mouse button Click.
    The code you are using is correct. The Event seems to be wrong. I test it with VB 2008 Express actually but I had the same problem a long time before.
    The solution is, use Button1_MouseDown instead if MouseClick and you’ll succeed. 
    
    Public Class Form1
    
      Private Sub Button1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        'Works with both buttons
        If e.Button = Windows.Forms.MouseButtons.Right Then
          MsgBox("Right Mouse Button.")
        Else
          MsgBox("Another Mouse Button.")
        End If
      End Sub
    
      Private Sub Button1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
        'Works only with left button
        If e.Button = Windows.Forms.MouseButtons.Right Then
          MsgBox("Right Mouse Button.")
        Else
          MsgBox("Another Mouse Button.")
        End If
      End Sub
    End Class
    
    
    


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Wednesday, October 20, 2010 6:22 AM
  • Hi Giftzwockel (and Dave 999)

    You guys are stars!

    Have changed event to be MouseDown rather than MouseClick and it works a treat.  Really appreciate the quick responses and the help, was pulling my hair out trying to do this simple task!

    Now all I have to do is right the code that I want to be associated with the event. Watch this space as I am bound to be asking loads of questions!!!!!

    Regards

    PKDJ

     

     

    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Wednesday, October 20, 2010 8:17 AM
  • Hi pkdj,

    Fine to read this. So I suggest marking Daves and my answer as answer.

    Of course we are always visites of the forum and will assist where we can. Please use for each other question a separate thread.

     


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by pkdj Wednesday, October 20, 2010 10:04 AM
    Wednesday, October 20, 2010 8:59 AM