【发布时间】:2017-11-25 04:31:34
【问题描述】:
我有一个程序,我在其中动态添加controls。控件类型基于database 中的值。例如,如果Database 中的值为Label,则程序会动态创建控件。
动态创建控件效果很好。我正在使用以下功能:
Type typeFrm = typeof(Form);
Assembly assb = typeFrm.Assembly;
Type controlType = assb.GetType("System.Windows.Forms." + strType);
object obj = Activator.CreateInstance(controlType);
Control control = (Control)obj;
return control;
然后Control ctrl = CreateControl(strCtrlType);
其他代码是设置控件的位置、宽度、高度等。
我的问题是我有一个custom control,如何将它动态添加到表单中?我尝试了上面的功能并更改了行:
Type controlType = assb.GetType("System.Windows.Forms." + strType);
到
Type controlType = assb.GetType("CustomCtrl." + strType);
但它不起作用。该函数始终返回null。
查看示例自定义控件代码。
namespace CustomCtrl
{
public class CButton : Button
{
public CButton() : base()
{
}
}
}
【问题讨论】: