Detect if the Control was Moved inside the picturebox - c#

I have added a custom control to picturebox.I need to check if the control has been moved.
Currently i use pictureboxmousemove event,which obviously has some issues.
I have tried adding the mousemouse event to the control as well.It does not seem to work.How can i sort this issue?
pictureBox5.Controls.Clear();
var c = new FrameControl();
c.Size = obj.Size;
c.Location = obj.Location;
pictureBox5.Controls.Add(c);
UPDATE:
I have tried overriding the mousemove event of the Frame Control but it does not seem to work.
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
//Debugger does not hit breakpoint
}

MouseMove event is called when you hover mouse on the control. If you want to check whether control is moved then please use LocationChanged event.

Related

Call parent control click event c#

I developping one win-form application which having one custom control with one label and text box, and placed the custom control in one panel with docksytle as fill,
there is mouse click event for panel and custom control both, but when i click only custom control mouse click event is firing not the panel click event,
so anyone please let me know how to call the panel mouse click event.
Are you sure that you really need to invoke click of parent control? In general it would be, in my opinion, a code smell if you will do something like that - especially when it requires some strange constructions.
If you need to react in a same way when clicking on panel and on any child control inside the panel, it should be enough just to call the same method from two event handlers (that is from event handler of parent panel and event handler of child control. If you need, for example, mouse pointer location inside parent panel, you can easily calculate the position of mouse pointer using, for example, PointToScreen() and PointToClient() methods.
This is not a general solution, but maybe it's what you're looking for:
private void CustomControl_MouseClick(object sender, MouseEventArgs e)
{
panel_MouseClick(sender, e);
}
private void panel_MouseClick(object sender, MouseEventArgs e)
{
}
Create Click Event for each control in panel and invoke the parent :
private void This_Click(object sender, EventArgs e)
{
this.InvokeOnClick(this, null);
}

How to capture mouse wheel on panel?

How to capture mouse wheel on panel in C#?
I'm using WinForms
EDIT:
I try to do it on PictureBox now.
My code:
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("Click");
}
Clicking works. Wheelling doesn't.
Why?
If you can't see the "MouseWheel" event on a component, then you need to create it manually. Also, we need to focus that component, otherwise the "MouseWheel" event will not work for that component. I will show you how to create a "MouseWheel" event for "pictureBox1" and how it works.
INSIDE THE CONSTRUCTOR, create a mousewheel event on that component.
InitializeComponent();
this.pictureBox1.MouseWheel += pictureBox1_MouseWheel;
CREATE THE FUNCTION manually. According to my example, call it "pictureBox1_MouseWheel"
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
//you can do anything here
}
CREATE a MouseHover event on that component (Go to properties in PicureBox1, select event, locate "MouseHover" and double-click the "MouseHover" event).
CALL "Focus()"; method inside that MouseHover event.
pictureBox1.Focus();
Now run the program.
Windows sends the WM_MOUSEWHEEL message to the control that has the focus. That won't be Panel, it is not a control that can get the focus. As soon as you put a control on the panel, say a button, then the button gets the focus and the message.
The button however has no use for the message, it's got nothing to scroll. Windows notices this and sends the message to the parent. That's the panel, now it will scroll.
You'll find code for a custom panel that can get the focus in this answer.
UPDATE: note that this behavior has changed in Windows 10. The new "Scroll inactive windows when I hover over them" option is turned on by default. The makes the mouse wheel behavior more consistent with the way it works in a browser or, say, an Office program. In this specific case the picturebox now will get the event. Watch out for this.
To wire it up manually...
this.panel1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseWheel);
private void panel1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
///process mouse event
}
Easier method is in visual studio click on panel, goto properties viewpanel, select events, locate and double click the "mousewheel" event.
In Winforms, this is achieved using the Control.MouseWheel event
Getting mousewheel events is tricky. The easiest way is using
this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseWheel);
instead of
this.panel1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseWheel);
This way the form gets the event instead of control. This way is easy but has one problem: you can use only one mousewheel event in your form.
If you have more than one control to get mousewheel event the best way is This answer by "Shehan Silva - weltZ"

MouseEnter and MouseLeave events from a Panel and its child controls

I have a Panel that contains child controls.
If I handle the Panel's MouseEnter and MouseLeave events, and its child's MouseEnter and MouseLeave events, the events are raised in this order:
Panel.MouseEnter
Panel.MouseLeave
Child1.MouseEnter
Child1.MouseLeave
Panel.MouseEnter
Panel.MouseLeave
But I need the following order:
Panel.MouseEnter
Child1.MouseEnter
Child1.MouseLeave
Panel.MouseLeave
Is that possible?
If you dont mind creating a usercontrol(derived from the panel or other parent container you wish),
Override your parent's OnMouseLeave method to look like the following..
protected override void OnMouseLeave(EventArgs e)
{
if(this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
return;
else
{
base.OnMouseLeave(e);
}
}
Then, the event raising will be in the required order.
The mouse is "leaving" the panel as it enters the child control which is why it fires the event.
You could add something along the following lines in the panel MouseLeave event handler:
// Check if really leaving the panel
if (Cursor.Position.X < Location.X ||
Cursor.Position.Y < Location.Y ||
Cursor.Position.X > Location.X + Width - 1 ||
Cursor.Position.Y > Location.Y + Height - 1)
{
// Do the panel mouse leave code
}
The solution is to track the number of enters/leaves.
In you overall control add a counter:
private int mouseEnterCount = 0;
In the MouseEnter handler do this:
if (++mouseEnterCount == 1)
{
// do whatever needs to be done when it first enters the control.
}
In the MouseLeave handler do this:
if (--mouseEnterCount == 0)
{
// do whatever needs to be done when it finally leaves the control.
}
and do the above MouseEnter and MouseLeave event handlers for ALL the child controls as well as the containing object.
Matthew's answer will not work always. Especially if the child control is set to the edge of its container and the mouse moves off the controls in that direction. You will never detect the MouseLeave event.
The best approach is to create a user control container then hook all the child controls' MouseEnter and MouseLeave events so that you can properly detect when and where the mouse is at all times. THEN if it enters your container's bounds you can fire a custom MouseEnter event and when it leaves MouseLeave event.
Jimmy T. is right. There will be problems if there is no (or small) space betwean Parent Control (Panel) edge and Child Control.
This is how I solve this problem in UserControl-derived class:
public CSStackPanelItem()
{
InitializeComponent();
MouseEnter += new EventHandler(CSStackPanelItem_MouseEnter);
foreach (Control child in Controls)
{
child.MouseEnter += (s, e) => CSStackPanelItem_MouseEnter(s, e);
child.MouseLeave += (s, e) => OnMouseLeave(e);
}
}
protected override void OnMouseLeave(EventArgs e)
{
if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
return; //suppress mouse leave event handling
if (m_bIsHover)
{
m_bIsHover = false;
Invalidate(); //actually my mouse Enter/Leave event
}
base.OnMouseLeave(e);
}
void CSStackPanelItem_MouseEnter(object sender, EventArgs e)
{
m_bIsHover = true;
Invalidate(); //actually my mouse Enter/Leave event
}
This may not be the most elegant solution, but you could set a property in the parent control panel (subclass panel) that is a bool value like "bool selected". Then when the MouseEnter for the panel fires set it to true...then stop the mouseleave logic from firing unless it is set to false.
example
bool selected;
MouseEnter(..,..)
{
if (!selected)
selected = true;
else
selected = false;
if (selected)
/.. Logic Here ../
}
MouseLeave()
{
if (selected)
return;
/.. Logic Here ../
}
In reality I would just have the MouseLeave event of the child set the parameter.
Example:
Parent:
bool doLeave;
MouseLeave(..,..)
{
if (doLeave)
{
/.. Logic ../
doLeave = false;
}
Child:
MouseLeave(..., ...)
{
DerivedPanel parent = this.Parent as DerivedPanel;
if (parent != null)
parent.doLeave = true;
}
Neither are elegant but it will work.
I believe so. A nice tool to have for verifying your WinForms application's events.
Windows.Forms Order of Events
http://missico.spaces.live.com/blog/cns!7178D2C79BA0A7E3!186.entry
Created with EventSpy written by Urs Eichmann. (ftp://missico.net/EventSpy.zip)
Using .NET Framework 3.5 and with Visual Basic’s Application Framework enabled.
This is a tricky one, and will be difficult to code reliably for. One idea is to "capture" the 3 incoming events in a list and execute your desired code once the list is complete (has the 3 desired events in the list). Then when you're done executing whatever code (or perhaps capture the combo of events in reverse), you could empty your list and have it ready for the next time that particular combo-event happens. Not ideal, but just a thought.
Of course, that doesn't overcome the potential resolution issues & possible missed events Hans raised. Perhaps more context is in order.
Check the child component..
if (panel1.GetChildAtPoint(panel1.PointToClient(Cursor.Position)) == null)
{
// mouse leave panel and children
}
My solution was to create a MouseEnter event for the panel and for the parent form of that panel. I don't tie to any MouseLeaving events.
When the cursor enters the panel, MouseEnter fires. I can visit all of the panel child controls and nothing happens (which is what I want). When I leave the panel, the parent form's MouseEnter fires.
I was looking for a solution to this problem so I could make a panel act like a button (ie change color when the user hovers over it). I found the simplest solution was to put another panel over the base panel (and over all it's controls) and make the new panel's BackColr Transparent (highlight the whole BackColr property and type in "Transparent"). Then add the MouseEnter and MouseLeave events to the new panel and make them perform whatever you want (change the background color of the base panel). The user still sees all the controls that are on the base panel but the base panel's mouse leave and mouse enter don't fire as the user moves the mouse over the controls. The user can't interact with the controls on the base panel but for a simple button type control this works OK. Just add a the mouse click event handler to the new panel.

Correct usage of OnClick vs. MouseClick events in Windows Forms applications using C#

I'm currently developing a custom control and realize that my code is being run twice. It is not really a huge issue (it is only a Focus method call). However, I would like to understand it.
From reading the MSDN description for click | onclick event, it states that:
Fires when the user clicks the left mouse button on the object.
So I added the OnClick event and the MouseClick events to handle both left and right clicking. But after debugging the code I found that the OnClick handles both left and right click events.
Why is OnClick handling both and do I need to keep both events in my code for some reason I'm overlooking?
protected override void OnClick(EventArgs e)
{
this.Focus();
base.OnClick(e);
}
private void CustomControl_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
rightClickMenu(e);
}
}
According to MSDN, the Click event is called not only when the mouse is clicked, but also when the Enter button is pressed. If you only need to handle mouse clicks, I'd move all of your code in the MouseClick event. You can't do it the other way around because the Click event doesn't tell you which mouse button (if any) was clicked.
First of all, your link is incorrect, it links to HTML and DHTML Reference, not WinForms :)
Correct link is Control.MouseClick event
You need to override only one method. If you want to handle only mouse clicks - override OnMouseClick() and don't handle MouseClick event, otherwise - override OnClick() and don't override OnMouseClick().
You shouldn't need to have both events... Just keep the OnClick.
Also, I haven't done Windows Forms in quite a while, but I think there's a better way to accept focus than manually setting it on the click event, but I can't tell you specifically what it is... I think there's a property for it or something.
In Winforms, the Click event is raised when either mouse key is clicked.
If my memory serves me right, click does both mouseclick and the 'Enter' key or even setting focus on the control using the 'Tab' key and then using 'Space' or 'Enter' to "click" it.
If such behaviour is acceptable/desired, you may do the following.
I had this workaround for a DoubleClick event...
void ControlClick(object sender, EventArgs e)
{
MouseEventArgs mEvt=e as MouseEventArgs; // or (MouseEventArgs)e;
// now mEvt has the same properties as 'e' in MouseClick event
}
Hope this helps.
-Nurchi
The OnClick and CustomControl_MouseClick is the same event
You can have how many methods you want attached to an event ( this.Click += ...)

MouseUp event interrupted?

I'm making a custom control with a panel. I want to be able to drag and drop it so I've implemented that in the MouseDown event of my control. But I want the thing to react when you start drag to give a little feedback to the user. So in the MouseDown even I change the color. Then I want to change it back in the MouseUp event.
My control is not installed into VS2008 but just a class I've written that I instanciate at run time (I don't know in advance how many I need and so on). Now, my control exposes a MouseDown event so as to be able to be dragged. When I subscribe to this event from the parent application to actually perform the drag and drop my control is not repainted on its MouseUp event! In fact, the MouseUp is never invoked. If, on the other hand, I don't subscribe to the event in the parent app it works as intended.
What's going on? Is the parent interrupting the flow so that the MouseUp event never fires in my control? How do I get around this?
I'm not sure if you are using Windows Forms or WPF, but in Windows forms here is what I mean:
public class DerivedPanel : Panel
{
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
Capture = true;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
Capture = false;
// Change your color or whatever here
}
}
In WPF there are two methods, CaptureMouse() and ReleaseMouseCapture() to do the same thing. When the control captures the mouse, it will received mouse events even if the cursor isn't over the control. This could be causing your problem. See MSDN Article
Do you capture the mouse in the custom control on the mousedown event? Try capturing on the mousedown and releasing the capture on the mouseup.

Categories