c# winform窗体控件怎么根据得到的字符串自动生成?

2024-12-22 08:21:29
推荐回答(2个)
回答1:

就是利用C#的反射技术:
例子,如下:
添加using System.Reflection;

private void button1_Click(object sender, EventArgs e)
{
string _textBox= "System.Windows.Forms.TextBox";
Type ctType = Assembly.LoadFrom(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll").GetType(_textBox);
Control control = (Control)ctType.InvokeMember(null, BindingFlags.CreateInstance, null, null, null);
this.Controls.Add(control);
}
control 就是你想要的winFrom控件
或者
TextBox control = (TextBox)ctType.InvokeMember(null, BindingFlags.CreateInstance, null, null, null);

回答2:

http://www.stuhack.com/biancheng/c_/7217.html,这篇文章应该能解开你的疑惑。