0
votes

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.

1

1 Answers

0
votes

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
    }
}