Results 1 to 14 of 14

Thread: Activate RIGHT MOUSE CLICK to press a button on a form

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    9

    Activate RIGHT MOUSE CLICK to press a button on a form

    I have a form with some buttons i built in visual studio basic 2010

    When I left click a button it uses 1 value....but I want to be able to right click the same button and it use a different value

    i cannot find anywhere how to activate the right mouse button so it presses the buttons on the form so it use the different value i specify

    can u help?

    thanks

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    Let the Click event handle the left mouse click and use MouseDown event, check to see if the right mouse button was clicked.

    Code:
        Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
            Console.WriteLine("Left mouse button clicked")
        End Sub
        Private Sub cmdClose_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cmdClose.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                Console.WriteLine("Right mouse button clicked")
            End If
        End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    9

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    unfortunately, my code starts like this:

    Private Sub rButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim rButton As Button = sender 'A reference to the button that was clicked With rButton

    If radio1.Checked Then
    value = value1.Text
    End If
    If Radio2.Checked Then
    value = value2.Text
    End If



    I want to change the bit in red so it says this:

    if left mouse button pressed, value = value1.text
    if right mouse button pressed, value = value2.text

    the catch is, i cant change the bit that says

    Private Sub rButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim rButton As Button = sender

    is it still possible to do this?

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    Quote Originally Posted by jillian View Post
    unfortunately, my code starts like this:

    Private Sub rButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim rButton As Button = sender 'A reference to the button that was clicked With rButton

    If radio1.Checked Then
    value = value1.Text
    End If
    If Radio2.Checked Then
    value = value2.Text
    End If



    I want to change the bit in red so it says this:

    if left mouse button pressed, value = value1.text
    if right mouse button pressed, value = value2.text

    the catch is, i cant change the bit that says

    Private Sub rButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim rButton As Button = sender

    is it still possible to do this?
    What I gave you works, so again with the same logic. for your code change Value1 and Value2 to whatever you are using, looks like a TextBox.

    Code:
        Private Value As String = ""
        Private Value1 As String = "1"
        Private Value2 As String = "2"
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Value = Value1
            showvalue()
        End Sub
        Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                Value = Value2
                showvalue()
            End If
        End Sub
        Private Sub showvalue()
            MsgBox(Value)
        End Sub

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    An alternative

    Code:
        Private Value As String = ""
        Private Value1 As String = "1"
        Private Value2 As String = "2"
    
        Private Sub Button1_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button1.Click
            If Button1.Tag Is Nothing Then
                Value = Value1
            Else
                Value = Value2
                Button1.Tag = Nothing
            End If
            Debug.WriteLine(Value)
        End Sub
    
        Private Sub Button1_MouseDown(sender As Object, _
                                      e As System.Windows.Forms.MouseEventArgs) _
                                  Handles Button1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                Button1.Tag = True
                Button1.PerformClick()
            End If
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    What if we ignore the button1_Click event and handle all on the MouseDown (or mouseUp), i like the later because it fires when you release the button.

    vb Code:
    1. Private Sub Button1_MouseUp(ByVal sender as Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
    2.     If e.Button = Windows.Forms.MouseButtons.Left Then
    3.         Value = Value1
    4.     ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
    5.         Value = Value2
    6.     End If
    7. End Sub

    Why not just put an Else clause insead of Else If? because there are other possibilities to Windows.Forms.MouseButtons like middle button, xButtons (for intellymouse) or nothing.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    Are talking about a RadioButton or Button.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    would be the same, wont it?
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    Having the user right click is not intuitive and would not use the method which is why I proposed two events and in reality should have suggested two buttons where there is no right click. I think about it this way, if you have a DataGridView with a context menu and no corresponding visual method (regular menu item on the form or button) that the user can easily see than more likely than not they will forget the option exists in the context menu. I have seen this when developers do not provide an alternate method where the user does not use said application often they forget. Any ways what you suggested is perfectly fine in the context of the question here it simply comes down to what works for user requirements.

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    I agree with Kevin. Consistent UI rules.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    9

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    i need to do it this way:

    Code:
    dim button as new button
    with button
    
          .backcolor = color.khaki
          .text = name
          .mousebuttonsXXXX = XXXXX
    i need it this way so i can use the .mousebuttons later in my code

    i was told that if right mouse is clicked the value 2097152 is returned and for left click 1048576 is returned.

    so somehow, i need to specify .mousebuttonsXXXXX= captures value returned

    and then in the other part of the code it will say

    Code:
    dim mouseclicked as integer = .mousebuttons....
    
    if mouseclicked = 1048576 
    then value = value1.text
    
    if mouseclicked = 2097152 
    then value = value2.text
    i dont know how to capture the 2 numbers using

    .mousebuttonsXXXX = XXXXX

    this should solve it.

  12. #12
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    Simply you could just use a boolean variable and assign it's value when the mouse click/down event is risen. Say left button is pressed, assign it's value to true, otherwise false. This way later on where it's needed you can simple evaluate which button was most recently pressed..

    Really you don't need these values in this case. You're able to by using kaliman79912 method a few posts back, then using your own values to represent which button was pressed. Knowing these values you should then be able to interpret them later on.

    My personal experience with those values your looking for were most commonly used in the good ol' vb6 and earlier days.. Now I feel today its unnecessary with all the implementations of the .net framework.

    Happy coding
    Last edited by DavesChillaxin; Jul 5th, 2011 at 10:03 AM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    9

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    can you please show how to do this with the code on both sides...?

  14. #14
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Activate RIGHT MOUSE CLICK to press a button on a form

    I may not be alone here but still I'm a little confused to exactly what it is you want but try either of these two methods already used above. I've read everything over and it seems this is what your asking for.

    So you could try this here..

    Code:
          Private Sub Button1_MouseUp(ByVal sender as Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
              If e.Button = Windows.Forms.MouseButtons.Left Then
                  Value = Value1
              ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
                  Value = Value 2
              End If
          End Sub
    ~kevininstructor

    Or

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Value = Value1
        End Sub
        Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                Value = Value 2
            End If
        End Sub
    ~kaliman79912

    if this doesn't help then feel free to PM me, and I'll be glad to help out.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

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