RainX

Hi all,

I'm new to c# and currently working on a project containing mdi forms.
This is how the project is layed out.
Parent form is called Street_Index. This contians a menu strip which can open 2 different forms.
* Street_Search
* Office_Search

Within the Street_search form there is a button that i click that returns the results in another form called Search_Results.
Search Results contains a grid and on double clicking the column of the grid it returns the results of that row back to Street_Search form which displays them in textboxes, grid, combobox, etc..

Within the Office_Search form there is also a button that opens the Search_Results form and then from there you can double click on the grid within Search_Results and open up Street_Search form which will display the results as previous.


Now onto the question. What is the cleanest possible way to check to see if a childform is open or not and if it is refresh it or bring it into focus

Thanks in advance
Take care all.







Re: Windows Forms General Parent, child forms check if open or closed

Chris Langsenkamp

This is just a thought, and may or may not apply to your particular solution:

Instantiate your child forms when the app starts or early on. Use Show/Hide and override the Close by the user. Show-ing a minimized MDI child may restore it if it's minimized within the parent - you'll have to test that.

In the end, you're reusing your child forms, not creating umpteen multiples of them, so maybe that works for you and maybe it doesn't.

Chris





Re: Windows Forms General Parent, child forms check if open or closed

Rong-Chun Zhang - MSFT

Hi RainX,

You can loop through all MDI child forms and check the type of each form. If these is a form whose type matched the type of the form you want to open, you need to create a new instance, you just need to make the match form as the active form. Try the following code:

Code Snippet

namespace MDIForm

{

public partial class MainForm : Form

{

public MainForm()

{

InitializeComponent();

}

private void openChiid1ToolStripMenuItem_Click(object sender, EventArgs e)

{

int intIndexChild;

Form frm;

if(!ExistInParent(typeof(ChildFrm1),out intIndexChild))

{

frm = new ChildFrm1();

frm.MdiParent = this;

frm.Show();

}

else

{

MdiChildren[intIndexChild].Activate();

}

}

private void openChiid2ToolStripMenuItem_Click(object sender, EventArgs e)

{

int intIndexChild;

Form frm;

if(!ExistInParent(typeof(ChildFrm2),out intIndexChild))

{

frm = new ChildFrm2();

frm.MdiParent = this;

frm.Show();

}

else

{

MdiChildren[intIndexChild].Activate();

}

}

bool ExistInParent(Type type, out int intIndexChild)

{

int count = this.MdiChildren.Length;

for (int i = 0; i < count; i++)

{

if (this.MdiChildren[i].GetType() == type)

{

intIndexChild = i;

return true;

}

}

intIndexChild = -1;

return false;

}

}

}

Hope this helps.
Best regards.






Re: Windows Forms General Parent, child forms check if open or closed

nobugz

The cleanest possible way is to let the child form raise an event, picked up by the MDI parent. Who can then decide how other possible instantiated children should be notified.