简体   繁体   English

如何使用从主窗体上的内置控件继承的自定义控件类?

[英]How can I use a custom control class that inherits from a built-in control on my main form?

I have a mydatagridview class which inherits from the built-in DataGridView control, as shown below: 我有一个mydatagridview类,它从内置的DataGridView控件继承,如下所示:

public class mydatagridview : DataGridView 
{
    protected override bool ProcessDataGridViewKey(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter) 
        {
            this.ProcessTabKey(e.KeyData);
            return true;
        }
        return base.ProcessDataGridViewKey(e);
    }

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if (keyData == Keys.Enter) 
        {
            this.ProcessTabKey(keyData);
            return true;
        }
        return base.ProcessDialogKey(keyData);
    }
}

Now I want to utilize it in my main class: 现在,我想在我的主课中使用它:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}

I want to Utilize myDatagridview with Datagridview1 of : public partial class Form1 : Form 我想将myDatagridview与Datagridview1一起使用:公共局部类Form1:Form

How can I do this? 我怎样才能做到这一点?

You need to create an instance of your custom control class, and then add that instance to your form's Controls collection. 您需要创建自定义控件类的实例,然后将该实例添加到窗体的Controls集合中。 For example: 例如:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // Create an instance of your custom control
        mydatagridview myDGV = new mydatagridview();

        // Add that instance to your form's Controls collection
        this.Controls.Add(myDGV);
    }
}

Of course, you could also do the same thing from the Designer. 当然,您也可以从Designer中执行相同的操作。 It will automatically insert code very similar to that shown above inside the InitializeComponent() method. 它将自动在InitializeComponent()方法中插入与上面显示的代码非常相似的代码。

If your custom control doesn't show up in the Toolbox automatically after you've rebuilt your project, make sure that you've enabled toolbox auto-population : 如果重建项目后自定义控件未自动显示在工具箱中,请确保已启用工具箱自动填充

  1. From the "Tools" menu, select "Options". 从“工具”菜单中,选择“选项”。
  2. Expand the "Windows Forms Designer" category. 展开“ Windows窗体设计器”类别。
  3. Set the "AutoToolboxPopulate" property to True. 将“ AutoToolboxPopulate”属性设置为True。

    在VS选项中将AutoToolboxPopulate设置为True

If I understand correctly, and i'm not sure that I do, you can just use it like any other type: 如果我理解正确,但不确定是否可以这样做,则可以像使用任何其他类型一样使用它:

mydatagridview mydatagrid = new mydatagridview();
this.Controls.Add(mydatagrid);

Next to the answers that have already been given, it should be possible to drag and the drop the control from the toolbox to your form. 在已经给出的答案旁边,应该可以将控件从工具箱拖放到表单中。

If you create a user control, or a custom Control, and build your project, the control should show up in the toolbox. 如果您创建用户控件或自定义控件,并生成您的项目,则该控件应显示在工具箱中。

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

相关问题 如何在我自己的 ContentView(自定义控件)中使用 Xamarin.Forms.Setter 类? - How can I use the Xamarin.Forms.Setter class in my own ContentView (custom control)? Silverlight中是否有这样的内置控件? - Is there such built-in control in silverlight? 如何使用XAML中的另一个自定义控件基类使WPF在视图中实例化一个自定义控件? - How can I make WPF instantiate a custom control in my view, using another custom control base class in my XAML? 如何使我的 Form Control 变量可以访问我的 Form 类以外的类? - How can I make my Form Control variables acessible to classes other than my Form class? 如何从用户控件中的主窗体中读取属性 - How do I read a property from my main form in a user control 子类内置WinForms控件? - Subclass built-in WinForms control? 我无法在 Windows 表单中使用我的用户控制组件 - I can't use my User Control Component in Windows Form 如何将自定义TextBox样式应用于内置控件? - How can I apply a custom TextBox style to built-in controls? 如何从内置字符串(控件名称)访问控件? - How do I access a control from a built string(control name)? 我可以将具有导航属性的EF类用作控件的数据源吗? - Can I use EF class with navigational property as a datasource for my control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM