Check which mouse button was "upped" - vb.net

Well, I have an event handler for when a mouse button is up. I want to check which button was (left or right). This is the function definition:
Private Sub PictureBox2_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
I used
e.Button.Left()
to try to get a boolean, but I get an error....

use the button property
If (e.Button = Windows.Forms.MouseButtons.Left) Then
'Do Somthing
End If

Try e.Button = Windows.Forms.MouseButtons.Left:
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.button.aspx

Related

VB:mouse click not work

My codes:
Me.KeyPreview = True
...
Private Sub Form_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
MsgBox("Right Mouse clicked.")
End If
End Sub
Try to capture mouse right click, but not work.
Any suggestions welcomed. Thanks
As other mention in comments your code seems right, but will only work on plain form. To overcome that you can join events.
Private Sub Form_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick, Button1.MouseClick, Control1.MouseClick, AnyOtherControl.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
MsgBox("Right Mouse clicked.")
End If
End Sub
Please replace Control names you want your event to fire on in this code:
Handles MyBase.MouseClick, Button1.MouseClick, Control1.MouseClick, AnyOtherControl.MouseClick
I'm guessing you are using any of container controls which fill up most of your form. If you want your event to work with them you need to add them to your event.
Finally there is also matter of DoubleClick which won't fire above event. To overcome it all you need to do is change MouseClick to MouseDown

button release after mouse hovering out

I have this six button
After i press one button, for example i press settings it will open an new windows after i close that windows the button remain pressed.
Here are the settings button code:
Private Sub btn_SETTINGS_MouseEnter(sender As System.Object, e As System.EventArgs) Handles btn_SETTINGS.MouseEnter
btn_SETTINGS.ForeColor = Color.White
End Sub
Private Sub btn_SETTINGS_MouseLeave(sender As System.Object, e As System.EventArgs) Handles btn_SETTINGS.MouseLeave
btn_SETTINGS.ForeColor = SystemColors.HotTrack
End Sub
Private Sub btn_SETTINGS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SETTINGS.Click
OpenSettings()
End Sub
Any suggestion what can i do to fix this problem.
Okay, this problem seems related to Focus that got set to the button when you pressed it. Very easy and quick fix of this problem is, change the focus of control to some other control. On the same side, add one label control which is of Hidden type say lblHidden. So when you're doing following code then change focus.
Private Sub btn_SETTINGS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SETTINGS.Click
OpenSettings()
Me.ActiveControl = lblHidden
End Sub
This will change the focus to hidden control. However if you are doing any formatting change of button on click event then revert it back to original in above even.t

Get the name of the button that triggered the event

I have an event handler that handles the click event of multiple buttons:
Private Sub primeHandler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _2s.Click, _3s.Click, _4s.Click, _5s.Click, _6s.Click
End Sub
_2s, _3s, etc are all buttons.
Now I need a way to determine which button triggered the event and also get the button's name as string. Any way to do that? Thanks
You can cast sender to type Button and access the Name property.
Private Sub primeHandler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _2s.Click, _3s.Click, _4s.Click, _5s.Click, _6s.Click
Dim myButton As Button = CType(sender, Button)
Dim myName As String = myButton.Name
End Sub
Use sender - it's what it's designed to do.
MessageBox.Show((sender as Button).Name);
If you're going to use it more than once, assign it to a variable to make it easier.
var button = (sender as Button);
MessageBox.Show(button.Name);

Mousewheel Event

I have got as form with a vertical scroll bar, I want to use the mouse wheel to scroll up and down the form but I cant seem to get to get the mousewheel event to fire.
I just use the standard mousewheel event
Private Sub frmTest_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
'code here
End Sub
Any help would be appreciated.
Try to place a one Panel in your Form set to AutoScroll = true, Dock = Fill and from the Panel place all your control like TextBox, label, listView etc..
Private Sub Panel1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles Panel1.MouseEnter
Panel1.Focus()
End Sub
Private Sub Panel1_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseWheel
MessageBox.Show("okay")
End Sub

call double click on mouse single click

how do we call double click on single mouse click event? Iam doing this and it's not working. Any ideas ?
Private Sub RichTextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick
RichTextBox1_MouseDoubleClick(sender, e)
End Sub
In properties windows click Event Icon and find double click and in selected event name select "RichTextBox1_MouseClick"
Have you tried:
Private Sub RichTextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick, RichTextBox1.MouseDoubleClick, RichTextBox1.DoubleClick
'Do stuff here
End Sub
There are basically 2 options. The first is how your doing it, which the click can still do more as well as the double click.
Private Sub RichTextBox1_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick
'' i can do stuff here
RichTextBox1_MouseDoubleClick(sender, e)
'' and here
'' as well as do whatever double click is doing
End Sub
Private Sub RichTextBox1_MouseDoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDoubleClick
'' to do something here
End Sub
The other option, if they are always going to do the same, you could do this:
Private Sub RichTextBox1_Single_DoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick, RichTextBox1.MouseDoubleClick
'' do something here
End Sub
Notice the second Handles bit on the end? that function can handle both now :)
Either should work fine for what you want.