Reputation: 1
I have a MDI Parent form in which I open MDI Child forms, but I don't want to reopen them if i click another time, instead of that i want to focus on that particular mdi Child form which is already opened, and i am doing this job from clicking my menu strip. i already tried a lot to do this successfully, but got failed all the time. well my code is :
this is a Method...
private bool CheckMdiClientDuplicates(string WndCls)
{
Form[] mdichld = this.MdiChildren;
if (this.MdiChildren.Length == 0)
{
return true;
}
foreach (Form selfm in mdichld)
{
string str = selfm.Name;
str = str.IndexOf(WndCls).ToString();
if (str != "-1")
{
return true;
}
}
return false;
}
and i am implementing this method via...
private void myToolStripMenuItem_Click(object sender, EventArgs e)
{
MyForm f = new MyForm();//MyForm is the form on which i am working
if (CheckMdiClientDuplicates("MyNamespace.MyForm") == true)
{
f.MdiParent = this;
f.Show();
}
else
{
f.Activate();
f.BringToFront();
}
}
Upvotes: 0
Views: 2352
Reputation: 1294
You should try this code:
This is when you click a button for your first form to appear:
private void button1_click(object sender, EventArgs e){
Form1 f1 = null;
if (IsFormAlreadyOpen(typeof(Form1)) == null)
{
if (ActiveMdiChild != null)
{
ActiveMdiChild.Close();
}
f1 = new Form1();
f1.MdiParent = this;
f1.Show();
}
}
Method:
public static Form IsFormAlreadyOpen(Type FormType)
{
foreach (Form OpenForm in Application.OpenForms)
{
if (OpenForm.GetType() == FormType)
return OpenForm;
}
return null;
}
Do the same when you click the next button for the other form.
Upvotes: 0
Reputation: 116438
f
is still your new MyForm()
, so what do you expect Activate
ing it will do? You need to get the actual form you want to bring to the front and Activate
that.
Additionally, you probably don't want to make a new MyForm
unless you intend to use it.
Upvotes: 2