使用 UserControl 创建具有设计时支持的 ToolBox 组件

发布于 2024-08-05 08:58:15 字数 222 浏览 3 评论 0原文

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

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

I have a UserControl for Windows Forms. How can I convert it to a component? What I want to do is, to add it to the VS toolbox, add it to form with drag and drop at design-time and change its Location and Dock properties using the Properties window. What should I look for to do this?

I've created something similar before but it was a Component class. I can add it to the VS toolbox, add it to the form with drag and drop and change its custom properties using the Properties window but since it's a component it shows on bottom component area and has no design-time support.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的故事 2024-08-12 08:58:15

UserControl 一个组件。您无需执行任何额外操作即可实现此目的。

如果您在 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;
}

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

A UserControl is a Component. You don't have to do anything extra to make it so.

If you create a new UserControl within VS, it will automatically have the behavior you are seeking: drag from toolbox onto form, change its properties through the Properties panel.

This code creates functioning control that shows itself in the toolbox, and allows its properties to be changed.

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;
}

Maybe you could add your functionality to this class and see if it works then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文