是否Application.OpenForms集合里的窗体都是要经过Form.Show()之后才可以列入Application.OpenForms;集合的?我刚刚做了一个小测试。
private void TestApplicationOpenFormsMethod()
{
#region 测试代码一
Form temp_form = new Form();
temp_form.Name = "MyForm"; foreach (Form f in Application.OpenForms)
{
if (f.Name == temp_form.Name)
{
MessageBox.Show("I Found it.");
return;
}
}
MessageBox.Show("I not found it.");//代码二可以执行到这里,说明Application.OpenForms集合是没有这个temp_form的。
return;
#endregion #region 测试代码二
Form temp_form = new Form();
temp_form.Name = "MyForm";
temp_form.Show();//主要就是这里与测试代码一的不一样。 foreach (Form f in Application.OpenForms)
{
if (f.Name == temp_form.Name)
{
MessageBox.Show("I Found it.");//代码二可以执行到这里,说明Application.OpenForms集合是有这个temp_form的。
return;
}
}
MessageBox.Show("I not found it.");
return;
#endregion //现在主要我想解决的是。
//我想窗体不想Show()之后Application.OpenForms里有我想要的指定的已经New过的Form;
//是否有相关的Application.属性呢?
//我也不可能用Form.Show()之后,再马上用Form.Hide()吧?这方法帮笨了。
//如果有现成的方法可以在不用Form.Show()后,又可以拿到所有已经new()过的(即在内存的位置的)的窗体的集合。
//我该怎么写呢?
}
private void TestApplicationOpenFormsMethod()
{
#region 测试代码一
Form temp_form = new Form();
temp_form.Name = "MyForm"; foreach (Form f in Application.OpenForms)
{
if (f.Name == temp_form.Name)
{
MessageBox.Show("I Found it.");
return;
}
}
MessageBox.Show("I not found it.");//代码二可以执行到这里,说明Application.OpenForms集合是没有这个temp_form的。
return;
#endregion #region 测试代码二
Form temp_form = new Form();
temp_form.Name = "MyForm";
temp_form.Show();//主要就是这里与测试代码一的不一样。 foreach (Form f in Application.OpenForms)
{
if (f.Name == temp_form.Name)
{
MessageBox.Show("I Found it.");//代码二可以执行到这里,说明Application.OpenForms集合是有这个temp_form的。
return;
}
}
MessageBox.Show("I not found it.");
return;
#endregion //现在主要我想解决的是。
//我想窗体不想Show()之后Application.OpenForms里有我想要的指定的已经New过的Form;
//是否有相关的Application.属性呢?
//我也不可能用Form.Show()之后,再马上用Form.Hide()吧?这方法帮笨了。
//如果有现成的方法可以在不用Form.Show()后,又可以拿到所有已经new()过的(即在内存的位置的)的窗体的集合。
//我该怎么写呢?
}
你说的这个方法,我想过,是可以实现。但是。我们那个技术经理说。能不用static最好。容易搞乱类之间的数据。要是以前的我的话。我也会用你现在所说的这个方法。
这种方法还要在每个Form里的构造器里写这个麻烦的东西。假如我有:One,Two,Three三个窗体。class Common
{
public static ArrayList al=new ();
}class One:Form
{
public One()
{
InitilizeCompenent();
Common.al.Add(this);
}
private void One_FormClosed(object sender,EventAgrs e)
{
Common.al.Remove(this);
}
}
class Two:Form
{
public Two()
{
InitilizeCompenent();
Common.al.Add(this);
}
private void Two_FormClosed(object sender,EventAgrs e)
{
Common.al.Remove(this);
}
}
class Three:Form
{
public Three()
{
InitilizeCompenent();
Common.al.Add(this);
}
private void Three_FormClosed(object sender,EventAgrs e)
{
Common.al.Remove(this);
}
}每个构造器都要加。而且每个Form都要给加一个事件FormClosed事件处理Common.al的相应删除。要是用这种方法,我们那个经理道:方法太笨了。
public static ArrayList al=new ();改为:public static ArrayList al=new ArrayList();
Form2 f = new Form2();
foreach (Control item in f.Controls)
{
Console.WriteLine(item.Name);
}
private void frmMain_Load(object sender, EventArgs e)
{
RefreshListBox();
} private void button1_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Name = nNumber.ToString();
form.Text = nNumber.ToString();
form.Show();
nNumber++;
RefreshListBox();
} private void button2_Click(object sender, EventArgs e)
{
if (-1 == listBox1.SelectedIndex)
{
return;
}
Application.OpenForms[listBox1.Items[listBox1.SelectedIndex].ToString()].Close();
RefreshListBox();
} private void RefreshListBox()
{
listBox1.Items.Clear();
foreach (Form form in Application.OpenForms)
{
listBox1.Items.Add(form.Name);
}
}
wuyq11朋友。你这个是用了Show()的。我就是想不用Show()。而通过另一种方式来获取所有已经new()过的窗体集合。Application.OpenForms;是存放着所有已经Show()过之后的所有窗体的集合。
属性值
类型:System.Windows.Forms.FormCollection包含属于此应用程序的所有当前打开窗体的 FormCollection。
请问LZ怎么理解 当前打开窗体 这个字眼,没有Show难道是打开?
Form1 f = new Form1();//此时没有Application.OpenForms中还不存在f
f.OnLoad(null);//此时Application.OpenForms中已存在f//f.Show(this)//只有调用了这个Show才会显示出窗体,这个不调用就不显示
{
base.OnClick(e);
foreach (Form frm in Application.OpenForms)
{
Console.WriteLine(frm.Name);
}
Form1 f = new Form1();
f.OnLoad(null);
foreach (Form frm in Application.OpenForms)
{
Console.WriteLine(frm.Name);
}
//f.Show();
}