-
Jul 1st, 2011, 11:20 PM
#1
Thread Starter
New Member
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
-
Jul 2nd, 2011, 10:09 AM
#2
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
-
Jul 4th, 2011, 07:55 AM
#3
Thread Starter
New Member
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?
-
Jul 4th, 2011, 12:15 PM
#4
Re: Activate RIGHT MOUSE CLICK to press a button on a form
Originally Posted by jillian
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
-
Jul 4th, 2011, 01:03 PM
#5
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
-
Jul 4th, 2011, 01:06 PM
#6
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:
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 = Value2
End If
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
-
Jul 4th, 2011, 01:22 PM
#7
Re: Activate RIGHT MOUSE CLICK to press a button on a form
Are talking about a RadioButton or Button.
-
Jul 4th, 2011, 01:32 PM
#8
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
-
Jul 4th, 2011, 02:00 PM
#9
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.
-
Jul 4th, 2011, 03:31 PM
#10
Re: Activate RIGHT MOUSE CLICK to press a button on a form
I agree with Kevin. Consistent UI rules.
-
Jul 5th, 2011, 07:27 AM
#11
Thread Starter
New Member
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.
-
Jul 5th, 2011, 09:57 AM
#12
Hyperactive Member
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.
-
Jul 5th, 2011, 09:26 PM
#13
Thread Starter
New Member
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...?
-
Jul 6th, 2011, 10:46 AM
#14
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|