Winforms 控件名称已在使用中错误

Winforms 控件名称已在使用中错误,winforms,custom-controls,Winforms,Custom Controls,当我从工具箱中拖动控件时,我试图弄清楚如何让它在名称的末尾添加“1”、“2”等。目前,它所做的只是拖动控件并将其名称保留为控件名称;我想发生的是,如果我从工具箱中拖动它,使其具有“MyButton1”的效果;如果我再拖动一个,“我的按钮2” 下面是我跟随的瘦子 我们非常感谢您的帮助,如果您发现需要改进的地方,也请告诉我 哦,如果有人有样品,他们可以把我介绍给他们,那就太好了 谢谢 // Button Code using CustomControls.Designers; using Syste

当我从工具箱中拖动控件时,我试图弄清楚如何让它在名称的末尾添加“1”、“2”等。目前,它所做的只是拖动控件并将其名称保留为控件名称;我想发生的是,如果我从工具箱中拖动它,使其具有“MyButton1”的效果;如果我再拖动一个,“我的按钮2”

下面是我跟随的瘦子

我们非常感谢您的帮助,如果您发现需要改进的地方,也请告诉我

哦,如果有人有样品,他们可以把我介绍给他们,那就太好了

谢谢

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

嗯,这很难出错。您必须在组件的自定义设计器中出错。我们看不到。谢谢Hans,我忘了覆盖InitializeNewComponent。我从以下方面找到了可以使用的工具: