上一贴已经结贴,新开一贴吧,虽然上一贴没有怎么切实解决问题,还是给分了,耽误时间太长了还是那个问题我有个MDI窗体MainMDIParent 在这上面有个按钮A 单击按钮A时弹出子窗体B
foreach (Form froms in this.MdiChildren)
{
if (froms is B )
{
froms.Focus();
return;
}
}
B test = new B();
test.MdiParent = this;
test.Show();然后窗体B出现在MDI窗体中
然而... ...
当在子窗体C中单击按钮来显示窗体B的时候,窗体B在任务栏中显示,一直没弄懂,求解... ...
显示C的代码:foreach (Form froms in this.MdiChildren)
{
if (froms is C )
{
froms.Focus();
return;
}
}
C test = new C();
test.MdiParent = this;
test.Show();
在C中显示窗体B的代码: B pei = new B();
pei.TopLevel = false;
pei.Parent = this.Parent;
pei.Show();
foreach (Form froms in this.MdiChildren)
{
if (froms is B )
{
froms.Focus();
return;
}
}
B test = new B();
test.MdiParent = this;
test.Show();然后窗体B出现在MDI窗体中
然而... ...
当在子窗体C中单击按钮来显示窗体B的时候,窗体B在任务栏中显示,一直没弄懂,求解... ...
显示C的代码:foreach (Form froms in this.MdiChildren)
{
if (froms is C )
{
froms.Focus();
return;
}
}
C test = new C();
test.MdiParent = this;
test.Show();
在C中显示窗体B的代码: B pei = new B();
pei.TopLevel = false;
pei.Parent = this.Parent;
pei.Show();
mdi.MdiParent = this.MdiParent;
mdi.Show();报错:
非顶级窗体不能显示为模式对话框。在调用 showDialog 之前应从所有父窗体中移除该窗体。
当前的子窗体不是showDialog
public partial class MdiForm : Form
{
public MdiForm()
{
InitializeComponent();
} private void bFormToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowChildForm<BForm>();
} public Form ShowChildForm<T>()
where T : Form, new()
{
Form child = null; foreach (Form c in this.MdiChildren)
{
if (c is T)
{
child = c;
c.Activate();
break;
}
} if (child == null)
{
child = new T();
child.MdiParent = this;
child.Show();
} return child;
} private void showCToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowChildForm<CForm>();
}
} public class BForm : Form
{
public BForm()
{
this.Text = "This is form B";
} protected override Size DefaultSize
{
get
{
return new Size(300, 120);
}
}
} public class CForm : Form
{
public CForm()
{
this.Text = "This is form C"; Button button = new Button();
button.Text = "Show C";
button.Click += new EventHandler(button_Click);
button.Bounds = new Rectangle(50, 50, 80, 30);
this.Controls.Add(button);
} void button_Click(object sender, EventArgs e)
{
MdiForm parent = this.MdiParent as MdiForm;
if (parent != null)
{
parent.ShowChildForm<BForm>();
}
} }
我试了一下,没有问题呀,不过我有一个语句不一样 :
pei.MdiParent = this.ParentForm ;