简体   繁体   中英

Enable a disabled button

I have a program with two windows form. The first 'form 1' has a button which opens 'form 2'. When 'form 2' is opened the button in 'form 1' is disabled, however I want to enable the button again if 'form 2' is closed. I can't figure out how to do this. Can anyone help?

Subscribe to Form2's Closed event. Here is an example.

public class Form1 {

   public Form1() {
      InitializeComponent();
   }

   // SomeButton is Clicked
   public void SomeButton_Click(object sender, EventArgs e) {
      // SomeButton is disabled
      SomeButton.Enabled = false;
      // Form2 is created
      var form2 = new Form2();
      // Subscribing to Form2's Closed event
      form2.Closed += OnClosed;
   }

    private void OnClosed(object sender, EventArgs eventArgs)
    {
        // Event is fired and you can enable the button
        SomeButton.Enabled = true;
    }

}

Subscribe to Closed event on Form2 and enable the button.

Place below logic next to Form2 instance creation.

form2.Closed += (s,ev) => this.button1.Enabled = true; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM