Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am trying to open a form with a button click. I am constantly looking to see if that form is opened. But when it closes, I want to refresh the original form or the "parent form". Need Help! When I click the button, I created an infinite loop which form5 will never load!. How can I do this properly?

WebBrowserForm form = new WebBrowserForm();
form.Show();
while (IsFormAlreadyOpen(typeof(Form5)))
{
}
refresh();

public Boolean IsFormAlreadyOpen(Type FormType){
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == FormType)
                    return true;
            }
            return false;


UPDATE:

I mean that my original form(1) has a button that opens another form(2). When form(2) closes, I want an update happen on form(1)
Posted
Updated 27-Jun-11 7:05am
v3
Comments
Sergey Alexandrovich Kryukov 28-Jun-11 0:48am    
The problem looks really simple (and this code quite inappropriate, apparently dangerous, ineffective, etc.), but to offer a solution I need to know the goal of this activity. A hint: it's much better to never work with "non-opened" forms. All forms should be opened but some just invisible. Closing event should hide a form instead of closing (except main one). Even better -- use only one form; what was other forms should go to controls, such as tabs.
--SA

Well, this is certainly an interesting approach, but not one I'd recommend. You are calling a function in a tight loop on the UI thread - so, how can processing occur in Form5 (please rename your forms to be meaningful, what happens later on when you are trying to remember what form does what?)

Anyhoo, a better approach would be to create an event in Form5, and subscribe to it in the calling form. Raise this event when your form is closing and then call refresh in your event handler.
 
Share this answer
 
There are two types of forms modal and modeless. As long as a modal form is open, the form that opened it (if it's in the same apploication) cannot be used, so there's no point in checking to see if the 2nd form is already open.

If the 2nd form is modeless, you simply need to see if it's already created, and if so, show it. When you're done with it, hide it or dispose it.

In either case, you can add an event handler for the 2nd form in your first form so that it can respon appropriately when the 2nd form closes.

Now, what's your issue?
 
Share this answer
 
v2
Hello
you can do this

1 . in parent form for example "FrmMain" , create a memebr of type "Form5" for Example form5;
2 each time that you need to open this form do this
if(this.form5 ==null)
{
  this.form5 = new Form5();
  form5.FormClosing -= new FormClosingEventHandler(form5_FormClosing);
  form5.FormClosing += new FormClosingEventHandler(form5_FormClosing);
}
form5.Show();

and in this event method do each work that you want for example "Refresh" current form
C#
private void form5_FormClosing(object sender, FormClosingEventArgs e)
{
  this.Refresh();
  this.form5 = null;
}

if this is not enough for you problem it can be more complex m for example use from Show() and Hide() methods
 
Share this answer
 
v2
Comments
Pete O'Hanlon 27-Jun-11 12:58pm    
You have the event deallocation in the wrong place. It should be in Form1_FormClosing, not just after you've created a new instance of Form5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900