自定义组件不会使其成为组件

custom component does not makes it into components

我有一个在 winforms 中使用的自定义组件,效果很好。 只有一个小问题,当我将它放在表单上时,设计器不会将它插入到组件集合中,就像它对所有其他组件所做的那样。

当我在表单上放置上下文菜单项或绑定源时,它们包含在组件集合中,但我的用户组件不包含。 我怀疑我需要在组件的声明中添加一些奇特的属性,但我不知道那可能是什么。

也许这里有人知道该怎么做?

这里是组件的完整代码

public partial class ggDataBase : Component
{
    private string _ConnectionString = "";
    private SqlConnection connection = new SqlConnection();
    private SqlDataAdapter adapter = new SqlDataAdapter();
    private SqlCommand command = new SqlCommand();

    public string ConnectionString
    {
        get { return _ConnectionString; }
        set { _ConnectionString = value; }
    }

    public void FillDataTable(DataTable Table, string SqlText)
    {
        if ((connection.ConnectionString == null) || (connection.ConnectionString != _ConnectionString))
            connection.ConnectionString = _ConnectionString;

        if (connection.ConnectionString != null && connection.ConnectionString != "")
        {
            if (command.Connection == null)
                command.Connection = connection;
            command.CommandText = SqlText;
            command.CommandType = CommandType.Text;
            adapter.SelectCommand = command;
            adapter.Fill(Table);
        }
    }
}

不确定您哪里出错了...您的确切代码对我有用。我还创建了一个空白组件并添加了它:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

    public partial class MyComponent : Component
    {

    }


    public partial class ggDataBase : Component
    {
        private string _ConnectionString = "";
        private SqlConnection connection = new SqlConnection();
        private SqlDataAdapter adapter = new SqlDataAdapter();
        private SqlCommand command = new SqlCommand();

        public string ConnectionString
        {
            get { return _ConnectionString; }
            set { _ConnectionString = value; }
        }

        public void FillDataTable(DataTable Table, string SqlText)
        {
            if ((connection.ConnectionString == null) || (connection.ConnectionString != _ConnectionString))
                connection.ConnectionString = _ConnectionString;

            if (connection.ConnectionString != null && connection.ConnectionString != "")
            {
                if (command.Connection == null)
                    command.Connection = connection;
                command.CommandText = SqlText;
                command.CommandType = CommandType.Text;
                adapter.SelectCommand = command;
                adapter.Fill(Table);
            }
        }
    }

}

这是一个示例设计器文件。请注意,组件已声明并实例化,但您将 NOT 看到它们中的任何一个(甚至 built-in .Net 组件都没有)被显式添加到 "components" collection:

partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.myComponent1 = new WindowsFormsApplication111.MyComponent(this.components);
        this.button1 = new System.Windows.Forms.Button();
        this.ggDataBase1 = new WindowsFormsApplication111.ggDataBase();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
        this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
        ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(55, 62);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // ggDataBase1
        // 
        this.ggDataBase1.ConnectionString = "";
        // 
        // contextMenuStrip1
        // 
        this.contextMenuStrip1.Name = "contextMenuStrip1";
        this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 261);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private MyComponent myComponent1;
    private System.Windows.Forms.Button button1;
    private ggDataBase ggDataBase1;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
    private System.Windows.Forms.BindingSource bindingSource1;

}

这里的输出显示实际添加了组件(甚至是你的):

WindowsFormsApplication111.MyComponent
System.Windows.Forms.Button
WindowsFormsApplication111.ggDataBase
System.Windows.Forms.Timer
System.Windows.Forms.ContextMenuStrip
System.Windows.Forms.BindingSource

由以下代码生成:

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

    private void button1_Click(object sender, EventArgs e)
    {
        foreach(Component C in EnumerateComponents())
        {
            Console.WriteLine(C.GetType().ToString());
        }
    }

    // http://whosebug.com/a/17173320/2330053
    private IEnumerable<Component> EnumerateComponents()
    {
        return from field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
               where typeof(Component).IsAssignableFrom(field.FieldType)
               let component = (Component)field.GetValue(this)
               where component != null
               select component;
    }

}

请参阅我之前的评论,其中包含 link。 Here's the article again:

A component can let the designer know that it would like to be notified when its container goes away by implementing a public constructor that takes a single argument of type IContainer, as shown in this snippet. ... Notice that the constructor uses the container to add itself as a container component. In the presence of this constructor, the designer will generate code that uses this constructor, passing it a container for the component to add itself to.

由于您没有提供该构造函数,我只能猜测这是由基础 class 构造函数为您完成的。但我向你保证,即使你没有像传入组件那样的行,你的组件确实被添加到 collection。查看我在上面发布的先前代码的输出。