Mousemove detect left click not firing in usercontrol winform - vb.net

I know this should be simple to do but for some reason I can't get a simple mousemove when I left click the mouse. this inside a custom button I have made with 2 labels in it. I am moving only on the button area and not the label. When I move over the button I see "Mousemove" printed to the console but when I hold the left click I get nothing...Not even the "Mousemove". I looking to implement a dragdrop for this button and was just starting out simple to detect the left click and mousemove. Am I missing another setting. I had a Click event and event removed that just to detect this movement.
Thanks in advance for any info.
Public Sub New()
'InitializeComponent()
DoubleBuffered = True
AllowDrop = True
End Sub
Private Sub CustomerButton_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Debug.Print("MouseMove")
If e.Button = MouseButtons.Left Then
Debug.Print("MouseMove and Left Click")
End If
End Sub

Related

Use mouse scroll anywhere on screen in VB.net

I'm trying to setup global shortcut using mouse button and scrolling combination, (scrolling up (or down) whlie pressing right mouse button).
I'm thinking I should try to react to mouse scroll, but I have no idea how to detect mouse wheel outside of form.
Right now I got it to react to mouse click with timer and setting form to always on top (no prolem with it being always on top), but no idea how to progress with it.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
TextBox1.Text = "YES"
Else
TextBox1.Text = "NO"
End If
End Sub
Goal is to make it minimize aplications set by user with that shortcut, just in case that info is helpful. I have rest of the code just cant figure that part out.
EDIT:
It works inside form with this:
Private Sub ListBoxCHOWAJ_MouseWheel(sender As Object, e As MouseEventArgs) Handles ListBoxCHOWAJ.MouseWheel
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
If e.Delta > 0 Then
TextBox1.Text = "up"
Else
TextBox1.Text = "down"
End If
End If
End Sub
You need a mouse hook to detect scrolling outside your application. Have a look at my InputHelper library and its low-level mouse hook.
To include it in your project, download the compiled DLL and add it as a reference:
Right-click your project in the Solution Explorer and press Add Reference...
Go to the Browse tab.
Locate the InputHelper DLL-file, select it and press OK.
You can do all your logic in the hook's MouseWheel event handler, no need for the timer any longer:
Imports InputHelperLib
Public Class Form1
Dim WithEvents MouseHook As New InputHelper.Hooks.MouseHook
Private Sub MouseHook_MouseWheel(sender As Object, e As InputHelperLib.InputHelper.Hooks.MouseHookEventArgs) Handles MouseHook.MouseWheel
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
'Do something...
End If
End Sub
End Class
There's no need to check e.Delta like you did because it will never be 0.

VB.Net 2015 "Disable btn_MouseLeave on btn_MouseClick"

In VS 2015 Community, My form as a button.
Current Code is:
Private Sub CellStart_MouseHover(sender As Object, e As EventArgs) Handles CellStart.MouseHover
CellStart.BackgroundImage = My.Resources.graycell_hilight
End Sub
Private Sub CellStart_MouseLeave(sender As Object, e As EventArgs) Handles CellStart.MouseLeave
CellStart.BackgroundImage = My.Resources.graycell
End Sub
Private Sub CellStart_Click(sender As Object, e As EventArgs) Handles CellStart.Click
CellStart.BackgroundImage = My.Resources.graycell_select
End Sub
What I want is that if the button is in the post click state (Current BackgroundImage is graycell_select), for the MouseLeave and Mouse Hover to no longer be enabled. If the button becomes deselected I need them to be enabled again. I have tried if statements, assigning an integer +1 on click but nothing seems to override the leave and hover events. I am dreadfully new so any help or points to something similar is appreciated. Thanks in advance. This is in a winform.
Click your button and Find the GotFocus and LostFocus in the Event tab.
Click GotFocus and put this code inside of it.
CellStart.BackgroundImage = My.Resources.graycell_hilight
Click LostFocus and put this code inside of it.
CellStart.BackgroundImage = My.Resources.graycell
leave the click on what it has.
Dont forget to remove not just copy remove the codes in the MouseHover and MouseLeave

.NET TreeView NodeMouseClick vs MouseClick vs Click events

Could someone clarify what's the difference between these three events for the TreeView control in .NET?
Everyone of them is triggered when a node is clicked.
In practice, when should I use each of them over the others?
Private Sub TreeView_Devices_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) _
Handles TreeView_Devices.NodeMouseClick
DisplaySignals(e.Node, Me)
End Sub
Private Sub TreeView_Devices_MouseClick(sender As Object, e As MouseEventArgs) _
Handles TreeView_Devices.MouseClick
If e.Button = MouseButtons.Right Then
MsgBox("Right Click")
ElseIf e.Button = MouseButtons.Left Then
MsgBox("Left Click")
End If
End Sub
Private Sub TreeView_Devices_Click(sender As Object, e As EventArgs) _
Handles TreeView_Devices.Click
End Sub
thanks.
.NET distinguishes between Click and MouseClick because some controls allow firing a logical click with the keyboard. Buttons and checkboxes for example, you click them with the space-bar. Of course there's no mouse information available when the user operates the keyboard so that's why the Click event has a plain EventArgs instead of a MouseEventArgs. You need to use MouseClick only when you care about the mouse location for some reason.
It doesn't apply to TreeView, it doesn't support "clicking" a node with the keyboard.
Next thing that matters is exactly where the user clicks. Only some of the locations in a TreeView co-incide with a node. So that's why there's NodeMouseClick, it only fires when the user clicked a node. And doesn't fire when he clicked anywhere else, when expanding or collapsing a node for example.
It is a convenience event, it isn't actually necessary. Because MouseClick is already good enough to also detect that the click was on a node. But it requires more code, you have to use the HitTest() method. The event helps you avoid having to write that code. Convenient.
Since you very rarely care about the user clicking on anything but nodes, you'd normally always favor NodeMouseClick.

Contextmenu position in vb.net

i have a datagridview. On right click it shows a contextmenu but it is always in the right upper corner. I want it so that the menu appears on the cell where user right clicks. It could be Cell 1 or two or whatever.
Thanks
Furqan
The easiest way to do this is to handle showing your context menu on your own (not using the context menu property on the grid view) on MouseDown for the data grid. Like this:
Private Sub DataGridView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(CType(sender, Control), e.Location)
End If
End Sub

Button not hiding when mouse leaves from bottom

I have a form where I want buttons at the very bottom edge of the form, with no gap to the border. These buttons shall be "auto-hide", so they only show when the mouse is in for example the lower 20 pixels of the form. So I use the MouseMove event to trigger this, like code below. However, if mouse leaves the form across the bottom edge, where the buttons are, then the buttons will obviously remain. But I want them to hide. So I need for this purpose to hide the buttons by some other event. Hence I try to hide them in the form's MouseLeave event. But this makes the buttons unclickable and in an erratic state, flashing on and off when the mouse goes over the button.. Why is this? And how can I avoid this problem to get such autohide feature?
Private Sub ZgScale_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Y > Me.ClientSize.Height - 30 Then
Button1.Visible = True
Else
Button1.Visible = False
End If
End Sub
Private Sub ZgScale_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.MouseLeave
Button1.Visible = False
End Sub
The MouseLeave event fires when the mouse is no longer directly on that control (or form).
If the mouse moves on to a child control, the event will fire.
You need to check whether the mouse is no longer on the form, like this:
If Not Me.ClientRectangle.Contains(Me.PointToClient(e.Location)) Then
Button1.Visible = False
End If
EDIT: Fixed
Windows has direct support built-in for this scenario. Also exposed in Windows Forms and WPF. Once you get the MouseMove event, set the Capture property on the control to True. That forces all mouse messages to be directed to the control, even if the mouse moves outside of the control window.
Once you see it moving outside of the control bounds, set Capture back to false and hide your control. Beware that capture is turned off when the user clicks the mouse so you'll probably have to turn it back on afterwards. Although it should be automatic, you'll get another MouseMove event. Could fail if the user moves the mouse really fast.