Keydown event not firing in user control obj - c#

I have a user control which is not declared in the designer. I have a button that I want to have create this user control when I click it - it should initialize the user control and insert it in the main UI.
However, it happens that the user control has a key press event on it, which is not firing.
Why does this happen?
I already tried to attach the event on the user control itself but it seems that it's not firing at all. Is there some kind of bug?

It is very hard to fix problems with code that you can't see, but in WPF, there are often reasons why Bubbling events like the KeyDown event don't fire. Occasionally certain controls may make use of these events and set them as handled internally, thereby stopping them from bubbling up any further.
The normal solution on these occasions is to use the related Tunneling events instead, which are raised before the Bubbling methods and not used internally by controls. So, while I can't guarantee that this will fix your problem, it is certainly worth trying to handle the UIElement.PreviewKeyUp event instead of the UIElement.KeyUp event.

Related

Why does the DragLeave Event not use the DragEventHandler?

I only noticed this because I mistakenly tried to attach a DragEventHandler to a DragLeave event. I was surprised to see that the DragLeave Event was just as a general EventHandler. This is odd to me because both the DragDrop Event and the DragEnter Event use DragEventHandlers. Is there a reason or explanation for this?
Resources:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragleave http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragenter http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragdrop
Yes. When handling DragEnter or DragDrop, we usually require or update additional information (such as the current drag effect, the active modifier keys, or the actual data involved). Therefore, the event argument passed to the handler has to provide access to that information.
On the other hand, there is not much we can (or should) do on DragLeave. We cannot cancel a leave. We cannot change the drag engine's behavior or the mouse pointer's shape because it's, well, a leave, the system's way of telling us thank you for your cooperation, we're done now, maybe we'll get in touch with you again in the future.

Silverlight 4 Key Event at Control Level

I have a Silverlight control (ChildWindow) that I want to receive all key down events. The problem is that if I simply say
this.KeyDown += new KeyEventHandler(EventDetailsPopup_UC_KeyDown)
it won't work because the event is routed to all of its child controls. How do I receive Key or Mouse events from the UserControl or ChildWindow level? Thanks
You do not describe what you mean by "it won't work", but I can at least give you some background.
Silverlight supports the concept of Routed Events. When a routed event is fired on a child control, it is passes up the logical tree, firing on the control's parent, then the parent's parent etc ... until the root visual is met. The list of routed events is detailed on this MSDN page. This is called bubbling.
It looks like you want to prevent a child control from seeing this event? i.e. you want to cancel it by setting it as handled. Unfortunately this is not possible because the child control will always receive the event first. To support this you require a feature called tunneling, where a 'preview' event first tunnels from parent to child before the bubbling event is fired. This is a WPF-specific feature as described in the MSDN page referenced above.
Or if "wont work" means that you are not getting the event.
It may be beacause some child element has setted Handled parameter of eventarguments to true.
But you can still register for listening handled events in code lets say in constructor of your childwindow:
this.AddHandler(KeyDownEvent, (KeyEventHandler)YourHandlerFunction, true);

DoDragDrop disables MouseMove Events

After having started a Drag & Drop operation by DragDrop.DoDragDrop(...) no more MouseMove Events are fired. I even tried
AddHandler(Window.MouseMoveEvent, new MouseEventHandler(myControl_MouseMove), true);
where the last parameter means I even opt in for handled events. No chance, seems like the MouseMove Event is never fired at all! Any way to still get MouseMove Events while using Drag & Drop? I'd like to Drag & Drop a control, while dragging this control it shall follow the mouse pointer. Any idea how to do this in this case?
You need to handle the DragOver event.
EDIT: Try handling the GiveFeedback event on the control that you called DoDragDrop on; that might do what you're looking for.
What is the DragDrop.DoDragDrop construction? DoDragDrop is intended to be called in MouseDown/MouseMove method indeed, not in DragDrop. It is supposed to START handling the procedure, not to react for the drop (the desired effect of the drop you just implement directly in DragDrop). MouseMove never fires while already dragging, perhaps that's why it does not also fire with you, since you set the procedure. I think you handle this wrong way, here is one of examples http://msdn.microsoft.com/en-us/library/aa984430%28v=vs.71%29.aspx.

Global PreviewKeyDown handler vs local PreviewKeyDown handler

I have a PreviewKeyDown handler on my mainwindow which handles up and down keys so I can navigate with the keyboard between my controls.
Now I have the problem that in some Textboxes I also want to use the up/down keys. This seems impossible because the other handler seems to swallow the keys first.
Is it possible that when one of these TextBox controls are focused they get the up/down keys first and then then swallow them so that the "global" PreviewKeyDown does not get them?
Sure I could disable the global handler somehow when such a TextBox got focus but is this good style?
You don't really have an option, aside from filtering out those keys in the global key handler.
The reason that you're having this problem is that all of the Preview* events are tunneling, meaning that controls higher in the visual tree get them first (starting at the root). The very reason why you're using this event in the first place is causing your problem.
One less than ideal option would be to register a class handler for TextBox.PreviewKeyDown (see EventManager.RegisterClassHandler()). While this would be called before your window's PreviewKeyDown handler, it will be called for all TextBoxes in your application. This may or not be what you want.

How To Make A Web User Control's Child Control's Events Cause A Catchall Event To Fire On The Parent Once Per Postback

Say that I have a web user control that has several drop down lists in it. They are all set to AutoPostBack = true, BUT each SelectedIndexChanged event handler in my control will fire/chain the other SelectedIndexChanged handlers I have defined for the other DDLs. This means that when the user changes a single DDL, the event handlers are chained/fired for several other DDLs. The logic for which events are chained is very complicated, data driven, and can change depending on which list was actually changed by the user. Therefore, it is very difficult to determine which event handler would fire last.
From the page's point of view, I want to subscribe to a single SelectionChanged Event on the user control that will only fire one time per postback and not until all of the event handlers have fired. I don't care which event handlers have fired, only that the state of the control as a whole has changed.
I'm using C# 3.5/ASP.NET 2.0/VisualStudio 2008
How can I go about doing this?
EDIT: Moved clarification into original description. I think the fact that I specified AutoPostBack=true without specifying that chaining was happening was misleading. I apologize for the confusion.
It depends on when you need the event handler to fire in the page lifecycle.
Here's one strategy:
1) In your user control, track the selection changing of your dropdown lists. If the event handler is executed, update your local tracking variables.
2) In your usercontrol's PreRender handler, check your tracking variables and if called for, fire the user control's SelectionChangedEvent.
This strategy will guarantee that the event handling phase of the page lifecycle is done, but has the drawback that your main page won't receive the "SelectionChanged" on your user control until the PreRender phase. This may or may not work for your situation.
If you need to handle the SelectionChanged event for your usercontrol earlier, then you will likely have to put in more complicated tracking logic in your dropdownlist handlers, and add a tracking variable to ensure that the usercontrol's "SelectionChanged" event only ever gets fired once.
I think you need to create a delgate in child control and then reference that delegate into parent control.

Categories

Resources