简单问题,帮忙写个函数!

colinmad 2005-05-04 05:03:08
if( _test==null || _test.IsDisposed)
{
_test = new test();
_test.MdiParent=this;
_test.Show();
}
else
{
_test.Activate();
_test.WindowState = FormWindowState.Maximized;
}
...全文
180 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
凨叔 2005-05-05
  • 打赏
  • 举报
回复
这是我在应用程序中用的一种方法,借你参考一下。

internal static Hashtable MDIFormLists = new Hashtable();

#region OpenMDIForm
public static Form OpenMDIForm(System.Type type, Form owner)
{
return OpenMDIForm(type, owner, true);
}
public static Form OpenMDIForm(System.Type type, Form owner, string ModuleCode)
{
return OpenMDIForm(type, owner, true, ModuleCode);
}
public static Form OpenMDIForm(System.Type type, Form owner, bool active)
{
return OpenMDIForm(type, owner, null, active, "");
}
public static Form OpenMDIForm(System.Type type, Form owner, bool active, string ModuleCode)
{
return OpenMDIForm(type, owner, null, active, ModuleCode);
}
public static Form OpenMDIForm(System.Type type, Form owner, object[] args)
{
return OpenMDIForm(type, owner, args, true);
}
public static Form OpenMDIForm(System.Type type, Form owner, object[] args, string ModuleCode)
{
return OpenMDIForm(type, owner, args, true, ModuleCode);
}
public static Form OpenMDIForm(System.Type type, Form owner, object[] args, bool active)
{
return OpenMDIForm(type, owner, args, active, "");
}
public static Form OpenMDIForm(System.Type type, Form owner, object[] args, bool active, string ModuleCode)
{
Form form;
if(MDIFormLists.ContainsKey(type))
{
form = MDIFormLists[type] as Form;
if(active)
{
if(form.WindowState==FormWindowState.Minimized)
{
form.WindowState = FormWindowState.Normal;
}
form.Activate();
}
}
else
{
form = CreateForm(type, args);
form.MdiParent = owner;
if(active)
form.Show();
}

return form;
}
#endregion

#region CreateForm
public static Form CreateForm(System.Type type)
{
return CreateForm(type, null);
}
public static Form CreateForm(System.Type type, object[] args)
{
Form form;
if(args==null || args.Length<=0)
form = Activator.CreateInstance(type) as Form;
else
form = Activator.CreateInstance(type, args) as Form;
return form;
}
#endregion
zhouyong2052 2005-05-04
  • 打赏
  • 举报
回复
将窗体作为object类型传递下去,同时将窗体的类型也提取出来传下去,在函数里再将窗体转换为该类型。(不过以前做这样的函数都是系统类型,不知自定义类型能不能做?)
xzq686 2005-05-04
  • 打赏
  • 举报
回复
你把你的代码放到一个函数里面
用的时候实例一个test类传函数。
colinmad 2005-05-04
  • 打赏
  • 举报
回复
要自定义能实例名
Ivony 2005-05-04
  • 打赏
  • 举报
回复
void AFunction ( System.Windows.Forms.Form _test )
{

......

}
colinmad 2005-05-04
  • 打赏
  • 举报
回复
是想要一个通用的,可以处理不同的窗体
orcale 2005-05-04
  • 打赏
  • 举报
回复
void showform(test _test)
{
if( _test==null || _test.IsDisposed)
{
_test = new test();
_test.MdiParent=this;
_test.Show();
}
else
{
_test.Activate();
_test.WindowState = FormWindowState.Maximized;
}



}
colinmad 2005-05-04
  • 打赏
  • 举报
回复
_test 是一个winform的实例
test是窗体类
like this :void AFunction(?? _test,?? test)
打开窗体时就不要敲一大堆代码

curlfw 2005-05-04
  • 打赏
  • 举报
回复
???
不懂楼主意思
这么几行也要省?
把_test写为全局变量应该就可以了咯
Ivony 2005-05-04
  • 打赏
  • 举报
回复
????

void AFunction( UnknowType _test )
{
if( _test==null || _test.IsDisposed)
{
_test = new test();
_test.MdiParent=this;
_test.Show();
}
else
{
_test.Activate();
_test.WindowState = FormWindowState.Maximized;
}

}
colinmad 2005-05-04
  • 打赏
  • 举报
回复
把以上代码改成函数,这样就可以省代码了

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧