一个程序中有两个窗体Form1和Form2
两个窗体都显示出来,然后再单击其它程序的任意一个窗口,这时候另外的程序的窗口遮挡了这两个窗体。然后再次但是任务栏中的Form1窗体,只显示Form1窗体,也就是说只让Form1窗体显示在所有窗口之上,成为当前窗口,我想另外的Form2,也跟着显示在其它程序窗口的前面。有什么办法实现我的要求?
两个窗体都显示出来,然后再单击其它程序的任意一个窗口,这时候另外的程序的窗口遮挡了这两个窗体。然后再次但是任务栏中的Form1窗体,只显示Form1窗体,也就是说只让Form1窗体显示在所有窗口之上,成为当前窗口,我想另外的Form2,也跟着显示在其它程序窗口的前面。有什么办法实现我的要求?
{
foreach( Form frm in System.Windows.Forms.Application.OpenForms)
{
if (frm is Form2) frm.BringToFront();
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.Activated+=new EventHandler(Form1_Activated);
}
Form2 frm;
private void button1_Click(object sender, EventArgs e)
{
frm= new Form2();
frm.Show(this);
} private void Form1_Activated(object sender, EventArgs e)
{
if (frm != null)
frm.Activate();
}
}
}在Activated事件中进行操作显示另外的窗体就可以了
是正确的,但是直接在Activated事件中激活的话,会陷入死循环的