问题描述
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();
推荐答案
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.
这篇关于使用字符串值动态显示表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!