Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fire a custom event from a User Control?

I have a very interesting task that I need help with. The description is as follows:

I have a user control (SomeUserControl), that I am using in a Main Window. I write my application entirely on SomeUserControl, and Main Window is hosting SomeUserControl and nothing else.

Right now I have a shutdown button (in SomeUserControl), that is supposed to close the Main Window. The reason for this is that I do not want anything from SomeUserControl to close the application itself, but to fire an event from SomeUserControl, and Main Window receives it, and Main Window will close the application instead of SomeUserControl.

How do I do it? I am not familiar with the concept of creating and handling custom events, so if someone could explain it in words and in code as an example, I will be very grateful to you!

Edit: Here's my code so far.

(in Window 2)

Public Event CloseApp As EventHandler

Private Sub CancelButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles CancelButton.Click
    DialogResult = False
    RaiseEvent CloseApp(Me, New EventArgs)
End Sub

(In Main Window) Public loginPage As New LoginPage

Public Sub New()
    InitializeComponent()
    AddHandler loginPage.CloseApp, AddressOf Me.ShutDownJobChangeWindow
End Sub

Private Sub ShutDownJobChangeWindow(ByVal sender As Object, ByVal e As EventArgs)
    Application.Current.Shutdown()
End Sub

Goal: I want to close the application when I click cancel in Window 2, but I don't want to do it in such a way that Window 2 closes itself, but by sending some notification to Main Window, and Main Window closes the application.

like image 207
will0809 Avatar asked Feb 10 '12 10:02

will0809


2 Answers

The simplest way to do this is to declare a custom event:

public delegate void ShutdownEventHandler (object sender,  EventArgs data);
public event ShutdownEventHandler Shutdown;
protected void OnShutdown(EventArgs args)
{
    if(Shutdown != null)
        Shutdown(this, args);
}

Then you subscribe to the Shutdown event in MainWindow, and handle shutdown logic there. And in SomeUserControl, you simply run OnShutdown when you want to shutdown your application.

like image 75
AkselK Avatar answered Sep 20 '22 04:09

AkselK


If the logic of the user control is implemented in the "code behind" of the user control class, do the following.

I'm assuming the XAML file has somewhere a button with a click event:

<Button Click="Button_Click">Close App</Button>

Then, in your code behind for SomeUserControl class, do the following:

public partial class SomeUserControl : UserControl
{
    public event EventHandler CloseApp;

    ...

    private void Button_Click( object sender, RoutedEventArgs e )
    {
        if( CloseApp != null ) {
            CloseApp( this, new EventArgs( ) );
        }
    }
}

In your Main window, listen to the event:

...

someUserCtrl.CloseApp += new EventHandler( MyFn );

private void MyFn( object sender, object EventArgs e ) {
    ...
}
like image 44
Uri Avatar answered Sep 23 '22 04:09

Uri