C# Manually triggered MouseDown fails to trigger MouseUp - c#

Let's say I have a created a Form class and a CustomControl class.
On my Form I have two instances of the CustomControl, and a Panel.
Panel has 4 event handlers: MouseEnter (to give a different cursor), MouseLeave (to reset the cursor), MouseDown (to start the dragging thread), and MouseUp (to kill the dragging thread and do post-drag logic).
I can drag the Panel onto the CustomControl. When I do this, the code in Form detects what I have done and deletes Panel from Form.Controls, passes some meta-information to CustomControl, which then creates a Panel on itself.
Basically, it is a hand-over. The Panel object now belongs to the CustomControl.
(This is necessary. It's complicated to explain why, but imagine the custom control has something like scrollbars, and it's necessary for the Panel to belong to the CustomControl so that it will scroll with the CustomControl.)
Now, when I click down on the Panel in the CustomControl, the Panel's MouseDown is triggered, it gets deleted from CustomControl.Controls and sends some meta-information back to the Parent (the Form), which then re-creates the Panel as it was at the start - however already in a dragging state so that the user can re-position the Panel onto the second CustomControl, or perhaps put it back onto the Form. The function which creates the Panel when the Form is first initialised is exactly the same function which creates it now.
However, the Panel's MouseDown has not been triggered. The mouse is down, but the event is not firing because the mouse was already down when it was created. So, I manually call the MouseDown handler in the function in Form which accepts the meta-information from CustomControl.
Unfortunately, this only half-works. The MouseUp handler isn't firing. I can pick up the Panel off the CustomControl and drag it around on the Form as expected, but when I release the mouse, the Panel is stuck to the cursor.
I'm not really sure how to get around this?
An ideal solution would be for, when the meta-information is passed back to Form and the new Panel is created, the MouseDown event to somehow fire naturally as though the user had just clicked down on the Panel.

It sounds like you are creating a new instance of Panel when you move it from CustomControl to Form and back and loosing it's state.
You should either try to pass the actual instance owned by Form to CustomControl without creating a new one or you could capture the state of the Panel in another object which you can pass to the constructor when you create a new Panel so that it is in the same state as the one you were dragging?
It seems as though you are trying to manually fire mouse events to compensate for problems in your design.
Always better to give some example code if you can than lengthy textual explanations.

Look at this
Instant of custom control is disappear when click outside it
I have problem like you.
you shouldn't use a local variable for handling mouseEvent.
Try to use "Capture" function. It's work for me.

Related

How to detect when the visual tree is destroyed?

In a WPF application I have, I have a control that is central to the application. This control, called ArtView, is visible for the entire lifetime of the main window. It performs hardware accelerated rendering, and in order to avoid bogging down the UI thread, I set up an event loop in the OnLoaded handler for this control. Events such as mouse clicks, keyboard input, and scrolling are added to a queue that is then consumed by the event loop, but the problem I have is that I do not know how to end the event loop when the window is closed. When I close the window that contains the control, OnUnloaded does not fire, so what is the best way to disable the event loop when the control is no longer visible?
I am aware that I can do something like Window.GetWindow(this).Closing += OnWindowClosing; but that feels like a hack.
dymanoid's solution of subscribing to IsVisibleChanged works flawlessly, and provides a more satisfying solution than depending on the Window for the cleanup of my control.

C# TreeView disable scrolling in UserControl [duplicate]

I have a little problem with winforms and mousewheel events.
I have a custom user control representing a slider. Now, I have a couple groups of sliders in which each group is wrapped inside a panel. All the groups are then wrapped in another panel (which has AutoScroll set to true) and this is wrapped in a form. The slider logic is implemented such that the mousewheel can be used to change its value. For this, the slider user control gets focus when the mouse is over the slider. However, when I scroll, also the AutoScroll parent panel scrolls with it.
I've already lost a lot of time on this issue. Anybody knows what is happening here and how I can solve it? I thought the event was bubbling to the parent panel but I don't find a Handled property on the event when handling it in the Slider control (as is possible with WPF).
many thanks
We implemented the Slider as a complete custom user control (inheriting the UserControl class) with own look-and-feel.
You might have noticed that a UserControl doesn't show the MouseWheel event in the Properties window. Hint of trouble there. The WM_MOUSEWHEEL message bubbles. If the control that has the focus doesn't handle it then Windows passes it on to its Parent. Repeatedly, until it finds a parent window that wants to handle it. The Panel in your case.
You'll need to invoke a bit of black magic in your slider control. The actual event args object that get passed to the MouseWheel event is not of the MouseEventArgs type as the event signature suggests, it is HandledMouseEventArgs. Which lets you stop the bubbling. Like this:
protected override void OnMouseWheel(MouseEventArgs e) {
base.OnMouseWheel(e);
// do the slider scrolling
//..
((HandledMouseEventArgs)e).Handled = true;
}
If you are creating event dynamically like
object.event += new EventHandler<EventArgs>(eventfunction);
try un-registering the event after the eventfunction is called like this
object.event -= new EventHandler<EventArgs>(eventfunction);

Remove original event behaviour of WinForm Control

I would like to remove the original event behavior of controls within a form (similar to design mode).
So, when the user clicks on the button, i only want to capture that event. I do not want the original button event to be fired. Is this somehow possible?
I am looking for a generic solution. So it should work with any form and any control within the form.
Reason: I wrote a form validation rules designer. It uses reflection to enumerate all form-types in the entry assembly. The user can then select a form type, the designer creates that form, enumerates the controls, and embedds the form in the designer panel.
clicking on a control, opens a formular designer panel, and the user can now create a formular for that control and saves the formular to a DB.
When the form is then opened in the normal "runtime" mode, it loads its validation formulars.
Events are not in fact disabled in the Winforms designer. The designer executes the constructor of the form through Reflection, everything in the InitializeComponent() method executes, including the event subscriptions. Wherever this might cause a problem, the controls check the DesignMode property (prevents a Timer from starting for example) or by custom designers. The form is displayed underneath a transparent layered window on top of which the selection rectangle and drag handles are painted. Which prevents issues with mouse clicks and keyboard focus.
You probably ought to look at this magazine article to get this working for you.
From what I understand from your question, I guess, you can still use the "DesignMode" property for this as well. In your event handling routine, you may want to bypass execution by checking on this property:
if (this.DesignMode) return;
as the first statement in your event handling block of code.

C# WinForms dragging controls with mouse

I'm making a calendar From in C# using WinForms.
I've put it together using a two-dimensional array of Panels, and inside them I have a List<> of custom controls which represent appointments.
The user needs to be able to drag appointment controls from one Panel to another (from day to day).
The custom control has a MouseDown and MouseUp event, which passes a message up from the control to the Parent.Parent (custom control -> day panel -> calendar form) and calls public methods StartDragging() and StopDragging() respectively.
Inside these methods, I make a clone of the custom control and add it to the Form, and store it in a global variable in the form which is called DraggedControl.
The Form has an event handler for MouseMove which goes like this:
void Calendar_MouseMove(object sender, MouseEventArgs e)
{
if (DraggedControl == null)
return;
DraggedControl.Location = PointToClient(MousePosition);
Refresh();
}
There are two problems, however:
First of all, the custom control is under everything else. I can see it being added and removed on MouseDown and MouseUp, but it is being added at 0,0 under the panels and day Labels.
Secondly, it does not appear to be moving with the MouseMove at all. I have a feeling this might be because I am moving the mouse with the button pressed, and this would represent a drag action rather than a basic MouseMove.
If I remove the MouseUp code, the control does drag with the mouse, however as soon as the mouse enters the panels (which the control is, sadly, underneath), the drag action stops.
What would you suggest I do?
I suspect there is probably a better way to do what I am trying to do.
custom control is under everything
else
bring it on top:
DraggedControl.BringToFront();
it does not appear to be moving with
the MouseMove at all
Control, which handled MouseDown event, captures mouse input and receives all following MouseMove events until it releases mouse input on MouseUp event, that's why Calendar_MouseMove() is not called. Handle MouseMove event for the same control, which generated MouseDown event.

How to tell when the user has clicked outside of the bounds your control?

I have a custom UserControl. I want to use it in a few different products, so I want something that can be implemented inside of the UserControl itself. I want to know when the user has clicked outside of the bounds of the UserControl so that I can hide it, similar to a ComboBox. How can I do that?
I tried handling the click event, but it only seems to fire if the click occured within the bounds of the control.
That's what the Capture property is designed to do. Set it to true and all mouse messages are routed to your control, even if it moves out of the window bounds. Check the e.Location property in the MouseDown event.
Hm, you may be able to accomplish what you want by listening to the GotFocus/LostFocus events. ComboBoxes give the drop downs focus when they open and close them when they lose focus.
do this
Select all controls on your form including form
In Property Window select MouseClick event
Now enter below Code in Common_MouseClick
Code:
if (!sender.Equals(yourControl))
{
yourControl.Visible=false;
}

Categories

Resources