views:

293

answers:

1

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.

+1  A: 

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.

Grammarian