Search code examples
c#silverlightchildwindow

Close previous window from current window


In Silverlight I am using a profile window, with the option to delete the current profile in a hyperlink button. When the user hits the hyperlink button it takes them to a new form to confirm the deletion. Although the delete function has worked (i.e. the profile has been deleted form the datatbase), how can I get the first window to close when the user confirms?

This is what the hyperlink calls-

private void deleteProfile(object sender, RoutedEventArgs e)
    {

        deleteProfile dProfile = new deleteProfile();
        dProfile.textBlock1.Text = System.Convert.ToString("Delete " + lblMsg.Content);
        dProfile.Show();
    }

Then from there on dProfile I want to close the profileForm that the hyperlink sits on when the user clicks ok-

private void OKButton_Click(object sender, RoutedEventArgs e)
    {

        textBlock1.Text = "Profile Deleted.";
        profileForm.Close();

        Close();
    }

However the dProfile form only recognises profileForm when I create a new instance of it, how can I reference the current one I am using?


Solution

  • There may be some other way, but you can try the following.

    Create an event in Child Window

    public event EventHandler SubmitClicked;
    

    in your OKButton_Click event

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

    Do the following in the main window, attach event against the ChildWindow object

    deleteProfile.SubmitClicked += new EventHandler(deleteProfile _SubmitClicked);
    

    Then in the event you could do:

    private void deleteProfile_SubmitClicked(object sender, EventArgs e)
    {
        this.Close();
    }
    

    (Although its not required here, but you can use the process to pass values from Child window to parent window) Also check out this article