【问题标题】:Creating a ToolBox component with design-time support using a UserControl使用 UserControl 创建具有设计时支持的 ToolBox 组件
【发布时间】:2010-11-29 17:16:38
【问题描述】:

我有一个用于 Windows 窗体的用户控件。如何将其转换为组件?我想要做的是,将其添加到 VS 工具箱中,在设计时通过拖放将其添加到表单中,并使用“属性”窗口更改其位置和停靠属性。我应该寻找什么来做到这一点?

我之前创建过类似的东西,但它是一个组件类。我可以将它添加到 VS 工具箱中,通过拖放将其添加到表单中,并使用属性窗口更改其自定义属性,但由于它是一个组件,它显示在底部组件区域并且没有设计时支持。

【问题讨论】:

    标签: .net winforms user-controls components design-time


    【解决方案1】:

    UserControlComponent。你不需要做任何额外的事情来做到这一点。

    如果您在 VS 中创建一个新的UserControl,它将自动具有您所寻求的行为:从工具箱拖到窗体上,通过属性面板更改其属性。

    此代码创建在工具箱中显示自身的功能控件,并允许更改其属性。

    public class TestUserControl : UserControl
    {
        public TestUserControl() {
            InitializeComponent();
        }
        /// <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 Component 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.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoEllipsis = true;
            this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(231, 51);
            this.label1.TabIndex = 0;
            this.label1.Text = "This is a test user control";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // TestUserControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.Controls.Add(this.label1);
            this.Name = "TestUserControl";
            this.Size = new System.Drawing.Size(231, 51);
            this.ResumeLayout(false);
    
        }
    
        #endregion
    
        private System.Windows.Forms.Label label1;
    }
    

    也许你可以将你的功能添加到这个类中,然后看看它是否有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      相关资源
      最近更新 更多