从字符串名称获取表单对象 [英] Get Form object From String Name

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

问题描述

大家好,

我在应用程序中拥有所有表单名称,我要做的就是获取该表单的实例

数据库中所有表单名称均可用,并且选定的命名表单将打开


所以我只有那个形式的字符串变量,我必须得到那个形式的实例


所以请帮我解决这个问题.

谢谢

Hi Everyone,

I have all the form name in my application and What i have to do is To get the instance of that form

All forms name are available in the database and selected named form will be open


So I have just string variable of that form and I have to get the instance of that form


So please Help me for this problem.

Thanks

推荐答案

讨论 [
This discussion [^]on DaniWeb may be useful. It boils down to this.

public void Open_Form(string formname)
{

    Type type = Type.GetType("myProject.Forms." + formname);
    object obj = Activator.CreateInstance(type);
    (obj as Form).MdiParent = this;
    (obj as Form).Show();

}


为此,您必须使用.Net的反射

可能对您有帮助
I thing for that you have to use reflection of .Net

may this helps you


您需要做的第一件事是在您的应用程序中的某个位置保留一组打开的表单,以供您执行查找操作.
然后,您可以简单地遍历集合,检查名称...
The first thing you need to do is to keep a collection of open forms somewhere in your application that you can perform the lookup against.
Then you can simply iterate over the collection checking the name...
public Form GetForm(string name)
{
    Form result = null;
    foreach(Form form in formsCollection)
    {
        if(form.Name == name)
        {
            result = form;
            break;
        }
    }
    return result;
}


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

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