简体   繁体   English

如何在表单中动态添加自定义控件

[英]How to add Custom Control in a form dynamically

I have a program wherein I am adding controls dynamically. 我有一个程序,其中controls动态添加controls Control type is based on a value in database . 控件类型基于database的值。 For example if the value in Database is Label then the program creates the control dynamically. 例如,如果“ Database值为“ Label则程序将动态创建控件。

Creating controls dynamically is working great. 动态创建控件的效果很好。 I am using below function: 我正在使用以下功能:

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;

then Control ctrl = CreateControl(strCtrlType); 然后Control ctrl = CreateControl(strCtrlType);

Other code is to setup the control location, width, height etc ect. 其他代码是设置控件的位置,宽度,高度等。

My question is I have a custom control and how will add it to the form dynamically? 我的问题是我有一个custom control ,如何将其动态添加到表单中? I tried the function above and change the line: 我尝试了上面的功能并更改了行:

Type controlType = assb.GetType("System.Windows.Forms." + strType);

to

Type controlType = assb.GetType("CustomCtrl." + strType);

But its not working. 但是它不起作用。 The function always return null . 该函数始终返回null

See sample custom control code. 请参阅示例自定义控制代码。

namespace CustomCtrl
{
public class CButton : Button
 {        
    public CButton() : base()
    {

    }
 }
}

Here is how to get the type from an assembly. 这是从程序集中获取类型的方法。 Imagine you have a class with full name (class name + namespace) SomeNamespace.SomeClass within a dll named Some.dll : 假设您有一个全名(类名+名称空间)的类SomeNamespace.SomeClass在名为Some.dll的dll中:

Type type = Type.GetType("SomeNamespace.SomeClass, 
    Some"); // without .dll

So in your case it will be: 因此,在您的情况下,它将是:

Type type = Type.GetType("CustomCtrl.CButton, 
    DllWhereCButtonIs"); // without .dll

Type.GetType("namespace.Type") only works when the type is present in mscorlib.dll or the currently executing assembly.If not of those things is true, you should use system.type.assemblyqualifiedname Type.GetType("namespace.Type")仅在mscorlib.dll或当前正在执行的程序集中存在该类型时才起作用。如果这些都不成立,则应使用system.type.assemblyqualifiedname

https://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx https://msdn.microsoft.com/zh-CN/library/system.type.assemblyqualifiedname.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何动态添加和从用户控件中删除用户控件的形式 - how to add dynamically and remove a user control from a user control in a form 如何以编程方式将自定义控件添加到表单并显示它? - How to programmatically add a Custom Control to a Form and show it? 动态添加控件以形成方法? - Dynamically add control to form in method? AsyncFileUpload动态添加自定义控件 - AsyncFileUpload Dynamically add in custom control 如何在代码中动态地将新的可视状态添加到自定义控件模板? - How to add new Visual States to a Custom control template dynamically in code? 如何将控件添加到自定义窗体的非客户区域 - How to add a control to the Non client area of a custom form 如何将自定义控件正确添加到另一个自定义控件,以便它将以表单形式呈现? - How do I properly add a custom control to another custom control so that it will render in a form? 如何在表格中添加表格控件? - How to add a table control to the form? 如何在最小化窗口的同时将控件动态添加到Windows窗体拆分器控件中? - How do I dynamically add controls into windows form splitter control while window is minimized? 如何将自定义控件添加到工具箱? - How to add a custom control to the toolbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM