私のプログラムの本質は、ユーザーがボタンをクリックしてnumericUpDownを使用することで、追加したいテキストボックスとラジオボタンの量をクリックできることです。ボタンをクリックすると、プログラムはテキスト ボックスの下にボタンを追加しますが、行数を変更すると最初のボタンが残り、新しいボタンが作成されます。では、ボタンを 1 つだけにする方法を教えてください。テキストボックスとラジオボタンを削除してこれを適用することもできますか。
これはボタンハンドラーの私のコードです
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);