动态添加和删除按钮c#

时间:2013-04-03 21:29:58

标签: c# winforms custom-controls

我的程序的本质是用户可以通过单击按钮并使用numericUpDown单击要添加的文本框和单选按钮。单击按钮后,程序会在文本框下添加一个按钮,但如果我更改第一个按钮所在的行数并创建一个新按钮。所以我想知道我怎么才能只有一个按钮?我是否也可以通过删除文本框和单选按钮来应用它。

这是我的按钮处理程序代码

 int length = (int)this.numericUpDownNumComp.Value;
        bool created = false;
        CustomButton c = new CustomButton();
        for(int i = 0; i < length; i++)
        {
            //instantiate and configure the text boxes
            textboxComputer.Add(new TextBox());
            System.Drawing.Point p = new System.Drawing.Point(176, 114 + i * 25);
            //to evoke an object in an ArrayList we use the 'as' keyword
            (textboxComputer[i] as TextBox).Location = p;
            (textboxComputer[i] as TextBox).Size = new System.Drawing.Size(183, 20);
            //use 'as' again here to add the control to the controls Collection
            this.Controls.Add(textboxComputer[i] as TextBox);
            //instantiate and configure the labels
            this.labels.Add(new Label());
            System.Drawing.Point pLabel = new System.Drawing.Point(100, 114 + i * 25);
            (labels[i] as Label).Location = pLabel;
            (labels[i] as Label).Size = new System.Drawing.Size(80, 13);

            (labels[i] as Label).Text = @"Computer " + (i + 1).ToString() + ":";
            this.Controls.Add((labels[i] as Label));
            //add some mouse events
            (textboxComputer[i] as TextBox).MouseEnter += new System.EventHandler(this.textBox_mouseEnter);
            (textboxComputer[i] as TextBox).MouseLeave += new System.EventHandler(this.textBox_mouseLeave);
            //add the radio buttons - these are already sized  (See RadioButtons.cs) so just need to place at a point
            radioButtons.Add(new RadioButtons());
            (radioButtons[i] as RadioButtons).Location = new System.Drawing.Point(370, 110 + i * 25);
            this.Controls.Add(radioButtons[i] as RadioButtons);
            int last = length - 1;


        }
        if (created == true)
        {
            this.Controls.Remove(c as Button);
            //(c as Button).Location = new System.Drawing.Point(370, 110 + i * 25 + 25);
            created = false;
        }

            created = true;
            this.Controls.Add(c as Button);
            (c as Button).Location = new System.Drawing.Point(370, 110 + length * 25 + 25);

0 个答案:

没有答案