views:

91

answers:

2

Hello,

I have a from "fm" that is a simple info window that opens every 10 mints (fm.Show();) .

how I can make that every 10 mints it will check if the the form "fm" is open and if it is open it closes it and open it again!

now the from fm is always created with form fm = new form(); so when I try to check if the form is open it will always be false and open a new window even if there is one from before!

I need to have a tool to give it a unique identity and then check if this form with unique identity open or not!

the reason why I do not want to jut update the data on the form (fm) because it is a complicated info with buttons that is why

the form name is "UpdateWindow"

Thanks

+3  A: 

Hi,

maybe this helps:

FormCollection fc = Application.OpenForms;

foreach (Form frm in fc)
{
//iterate through
}

Some code in the foreach to detect the specific form and it could be done. Untested though.

Found on http://bytes.com/topic/c-sharp/answers/591308-iterating-all-open-forms

Sascha
Thanks, so how to use it with my form (fm) ?
Data-Base
if (frm is MyForm) { /* */ }, should do the trick. But if it's just for updating, why not calling a method to update the data?
Sascha
I solve it with this .... Form fc = Application.OpenForms["UpdateWindow"]; if (fc != null) fc.Close(); fm.Show();
Data-Base
+1  A: 

I'm not sure that I understand the statement. Hope this helps. If you want to operate with only one instance of this form you should prevent Form.Dispose call on user close. In order to do this, you can handle child form's Closing event.

private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.Hide();
        e.Cancel = true;
    }

And then you don't need to create new instances of frm. Just call Show method on the instance.
You can check Form.Visible property to check if the form open at the moment.

    private ChildForm form = new ChildForm();

    private void ReopenChildForm()
    {
        if(form.Visible)
        {
            form.Hide();
        }
        //Update form information
        form.Show();
    }

Actually, I still don't understand why don't you just update the data on the form.

MAKKAM
this is interesting, but how I can identify the form, I mean we use form fm = new form(); so it is always new form, so how I can identify the form?
Data-Base
If it is only one such form you can create class level variable for it. If there are a plenty of forms created in the bunch of methods, you better use Sascha's approach)
MAKKAM
I solve it with checking if the form open by name then close it if it is open Form fc = Application.OpenForms["UpdateWindow"]; if (fc != null) fc.Close(); fm.Show();
Data-Base