Move control to cursor location? - vb.net

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.

Related

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!

.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.

Handle Control in UserControl click

I made a user control, with a picture box and 2 labels. Whenever i click on any of the controls in the user control in a form, the user control click event does not happen. How can i do for when i click any of the labels or the picturebox the usercontrol.click event happens too?
The labels are "label1" and "label2", The picture box is named "pic1".
The usercontrol's name is "Tile".
Please Help!
I have tried something like event handling, using addhandler but it confuses me a little...
Public Event ItemClicked()
`Private Sub Pic1_Clicked(sender As Object, e As EventArgs) Handles Pic1.Click
RaiseEvent ItemClicked()
End Sub`
But i'm stuck there, i don't know what else to do.

Catching event when a control loses child control

Imagine this application in Vb.net, I have 2 panels and several buttons. I am moving buttons from a panel to other panel (through catching drag and drop events). I reached it.
Now I am trying the following:
Is there any way to raise an event from a panel when this panel loses some child button (or control)?
Thanks in advance.
Try this event when a control has added to the panel :
Private Sub Panel1_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Panel1.ControlAdded
End Sub
And this when a control has removed :
Private Sub Panel1_ControlRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Panel1.ControlRemoved
End Sub
The panel control doesn't seem to have an event that fires when its child control collection changes. source
The best thing to do would be handle this as part of the drop event. Presumably you have some code to determine if the button is to be moved. If this is true, call a function to do everything you want when a panel loses a 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.