从字符串变量实例化表单 [英] Instantiate Form from string variable

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

问题描述

大家好,希望有人可以帮助我解决我的问题.

如何使用C#从字符串变量实例化表单?

我的情况是:

1.我的应用程序将从数据库中填充其菜单
2.在该数据库中,菜单引用了表单名称
例如:
MenuDesc MenuObject //这是指列名
一个.数据输入-frmDataEntry//引用行数据
b.流程条目-frmProcessEntry

3.当我选择/单击数据输入时,系统将实例化frmDataEntry.当我选择流程条目"时,该流程将相同.

这可能吗?希望有人可以帮助我.我正在使用C#

解决方案

您可以使用 ^ ]方法.在实施此方法之前,请先阅读有关工厂方法模式的信息.


您可以存储表单的全名(例如:foo.bar.frmDataEntry),并使用反射来加载表单对象.


让我知道您是否需要更多详细信息

做了一次Google搜索,并在这里想出了你想要的东西

http://www.debugging.com/bug/8503 [



我在VB中知道它..但是再次转换为C#应该很简单.

再见.

当您要调用/实例化的表单在同一项目中时,此方法有效.使用相同的解决方案引用其他项目时出现错误,编译后会生成DLL.

我的代码:

//IBS_SysAd-一个输出到DLL的项目.
字符串sExec ="IBS_SysAd.frmMenu";

类型Form3Type = Type.GetType(sExec,true,true); //这是我遇到错误的地方. TypeLoadException,无法从程序集"IBS,版本= 1.0.0.0,文化=中性,PublicKeyToken =空"中加载类型"IBS_SysAd.frmMenu" .


表单Form3 =(Form)Activator.CreateInstanceForm3Type);
Form3.Show();

tnx.


Hi everybody, hope someone might help me with my problem.

How can i instantiate a Form from string variable using C#?

my scenarios are:

1. My application will populate its menu from a database
2. In that database, the menu has its reference to the form name
eg:
MenuDesc MenuObject // This refers to the column name
a. Data Entry - frmDataEntry // refers to the row data
b. Process Entry - frmProcessEntry

3. When I select/click Data Entry the system will Instantiate frmDataEntry. The process goes the same when i choose Process Entry.

Is this possible? Hope somebody out there might help me. I''m using C#

解决方案

You can do the same with Activator.CreateInstance[^] method. Please read about Factory Method Pattern before implementing this.


You could store the full name of the form , (ex: foo.bar.frmDataEntry) , and use reflection to load the form object.


Let me know if you need more details

[EDIT] Did a google search and came up with what you want here

http://www.debugging.com/bug/8503[^]

what you need is

Method 3
	
''frmContractEdit needs to have Inherits System.Windows.Forms.Form in its Class definition (designer)
	Dim Form3Type As Type = Type.GetType("Fusion.frmContractEdit", True, True)
	MsgBox(Form3Type.AssemblyQualifiedName)
	Dim Form3 As Form = CType(Activator.CreateInstance(Form3Type), Form)
	Form3.Show()
''Method 4
	Dim Form4 As Form = CType(Activator.CreateInstance(Type.GetType("Fusion.frmContractEdit", True, True)), Form)
	Form4.Show()	
''Method 5
''Pass parameters using overload Activator.CreateInstance Object Paramater Array
''frmLegalServicesEdit(ByVal DD_TablesID As String, ByVal dgdBrow As DataGridView)
	Dim FType As Type = Type.GetType ("Fusion.frmLegalServicesEdit", True, True)
	Dim Args() As Object = {Me.DD_TablesID, Me.dgdBrow}
	Dim Form5 As Form = CType(Activator.CreateInstance(FType, Args), Form)
	Form5.Show()



I know its in VB .. but it should be straight forward to convert to C#


Hi again.

This works when the form that you''re calling/instantiating is within the same project. I''m having an error when i''m referencing to other project withing the same solution, that generates DLL when compiled.

my code:

// IBS_SysAd - a project that output to a DLL.
string sExec = "IBS_SysAd.frmMenu";

Type Form3Type = Type.GetType(sExec, true, true); // This is the point i''m encountering error. TypeLoadException, Could not load type ''IBS_SysAd.frmMenu'' from assembly ''IBS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null''.


Form Form3 = (Form)Activator.CreateInstanceForm3Type);
Form3.Show();

tnx.


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

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