简体   繁体   中英

to check if MdiChildren exists ,c#

I have MDI application and opeing new form on focus/selection. To avoide opening of the same image more than once I have written this piece of code but it has a problem

private void lstview1_MouseDoubleClick(object sender, MouseEventArgs e) {
string window_name= this.lstview1.FocusedItem.Tag.ToString();

            if (this.MdiChildren.Count() > 0)
            {

                if ( window_name == this.MdiChildren[i].Tag.ToString()) // At this point  need ur help
                {
                    this.MdiChildren[i].Activate();
                }
                else
                {
                    Image_show_form(image, window_name);

                }
            }
            else
            {
                Image_show_form(image, window_name);

            }

}

where childform tag is again int.parse(window_name). but it throws error which makes sensce [ this.MdiChildren[index].Tag] needs to exist first. How can I ckeck this existance / or how can I make my code better.

What about this approach:

private void ShowForm(string name)
{
    Form targetForm = null;

    foreach (Form frm in Application.OpenForms)
    {
        if (frm.Tag != null)
        {
            if (frm.Tag.ToString() == name)
            {
                targetForm = frm;
                break;
            }
        }
    }

    if (targetForm != null)
    {
        targetForm.Activate();
    }
    else
    {
        // create new form and show it
    }
}

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