How do I detect when i release mouse button in VB? - vb.net

How do I detect when I release the left mouse button in a VB program?
So currently using:
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
PictureBox1.Visible = True
End Sub
Means whenever I click my button the picturebox appears.
So how do I get the picturebox to disappear when I release the mouse button?

The click event will only occur after the the user releases the mouse button. A click occurs when the left mouse button is pressed and released.
You want to use the MouseDown and MouseUp events.

Related

Mousemove detect left click not firing in usercontrol winform

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

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

Mousedown event on winform button on touch screen

I have a vb.net winform application with a button that starts some motors (but that could be anything) on the mouse down event.
Private Async Sub btnForward_MouseDown(sender As Object, e As MouseEventArgs) Handles btnForward.MouseDown
Await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B + OutputPort.C, 100, 30000, False)
End Sub
Private Async Sub btnForward_MouseUp(sender As Object, e As MouseEventArgs) Handles btnForward.MouseUp
Await _brick.DirectCommand.StopMotorAsync(OutputPort.All, False)
End Sub
This works fine on my desktop PC where I am using a mouse. But this does not work on a touch screen device. Although the touching of the screen is interpreted a s a mouse click event, the mouse down event does not trigger and a prolonged touch is then interpreted as a right click. Is there a work around for this? So far all I found where suggestions to move the finger to trigger the mouse down event.
Thanks!

Move control to cursor location?

The think is I would like to show FlowLayoutPanel at mouse cursor location. But with my code bellow it only jumps around center of my form. I'm triggering it on DataGridView.CellMouseEnter event. Asking for some advice, thank you.
Private Sub dgw_CellMouseEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgw.CellMouseEnter
FlowLayoutPane1.Location = Cursor.Position
FlowLayoutPane1.Visible = True
End Sub
Use MouseMove instead of the MouseEnter event. MouseEnter is called only once when the mouse ENTERS the control.
Be aware that the MouseMove event fires even if the mouse location may be outside of the control.

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.