How does one go about raising an event for PreviewMouseDown. I can successfully do this for MouseClick events... but falling short on the preview variation. The goal is the automate PreviewMouseDown and then PreviewMouseUp only when needed.
Cheers.
Not sure what you mean by "automate PreviewMouseDown" but PreviewXXX routed events in WPF are tunneled down the control automatically (from top of the visual tree) and then it calls the XXX event which gets bubbled up.
There is no way you can 'just' call PreviewXX event. Though you can handle them separtely by attaching to the right event.
Related
I have researched this and I have it working, but it makes no sense to me.
I have a parent panel with a PictureBox and a Label in it. I want to catch a mouse click event that occurs anywhere within the parent panel.
Here's how I expected it to work: If I click the mouse over the PictureBox or Label the system sees if they have an event handler for it. If so, their event handler gets the event, otherwise, the event automatically is forwarded to the parent panel. If the parent panel has an event handler then it gets the event, otherwise, it is forwarded to its parent. When a control handles the event, its event handler can decide if the forwarding should continue or if the event is consumed.
Here's how it seems to work: If I click the mouse over the PictureBox or Label the system sees if they have an event handler - end of story. No automatic forwarding to the parent panel.
Am I misunderstanding something? This approach seems like it puts an unnecessary burden on the programmer, having to programmatically catch and forward events. There must be an easier approach that I'm overlooking.
Lets say I have my user-control somewhere in the visual tree. Parent and children are 3rd-party controls that I cannot modify. I want to filter keyboard events in my control so that children controls do not receive some keyboard events, but the parent controls do.
I'll try to explain what I want to achieve with some diagrams. If controls do not handle keyboard events, all events bounce through the visual tree:
But, f.e. when user presses A,
Child2.OnPreviewKeyDown() should NOT be called
but Parent2.OnTextInput should still receive an event
I can achive (1) by setting e.Handled = true in MyControl.PreviewKeyDown. The problem is that in this case TextInput event is not generated:
Is there a way to achieve behavior like on the 2nd picture?
Added:
The problem I'm trying to solve is that a 3rd-party control (Child 2) steals some input in OnPreviewKeyDown (and marks event as handled), and I'm trying to avoid that.
What you can generally do in WPF to handle a suppressed event is add a handler in code and re-raise the event. To do this, you use the UIElement.AddHandler() method, for example:
child2.AddHandler(UIElement.TextInput, new TextCompositionEventHandler(nameOfYourHandlerFunction), true);
The 'true' boolean value is what makes nameOfYourHandlerFunction fire even if the Handled flag is set. The event won't automatically re-bubble by doing that, so you need to raise the event again.
base.RaiseEvent(e);
This works for events that have a routing strategy of Bubble.
I want to give the user the option to use a tutorial, the first time he uses the program. I tried adding it in the Form.Load event, but the forms shows up after the Messageboxes have popped up.
That's why I would like to know, are there any events fired right after loading a form?
If not, is there a way to perform actions right after loading?
You should try the shown event, which fires after the form is shown for the first time.
Load happens before the form is shown.
You could try using the Shown event but that might be a bit early too based on what you are doing but it does occur after the Load.
If you have any controls on the page you could trigger it off the controls GotFocus event. Just make sure to put in checks to only do it once if using the GotFocus method.
MSDN Form.Shown
MSDN Control.GotFocus
MSDN Reference to order of events
System.Windows.Forms.Control.HandleCreated
System.Windows.Forms.Control.BindingContextChanged
System.Windows.Forms.Form.Load
System.Windows.Forms.Control.VisibleChanged
System.Windows.Forms.Form.Activated
System.Windows.Forms.Form.Shown
The Shown event should do this for you.
Can you write code to tell an event not to fire? Sometimes in my applications events fire unexpectedly. I know this is a broad question, but it must have happened to others out there.
You could put an if-statement in your event handler around all the code or you could dynamically detach and reattach the event handlers from outside the event handler, if necessary.
But really, events don't just fire without reason. You probably have something else going on. Maybe you can explain more what the situation is?
There is no standard way to suppress an "unexpected" event. But you could remove all of the event handlers. This is rather drastic, but would be equivalent to suppressing the event.
Alternatively you could add code to the event handlers to do nothing when the event is fired unexpectedly. This assumes you can define "unexpected".
Rather than suppress an event, a better approach is to remove/suppress whatever is that's causing the event to fire.
I'm using a large open-source control and I can't seem to find the code that handles a double-click event. Could I perhaps have the debugger break when a double-click occurs or otherwise learn what code is associated with that event?
Well, I stumbled around until I found the code I was looking for.
BTW, before the DoubleClick event occurs, a MouseDown event occurs for the second click, with MouseEventArgs.Clicks == 2.
The event you're probably looking for is Control.MouseDoubleClick, which is raised whenever a control is double-clicked by the mouse. Or, if you're deriving from Control, you can override OnMouseDoubleClick.