Marshal Marshal - 1 month ago 7
C# Question

Event handling (Detect when hiding form)

Well normally I am quite good at figuring and researching problems without guidance however I have come across a snag. I am trying to create an "Event" with C# (which I have not done before) everything I have looked up has nothing to do with what I need.

I am trying to call a class on my main form when form2 is hidden. I found some code which was supposed to check to see if form2 closed - Either I didn't integrate it into my code properly or closing is different to hiding.

So just to clarify I want to run through the program like this:


  • Form1 runs

  • Click setting button on Form1 which opens Form2

  • Form2 opens where settings can be changed

  • Click the "Ok" button on Form2 (here is where I want Form1 to realise Form2 has been hidden

  • Hide form one and run a class called Refresh which refreshes button names and URLs


Answer

Open the second form as modal

Form2 form2 = new Form2();
DialogResult result = form2.ShowDialog();

check the result and refresh:

if (result == DialogResult.OK)
    Refresh();

What you also need to do in this case is when closing the form set DialogResult of the form, for example if you have an OK button, in the button handler set:

this.DialogResult = DialogResult.OK;

This will automatically close the form as well as I remember correctly.

What you can also do is set DialogResult.Cancel on cancel button if you need that.

Comments