使用字符串值动态显示表单 [英] show form dynamically, with string value

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

问题描述

normaly这是我打开表格的方式,有2个参数



normaly this is how i open a form, with 2 parameter

FmCalculate cc = new FmCalculate("456", "123");
FmCalculate.ShowDialog();





现在如何使用字符串值打开表单

例如:



now how can i open the form by using the string value
for example:

String strFormName = "FmCalculate";



i尝试下面的示例,但它说该值为null,以及如何包含456和123参数?


i tried below example, but it said the value is null, and how to include the "456" and "123" parameter?

Form form = (Form)Activator.CreateInstance(Type.GetType("FmCalculate"));
form.ShowDialog();

推荐答案

您应该提供完全限定的名称类型,包括命名空间。这就是为什么你得到一个null。例如:



You should provide a fully qualified name type, including your namespace. That's why you're getting a null. For example:

Form form = (Form)Activator.CreateInstance(Type.GetType("MyNameSpaceName.FmCalculate"));





你应该能够直接传递任何构造函数参数,如:





And you should be able to pass any constructor parameters directly like:

Form form = (Form)Activator.CreateInstance(Type.GetType("MyNameSpaceName.FmCalculate","456", "123"));





此外,如果您使用的是高于2的.NET版本(如果我没记错的话),您应该可以使用泛型函数调用 Activator.CreateInstance< t>()< / t>





Also, if you're using a .NET version above 2 (if I remember correctly) you should be able to use the generic function call Activator.CreateInstance<t>()</t>:

FmCalculate form = (FmCalculate)Activator.CreateInstance("456", "123"));





我在这里通过记忆,所以可能会有一些小的不准确。 :-)

请查看 Activator.CreateInstance()上的MSDN页面了解更多详情。



I'm going by memory here, so there may be some small inaccuracy. :-)
Please check the MSDN page on Activator.CreateInstance() for more details.


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

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