How can I get mouse location on a button? - vb.net

VB.NET
Visual Studio
I have several buttons that I add at runtime, and I have a MouseDown event handler for the click event. Left click works just fine, but the right click event fires, but doesn't do what I need it to.
If e.Button = Windows.Forms.MouseButtons.Right Then
If sender.Bounds.Contains(e.Location) = True Then
ContextMenuStrip.Show(Cursor.Position)
End If
End If
I shortened this to make it easier to read.
When I look at e.Location, it shows the mouse location in relation to the button.
So if my button is at 400, 600, the location of the mouse should be in that area, but the mouse location comes back with 20, 30 because it is at 20,30 inside the button.
How can I do this right click event properly?

MouseDown event raises when the mouse is down on your control, so the mouse is surely in your control bounds and you don't need to check if the button contains e.Location.
To show the context menu strip, if you assign the context menu strip to ContextMenuStrip property of your control, then you don't need to do anything and the menu will show automatically. But if for any reason you want to handle MouseDown event, you can use either of these options:
ContextMenuStrip1.Show(DirectCast(sender, Control), e.Location)
ContextMenuStrip1.Show(MousePosition)
Note: Just for learning purpose if you want to check if e.Location is in your button, you can use either of these options:
Button1.ClientRectangle.Contains(e.Location)
Button1.Bounds.Contains(Button1.Parent.PointToClient(Button1.PointToScreen(e.Location)))
Or some other combinations using PointToClient, PointToScreen, RectangleToClient, RectangleToScreen methods of controls.

Related

Detect MouseUp after Leaving MouseDown Control

Using VB.NET, Short of adding a mouseup event to every control in my form, does anybody know a way to trigger a mouseup event regardless of where the cursor is located or what control the cursor is in?
Or is there a way to see if the mouse left button is up, that way I can do on enter, if mouseup then...
Or is there a way to see if the mouse left button is up, that way I can do on enter, if mouseup then...
There is a static property on the Control class - Control.MouseButtons which returns, in the form of a flag based enum, which buttons on the mouse are currently pressed.
var pressedButtons = Control.MouseButtons;
if (!pressedButtons.HasFlag(MouseButtons.Left))
{
// Left mouse button is not down, so do stuff
}

How would I differentiate from Click and Double Click while using a C1FlexGrid

I want to use the double click and click events for c1flexgrid in vb.net, but when I double click it only goes to the click event handler and never fires the double click event.
How can I separately handle the click and double click events?
As stated in MSDN :
Pressing a mouse button when the cursor is over any particular clickable control raises the events in following order:
MouseDown event.
Click event.
MouseClick event.
MouseUp event.
MouseDown event.
DoubleClick event.
MouseDoubleClick event.
MouseUp event.
Two single clicks that occur close enough in time, as determined by the mouse settings of the user's operating system, will generate a MouseDoubleClick event instead of the second MouseClick event.
Therefore,
Check your mouse settings.

Missing mouse up event from different canvas in smartGWT

I have two canvases located to each other. One is supposed to be a kind of workspace you can add items to, select and rearange them. The other one is just a property view.
What I want to do is to be able to draw a rectrangle on the workspace. As long as the user holds down the mouse button the rectangle will show up. If he releases the button all items that are beneath the rectangle will be selected. This currently works good with the MouseDown, MouseStillDown und MouseUp events. I'm drawing another rectangle shaed canvas on the workspace which will be transformed on every MouseStillDown event and the selection will occur on the MouseUp event. My problem is, that if the user hold down the mouse button and moves it to the property canvas and then releases the button the MouseUp event from the workspace isn't called. Neither is the one from the property since it's missing a MouseDown event. So if the user releases the button there the selection won't work and the rectangle stays in the workspace.
Is there an oppurtunity to somehow avoid this? Or is there a better way to determine the area the user selected with it's mouse while holding down the left mouse button?

ignore mouse event on label inside panel

I am making a custom menu. I am applying colors on panels on mouse enter and mouse leave.
I have labels on these panels, and the mouse enter and leave events work snappy, but as soon as I hover over the label (on/inside) the panel, the mouse leave event is fired. I know I can just do the same thing for the label mouse enter event, but I am doing some other visual stuff, and I need to have the label mouse events totally disregarded. Any ideas?
Thanks in advance.
I eventually just used the menu control, and styled it accordingly. Does the job better than I could with code...

Simulate Winforms Button Click Animation

I have a button and inside my button I have an image control. When the users click on the image I'd like to animate the button so it appears the button was pressed. I don't really care whether the actual button press event fires or not--it's the illusion of a button press I want to see.
Note: the only options I see on the web involve writing directly to the Windows API--a level of complexity and non-upgradability I really don't want to get into.
Why not just use two different images, one for a normal state, and another for when your button is being pressed.
If you want to go for more complicated route try using GDI+. Here is a quick sample tutorial on how to do this.
Why are you using an image control inside your button control instead of using the button control's Image property?
Using the Image property of the button will give you a button with an image that the user can press and that will raise the OnClick event without doing any extra work or re-implementing features that are already available.
I ended up making my picturbox look like a button by giving it a raised border style. Then on the mouseclick event I simulate the look of a button press by changing the border style for a few hundred miliseconds.
Private Sub simulateButtonPress(ByRef pictureBox As Infragistics.Win.UltraWinEditors.UltraPictureBox)
pictureBox.BorderStyle = Infragistics.Win.UIElementBorderStyle.Inset
Application.DoEvents()
System.Threading.Thread.Sleep(400)
pictureBox.BorderStyle = Infragistics.Win.UIElementBorderStyle.Raised
Application.DoEvents()
End Sub