Reputation: 1
Overview:
I have an MDI Parent Form in which I load other forms. After loading the second form, I can no longer bring the first one to the front.
Description:
On the parent form I have a menu strip containing 2 menu items; Home and Search. Each click event loads their corresponding form unless said form is already loaded.
The problem:
a. Click Search. Then click Home.
b. If Search is once again clicked, it no longer brings its corresponding, already opened form to the front.
private void tsmHome_Click(object sender, EventArgs e)
{
// Loop through all open forms...
foreach (Form form in Application.OpenForms)
{
// If frmHome is Opened, set focus to it and exit subroutine.
if (form.GetType() == typeof(frmSearch))
{
form.Activate();
return;
}
}
// If frmHome is not Opened, create it.
frmHome f = new frmHome();
f.MdiParent = this;
f.Show();
}
private void tsmSearch_Click(object sender, EventArgs e)
{
// Loop through all open forms...
foreach (Form form in Application.OpenForms)
{
// If frmSearch is Opened, set focus to it and exit subroutine.
if (form.GetType() == typeof(frmSearch))
{
form.Activate();
return;
}
}
// If frmSearch is not Opened, create it.
frmSearch f = new frmSearch();
f.MdiParent = this;
f.Show();
}
Upvotes: 1
Views: 12351
Reputation:
foreach (Form form in System.Windows.Forms.Application.OpenForms)
{
if (form.GetType() == typeof(Tviewer))
{
form.WindowState = FormWindowState.Minimized;
form.WindowState = FormWindowState.Normal;
return;
}
}
//It worked for me.
Upvotes: 0
Reputation: 54562
Your code is working for me.. After changing one line in your tsmHome_Click
event handler
You had.
if (form.GetType() == typeof(frmSearch))
It should be.
if (form.GetType() == typeof(frmHome))
Looks like a copy paste error got you.
Upvotes: 3
Reputation: 1
You can change code to this, if form exist make it bring to front.
// Loop through all open forms...
foreach (Form form in Application.OpenForms)
{
// If frmSearch is Opened, set focus to it and exit subroutine.
if (form.GetType() == typeof(frmSearch))
{
form.Activate();
form.BringToFront();
//form.WindowState = FormWindowState.Maximized;
return;
}
}
// If frmSearch is not Opened, create it.
frmSearch f = new frmSearch();
f.MdiParent = this;
f.Show();
Upvotes: 0
Reputation: 7918
You can try several options:
f.TopMost = true;
f.BringToFront();
Also, you can open the window in Dialog mode:
f.ShowDialog();
Hope this will help. Best regards,
Upvotes: 0