控件名称已在使用错误

时间:2015-09-03 21:58:34

标签: winforms custom-controls

当我从工具箱中拖动它时,我正在试图弄清楚如何让控件在名称的末尾添加“1”,“2”等。目前所做的只是拖动控件并将名称作为控件名称;我想要发生的是,如果我从工具箱中拖动它以获得“MyButton1”的效果;如果我拖动另一个,“MyButton2”。

以下是我跟随的瘦身。

如果您看到需要改进的内容,也欢迎任何帮助,请告诉我。

哦,如果有人有样品他们可以推荐给我那将是很棒的。

由于

// Button Code
using CustomControls.Designers;
using System;
using System.ComponentModel;
using System.Windows.Forms;


namespace CustomControls
{
   [Designer(typeof(MyButtonDesigner))]
    public partial class MyButton : System.Windows.Forms.Button
    {
        public MyButton()
        {
            InitializeComponent();
        }

    // removed blah blah code

    }
}

//designer code
using System.ComponentModel;
using System.Windows.Forms.Design;

namespace CustomControls.Designers
{
    class MyButtonDesigner : ControlDesigner
    {

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            MyButton uc = component as MyButton;
            this.EnableDesignMode(uc, "MyButton");
        }
    }
}

//from form1 designer code
   private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.MyButton = new CustomControls.MyButton();
        this.SuspendLayout();
        // 
        // MyButton
        // 
        this.MyButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("MyButton.BackgroundImage")));
        this.MyButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        this.MyButton.FlatAppearance.BorderSize = 0;
        this.MyButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.MyButton.ForeColor = System.Drawing.Color.White;
        this.MyButton.Location = new System.Drawing.Point(193, 58);
        this.MyButton.Name = "M3Button";
        this.MyButton.Size = new System.Drawing.Size(75, 23);
        this.MyButton.TabIndex = 2;
        this.MyButton.Text = "MyButton";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(632, 457);
        this.Controls.Add(this.MyButton);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

    #endregion
    private CustomControls.MyButton MyButton;

0 个答案:

没有答案