tags:

views:

313

answers:

2

I have Form1 in my application calling another form - Form2 using ShowDialog() method. After some user interaction Form2 calls its Hide() method. After that my application sometimes loses focus sometimes not. It could be some design mistake.

code extract:

    public class Form1 : Form
    {
      Form2 form2;

      public void SomeMethod()
      {
         if (form2==null) form2 = new Form2();
         DialogResult result = form2.ShowDialog(this);
      }

    }

    public class Form2 : Form
    {
        public Form2()
       {
           this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
       }

       void Form2_FormClosing(object sender, FormClosingEventArgs e)
       {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                Hide();
            }
       }
    }

edit: I had mistake in my code on line

 DialogResult result = form2.ShowDialog(this);

was

DialogResult result = ShowDialog(form2,this);
+1  A: 

If you hide the dialog box then Form1 will still be inaccessible as ShowDialog requires you to close it before it gives focus back.

Only handle the closing of Form2 if you intend to do something with it. Otherwise just let the dialog close there is no benefit to hiding it.

See MSDN Form.ShowDialog for more details.

Code Sample

public class Form1: Form
{
     private Form2: form2;
     private bool doDbQuery;

     public Form1()
     {
         doDbQuery = true;
     }

     public void SomeMethod()
     {
         if (form2 != null)
         {
              form2 = new Form();
         }

         if (doDbQuery)
         {
             // do DB query
             // take a note of the information you retrieve
             doDbQuery = false;
         }

         // pass this information to Form2 for it to display.
         DialogResult result = form2.Execute(...);
     }
}

public class Form2 : Form    
{        
    public Form2()       
    {
    }

    public DialogResult Execute(...)
    {
        // use the execute method to inject the data you require for the form
        return ShowDialog;
    }
}
James
In fact I had in the dialog form2 control which is connected to database and it takes some time to load it. This is reason why I am hiding it.
Michal
If you are doing a lengthy process when closing the dialog you should consider doing it on a BackgroundWorker so you don't hold up the UI.
James
I am doing lengthy process on opening the dialog and it is allways the same thing so it is not necessary to do it everytime dialog opens. This is reason why I am hiding the dialog form not just closing it.
Michal
Well in that case you should change your code. I wouldn't call ShowDialog if you intend on hiding the form as you NEED to close the dialog in order to get back to Form1 just call Show. Another alternative would be to set a flag that you can check when opening Form2 which will indicate whether it should perform your DB query or not.
James
Calling just Show works fine, but it makes parent form accesible. ShowDialog disables it until child form is closed or hidden.
Michal
Ok I think I know what it is you are trying to do but I think you are going about it the wrong way. You are using the Form as a controller when it should simply be treated for what it is....a UI. I will update my answer with an example.
James
This is what I was afraid of. My design is wrong. I will do it as you suggest. I will separate my DB operations from UI. Thanks for help.
Michal
There are other ways, this was just a way I could think of to suit your current design. However, you should never hard-code any DAL logic behind a form. Your model should be abstract from this so any changes you make don't require any changes to your View...this is known as de-coupling. You should read up on the Model View Controller pattern, it sounds like something you may want to think about for your particular scenario http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
James
A: 

if you use some "lazy" functions, you can use the asynchronous methods, at the end of what to close your form(Delegate Callbacks).

serhio