表单显示和关闭字符串名称 [英] Form Show and Close with String Name

查看:74
本文介绍了表单显示和关闭字符串名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串变量具有表单名称。 (同一个项目)



如何打开/关闭此表格

解决方案

这是一个相当糟糕的主意 - 它通常表明你的设计对于一个简单的任务是有缺陷的 - 但你可以用反射来做,因为ThePhantomUpovoter说:

  private   void  MyButton_Click( object  sender,EventArgs e)
{
类型t = Type.GetType( MyNamespaceName.MyFormClassName);
表格f =(表格)Activator.CreateInstance(t);
f.Show();
}

要关闭它,你不能直接这样做 - 你需要存储实例并关闭它以确保你正在关闭正确的表单 - 你可以获得使用Application.OpenForms打开表单,但是如果你打开多个同一个类,很可能它们对你来说几乎无法区分。





Pah! VB ...

  Dim  t  As  Type = Type。[ GetType ](  MyNamespaceName。 MyFormClassName
Dim f As Form = DirectCast (Activator.CreateInstance(t),Form)
f.Show()



[/ edit]


String variable has form name. (same project)

How to Open / Close this form

解决方案

It's a fairly poor idea - it normally indicates that your design is flawed for a simple task - but you can do it with reflection as ThePhantomUpovoter has said:

private void MyButton_Click(object sender, EventArgs e)
    {
    Type t = Type.GetType("MyNamespaceName.MyFormClassName");
    Form f = (Form) Activator.CreateInstance(t);
    f.Show();
    }

To close it, you can't really do it directly - you need to store the instance and close that to ensure that you are closing the correct form - you can get the list of open forms with Application.OpenForms, but if you open more than one of the same class, it is likely that they will be pretty much indistinguishable to you.

[edit]
Pah! VB...

Dim t As Type = Type.[GetType]("MyNamespaceName.MyFormClassName")
Dim f As Form = DirectCast(Activator.CreateInstance(t), Form)
f.Show()


[/edit]


这篇关于表单显示和关闭字符串名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆