-1

I have two windows. Main Window & Window1.

On Main Window, there is a button1. When it is clicked, it gets disabled and open Window1. But i want to enable button1 on Main Window when Window1 is closing or get closed.

3 Answers3

0

I guess you are using WinForms. In that case you have an event handler for the click on button1:

private void OnButton1Clicked(object sender, ...)
{
    // show window 1
}

Now there are two methods to show a Form. You can show it as a modeless dialog box or as a modal dialog box.

  • Modal dialog boxes, which require the user to respond before continuing the program
  • Modeless dialog boxes, which stay on the screen and are available for use at any time but permit other user activities

Most dialog boxes you see are Modal: If you press file save, you'll have to finish the Save-File-Dialog box before you can continue editing.

The modal dialog box is the easiest - Show them using using Form.ShowDialog. - ShowDialog returns when the form is closed.

If you use a modal dialog box your code would look sequential:

private void OnButton1Clicked(object sender, ...)
{
    using (Window1 window1 = new Window1())
    {
        // if needed window1.SetValues...
        var dlgResult = window1.ShowDialog(this);
        // if here, window 1 is closed
        if (dlgResult = DialogResult.OK)
        {   // ok button pressed
            // if needed: window1 read resulting values
        }
     }  // because of using window 1 automatically disposed
}

However if window1 is shown as a modeless dialog box, window1 will have to tell others that it is closed. Use event Form.Closed:

private Window1 window1 = null;
private void OnButton1Clicked(object sender, ...)
{
   if (this.window1 != null) return; // window1 already shown

   this.window1 = new Window1())
   this.window1.Closing += this.OnFormClosed;
}

private void OnFormClosed(object sender, FormClosedEventArgs e)
{
    Debug.WriteLine("window1 closed");
    if (this.window1.DialogResult = DialogResult.OK)
    {
        // process dialog results
    }
    this.window1.Dispose();
    this.window1 = null;
}
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
  • I don't know if that changes your answer but his question refers to WPF. – ConnorsFan Feb 26 '16 at 13:50
  • No, Its not WinForms. As i mentioned in my question. I want to do this in WPF. – Adnan Ahmed Feb 26 '16 at 13:55
  • On the other hand, setting an event handler on the Closing and/or on the Closed event of the popup window would be quite similar in WPF, I would think (http://stackoverflow.com/questions/10018308/execute-code-when-a-wpf-closes). – ConnorsFan Feb 26 '16 at 14:08
0

Create A Public Button in Window1

public Button mainBtn ; 

on mainWindow in the button click event

private void button_click(object sender , RoutedEventArgs e){
Window1 win = new Window1();
this.button.IsEnabled = false; 
win.mainBtn = this.button;
win.Show();
}

add on closing event to Window1

   private void Window_closing(object sender , CancelEventArgs e){
    mainBtn.IsEnabled = true; 
}

the idea is to pass the MainWindow Button to the Window1 Button then you can control it as like you want .

Rami ZK
  • 510
  • 3
  • 13
0

Data Binding is the strongest tool in WPF: Add the button and bind the IsEnabled property to a public property in your view model or code behind. In the secondary window - when closing - update the property to reflect the new state.

Do not forget to implement INotifyPropertyChanged

unkreativ
  • 482
  • 2
  • 8