override any event of user control when window unloads in wpf - c#

I have a user control and this control is drag-dropped on a window. When this window gets closed, the user control's "Unloaded" event gets fired. I want to override this event in user control (or any other event which gets fired on window close) as i want to write some logic in it before it calls the base.Unloaded event.
The problem is there is no override for "Unloaded" event like "OnUnloaded" in user control.
Is there any other event other than "Unloaded" which gets fired when window closes and that can be overriden?

Check if the below links can help
WPF Window.Close() not triggering UserControl.Unloaded event
Disposing WPF User Controls
http://geekswithblogs.net/cskardon/archive/2008/06/23/dispose-of-a-wpf-usercontrol-ish.aspx

Related

WPF: Is this behavior intended? PreviewLostKeyboardFocus and LostKeyboardFocus

I have a TextBox and I want to save the content, when the user leaves the TextBox. I planned to use PreviewLostKeyboardFocus, but it doesn't work as intended.
<TextBox PreviewLostKeyboardFocus="textBox2_PreviewLostKeyboardFocus"
LostKeyboardFocus="textBox2_LostKeyboardFocus" />
When I click on another control inside of the same application, I first get the PreviewLostKeyboardFocus event and then the LostKeyboardFocus event. But when I activate another application, the PreviewLostKeyboardFocus event simply doesn't happen. I only get LostKeyboardFocus.
This is the expected behaviour.
The PreviewLostKeyboardFocus event is not raised when you switch to another application.
The main purpose of handling the event in the first place is to prevent the keyboard focus from changing: https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.previewlostkeyboardfocus(v=vs.110).aspx
And if the event was raised when you switch to another application, you would be able to prevent the keyboard focus from changing by handling this event and set the the Handled property of the KeyboardFocusChangedEventArgs to true and this would effectively prevent the user from being able to focus any other element on the screen while running your application.

Winforms control LostFocus event not firing when you click the form background

I have a custom ComboBox which opens a separate control when you click on the arrow of the ComboBox. I would like to call the 'LostFocus' event handler to close the custom control when you lose focus of the ComboBox.
This works fine if you click onto another control such as a text box, however doesn't fire if you click on the background of the form.
I want to mimic the functionality of when you click off and close the dropdown of a normal combobox.
Take a bool variable at Form level and make it true during combobox enter(GotFocus) event. InLeave(LostFocus)event ofcombobox, make itfalse`.
Subscribe to Form MouseClick event and check bool variable in this event. if true , call combobox leave event here.

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. .

About "Load" event

I know that in C# the Form.Load event occurs only before the form is displayed for the first time.
Is there any similar event handler (in C#) which occurs every time that the form is displayed?
Assuming that you're showing and hiding the form instead of destroying it.. then you can hook into the VisibleChanged event and perform some code when its Visible property is true.
It can be
Shown event - fired when the form is first shown
Load event - fired whenever the user loads the form
Activate event - fired each time the form is activated or receives the focus
VisibleChanged event - fired whenever the visibility changes

How do i suppress mouse wheel event of container control?

I have a silverlight page with scroll viewer in it.
Inside scorll viewer I have a user control. (user control is like a NumericUpDown control.)
I have implemented Mouse-wheel event of Text-box inside User control.
When I scroll mouse wheel on User controls user controls Mouse-wheel event gets fire but at same time my scrollviewer also moves up and down.
How do I suppress scroll viewer mouse wheel event when focus is on the user control.
In user control code register PreviewMouseWheel event and set e.Handled to true. This will stop the event propagation.

Categories

Resources