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.
Related
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
I have a simple issue in my Windows Form. I have a listbox that is acting as a log for some serial data I have input, and would like to be able to use the scroll wheel to move thru it. In order to do this, I have a variable, hoverList, set to 0 initially, which becomes 1 inside the MouseEnter event for the listbox. Then for the MouseWheel event, I increase or decrease the selected index in the listbox, but only if hoverList is 1. I expect that when the mouse leaves the area above the listbox, the MouseLeave event will fire, which sets hoverList to 0 and stops allowing "scrolling". This works fine, until I click in the listbox; I can still scroll just fine, but now I can scroll the listbox everywhere on my screen. It seems that clicking inside the listbox doesn't allow the MouseLeave event to ever trigger. Is this the case? And if so, is there a workaround that avoids this problem? Code for the 3 events is shown below
Private Sub LstSerialData_MouseLeave(sender As Object, e As EventArgs) Handles lstSerialData.MouseLeave
hoverList = 0
End Sub
Private Sub LstSerialData_MouseEnter(sender As Object, e As EventArgs) Handles lstSerialData.MouseEnter
hoverList = 1
End Sub
Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
If hoverList Then
If e.Delta > 0 Then
If lstSerialData.SelectedIndex > 10 Then
lstSerialData.SelectedIndex -= 10
Else
lstSerialData.SelectedIndex = 0
End If
Else
If lstSerialData.SelectedIndex < lstSerialData.Items.Count - 10 Then
lstSerialData.SelectedIndex += 10
Else
lstSerialData.SelectedIndex = lstSerialData.Items.Count - 1
End If
End If
End If
End Sub
I have found the solution to the problem. When clicking in the listbox,it becomes the selected control; since it has a scrollbar, I am actually allowed to use the scroll wheel to scroll thru it. Moving the cursor off of the listbox disables my built in scroll, but doesn't deselect the listbox, meaning I can still scroll thru it. I edited my MouseEnter and MouseLeave events to select and deselect the listbox respectively. This means that as soon as my cursor leaves the listbox, regardless of whether or not I have clicked inside it, it is deselected and will no longer let me scroll thru it. It also means my ghetto workaround to allow scrolling was no longer needed.
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.
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.
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.