Silverlight 4 Key Event at Control Level - c#

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);

Related

Loaded event for WebBrowser control in winforms

I need to access the below property of WebBrowser control when it is loaded completely.
webBrowser1.ActiveXInstance
this is null if the control is not loaded completely.
What property/event can I check like Form_Load?
There is WebBrowser.LoadComplete event for top-level only (the WebBrowser here I guess). If you're looking for an non top-level element, DocumentCompleted event occurs for all elements.
As ActiveXInstance is inherited from WebBrowserBase, there is also the Control.HandleCreated event, being fired when the control is displayed for the first time, though I don't know if it is applicable here.
(Sadly I'm not able to give the class and event links, but should be possible to find them yourself, right?)

Form events not fired when other control is in place

I have an application that contains a form with multiple controls.
I have subscribed to the form mouse up event. However when I click on the form if thewre is an other control placed on the form the event is not fired.
So, I would like to capture an form event on the form (even when an control is in place). Is this possible?
Thanks in advance.
As far as i know windows forms doesn't implement the concept of event bubbling. So you should manually tweak controls to handle the event. You can do it manually looping through all controls, or you can create some kind of wrapper for your form/container to subscribe to the event automatically. You may check general implementation of this idea here. .

Keydown event not firing in user control obj

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.

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.

How to force a control to not pass a click event up the tree? WPF C#

Is it possible to set up a Grid or other container control to be sort of an event dam? So that when any events, specifically a left click, that start within it are working their way up that they stop at that control and go no further?
PreviewMouseDown is your friend...
Add this event to your control, and set the Handled property on true...
All events tunnel first from root to leaves in the preview fase, then they are handled from leaves to root in the actual event case...
So PreviewMouseDown handles the Grid before the Button, while the MouseDown event handles the Button before the Grid...
hope this helps...
You should be able to extend whatever control you want (assuming it is not sealed). In your extended class you can override the click event and swallow it (do not pass it to the base class).

Categories

Resources