我有一个已启动的主表单,然后它可以转到我创建的任何其他表单。但最重要的是,我编写了一个类,我称之为它返回一个字符串,其中包含要转到的表单名称。
目前我还没有这个工作,所以我将像这样从一个表单转到另一个表单(静态编写的链接代码):
this.Hide();
CloudAccess nextForm1 = new CloudAccess();
//Where CloudAccess is the class of the next form.
nextForm1.ShowDialog();
我想要的是这样的:
FormController pick = new FormController();
//Where FormController is the Class I create an object of and ask what's next
string next = pick.whereToGo(); //lets say it returns "CloudAccess"
this.Hide();
next nextForm1 = new next(); //next is desired to be the contents of the string
nextForm1.ShowDialog();
问题是我不知道如何使用返回的字符串来创建新对象并使用它。我一直在研究像这样的调用和反射主题:使用字符串值创建新实例 https://stackoverflow.com/questions/2778229/use-string-value-to-create-new-instance但我是 C# 新手,我不确定如何将其应用于此场景。
想法?谢谢!
这是 fejejosco 所说的工作代码:
string asdf = "CloudAccess";
Type CAType = Type.GetType("namespace." + asdf);
Form nextForm2 = (Form)Activator.CreateInstance(CAType);
nextForm2.ShowDialog();
Thanks!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)