首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

在线程中怎么打开指定Form窗体

2012-02-29 
在线程中如何打开指定Form窗体因为每个窗体都是独立的,所以想通过线程打开窗体,寻求方法!!现有A现有ABCDE5

在线程中如何打开指定Form窗体
因为每个窗体都是独立的,所以想通过线程打开窗体,寻求方法!!

现有A   现有A   B   C   D   E   5个子窗体和S一个主窗体,想实现在S中点击按钮后通过线程打开子窗体,子窗体显示在S主窗体的panel1中。   5个子窗体和S一个主窗体,想实现在S中点击按钮后通过线程打开子窗体,子窗体显示在S主窗体的panel1中。

点击   A   关闭打开的B   C   D   E,同理点击B   关闭打开的A   C   D   E

[解决办法]
帮LZ顶
[解决办法]
应该是隐藏哦 只能显示一个窗体 其他的都隐藏
主窗体 可以通过方法获得子窗体的句丙(类似指针)句丙.属性(方法),主窗体里面写一个循环判断。
[解决办法]
Mark
[解决办法]
写了一个,但比较麻烦...只用了三个窗体,S,A,B..

S窗体下:
//定义一个线程
private Thread thread;

//存储当前已打开窗体的类型
public static Type CurrentFormType = null;

//定义一个委托,用于打开窗体
public delegate void MyInvoke(Type type);

//定义一个方法用于打开窗体
private void OpenForm(Type type)
{
object obj = Activator.CreateInstance(type);
type.InvokeMember( "TopLevel ", BindingFlags.SetProperty, null, obj, new object[] { false });
type.InvokeMember( "Visible ", BindingFlags.SetProperty, null, obj, new object[] { true });
//添加
this.panel1.Controls.Add(obj as Control);
}

//线程方法
private void ThreadProc()
{
MyInvoke mi = new MyInvoke(OpenForm);
this.BeginInvoke(mi, new object[] { CurrentFormType });
}

//打开A窗体
private void button1_Click(object sender, EventArgs e)
{
//先判断当前panel中是否已有窗体
if (CurrentFormType != null)
{
if (!CurrentFormType.Equals(typeof(A)))
{
//假如不是A类型的,先将其关闭
foreach (Control ctl in this.panel1.Controls)
{
Type type = ctl.GetType();
if (type.Equals(CurrentFormType))
{
//将其关闭
type.InvokeMember( "Close ", BindingFlags.InvokeMethod, null, ctl, null);
}
}
}
else return;
}
CurrentFormType = typeof(A);
//启动线程,打开窗体
StartThread();
}
//启动线程
private void StartThread()
{
thread = new Thread(new ThreadStart(ThreadProc));
thread.Start();
}

//打开B
private void button2_Click(object sender, EventArgs e)
{
//先判断当前panel中是否已有窗体
if (CurrentFormType != null)
{
if (!CurrentFormType.Equals(typeof(B)))
{
//假如不是A类型的,先将其关闭
foreach (Control ctl in this.panel1.Controls)
{
Type type = ctl.GetType();
if (type.Equals(CurrentFormType))
{
//将其关闭
type.InvokeMember( "Close ", BindingFlags.InvokeMethod, null, ctl, null);
}
}
}
else return;
}
CurrentFormType = typeof(B);


//启动线程,打开窗体
StartThread();
}

B窗体下:
private void B_FormClosing(object sender, FormClosingEventArgs e)
{
S.CurrentFormType = null;
}
A窗体下:
private void A_FormClosing(object sender, FormClosingEventArgs e)
{
S.CurrentFormType = null;
}

还有很多地方待优化..
楼主参考一下...

热点排行