0

単一のテキスト ボックスの入力から動的にテキスト ボックスを作成するコードを作成しましたテキストボックスを生成する画像

ユーザーがデータを入力すると、このようなテキストボックスが自動的に生成されます....

画像あり

このコードを使用しました

private void textBoxInput_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(textBoxInput.Text))
        {
            //Get the number of input text boxes to generate
            int inputNumber = Int32.Parse(textBoxInput.Text);

            //Initialize list of input text boxes
            inputTextBoxes = new List<TextBox>();

            //Generate labels and text boxes
            for (int i = 1; i <= inputNumber; i++)
            {
                //Create a new label and text box
                Label labelInput = new Label();
                TextBox textBoxNewInput = new TextBox();

                //Initialize label's property
                labelInput.Text = "Product" + i;
                labelInput.Location = new Point(30, textBoxInput.Bottom + (i * 30));
                labelInput.AutoSize = true;

                //Initialize textBoxes Property
                textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3);

                //Add the newly created text box to the list of input text boxes
                inputTextBoxes.Add(textBoxNewInput);

                //Add the labels and text box to the form
                this.Controls.Add(labelInput);
                this.Controls.Add(textBoxNewInput);
            }


        }
 }

それはうまくいきますが、ユーザーがテキストボックスの値を変更した場合、そのテキストボックスを更新したいのですが、動的に変更する必要があります。しかし、それは起こっていません

私もelse条件を試しました

 else
        {
            MessageBox.Show("Enter Value");

            this.Controls.Clear();
            this.Controls.Clear();

        }

ただし、このフォームのすべての値が削除されます。

生成されたテキストボックスのみを削除するにはどうすればよいですか

更新 ここで、@New Developerのアイデアに従って変更を加えました

 if (!string.IsNullOrEmpty(textBoxInput.Text))
        {
            //Get the number of input text boxes to generate
            int inputNumber = Int32.Parse(textBoxInput.Text);

            if (inputTextBoxes != null && inputTextBoxes.Count > inputNumber)
            {
                int removecount = inputTextBoxes.Count - inputNumber;

                for (int i = 0; i < removecount; i++)
                {
                    TextBox t = inputTextBoxes[inputTextBoxes.Count - 1];
                    inputTextBoxes.RemoveAt(inputTextBoxes.Count - 1);
                    t.Dispose();
                }

                return;
            }


            if (inputlabels != null && inputlabels.Count > inputNumber)
            {
                int removecount2 = inputlabels.Count - inputNumber;

                for (int i = 0; i < removecount2; i++)
                {
                    Label l = inputlabels[inputlabels.Count - 1];
                    inputlabels.RemoveAt(inputlabels.Count - 1);
                    l.Dispose();
                }

                return;
            }

            //Generate labels and text boxes
            for (int i = 1; i <= inputNumber; i++)
            {
                //Create a new label and text box
                Label labelInput = new Label();
                TextBox textBoxNewInput = new TextBox();

                //Initialize label's property
                labelInput.Text = "Product" + i;
                labelInput.Location = new Point(30, textBoxInput.Bottom + (i * 30));
                labelInput.AutoSize = true;

                //Initialize textBoxes Property
                textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3);

                //Add the newly created text box to the list of input text boxes
                inputTextBoxes.Add(textBoxNewInput);
                inputlabels.Add(labelInput);

                //Add the labels and text box to the form
                this.Controls.Add(labelInput);
                this.Controls.Add(textBoxNewInput);
            }
        }
    }

また、追加されました

    List<TextBox> inputTextBoxes = new List<TextBox>();
    List<Label> inputlabels = new List<Label>();

ここでは機能しますが、値は毎回変わります

4

6 に答える 6

1

これを行う2つの方法:

1つの方法は、テキストボックスとラベルを作成するときに、それらへのポインタのリストを維持することです。

クラス定義に、プライベートリスト変数を追加します。

public partial class Form1 : Form
{
    private List<TextBox> generatedTextboxes = new List<TextBox>();
    private List<Label> generatedLabels = new List<Label>();

....

次に、それらを作成するときに、それらをリストに追加します。

//Generate labels and text boxes
for (int i = 1; i <= inputNumber; i++)
{
    //Create a new label and text box
    Label labelInput = new Label();
    TextBox textBoxNewInput = new TextBox();
    //Keep track of the references
    generatedTextboxes.Add(textBoxNewInput);
    generatedLabels.Add(labelInput );
    ....

今、あなたがそれらを削除したいとき:

for (int i = 1; i <= generatedTextboxes.Count; i++)
{
    this.Controls.Remove(generatedTextboxes[i]);
    this.Controls.Remove(generatedLabels[i]);
}

generatedTextboxes.Clear();
generatedLabels.Clear();

別の方法は、Panelフォームにコントロールを配置し、メインフォームに直接ではなく、その上に新しいテキストボックス/ラベルを追加することです。次に、panel1.Controls.Clear()-を実行して、パネルからコントロールをクリアします。

于 2012-12-31T10:13:48.657 に答える
1

編集
申し訳ありませんが、私のコードにエラーがありました。これは絶対に大丈夫なはずです。これを試して。これでもうまくいかない場合は、お知らせください。

    List<TextBox> inputTextBoxes =  new List<TextBox>();

    private void textBoxInput_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(textBoxInput.Text))
        {
            //Get the number of input text boxes to generate
            int inputNumber = Int32.Parse(textBoxInput.Text);
            if (inputTextBoxes != null && inputTextBoxes.Count > inputNumber)
            {
                int removecount = inputTextBoxes.Count - inputNumber;

                for (int i = 0; i < removecount; i++)
                {
                    TextBox t = inputTextBoxes[inputTextBoxes.Count-1];
                    inputTextBoxes.RemoveAt(inputTextBoxes.Count - 1);
                    t.Dispose();
                }

                return;
            }                

            //Generate labels and text boxes
            for (int i = 1; i <= inputNumber; i++)
            {
                //Create a new label and text box
                Label labelInput = new Label();
                TextBox textBoxNewInput = new TextBox();

                //Initialize label's property
                labelInput.Text = "Product" + i;
                labelInput.Location = new Point(30, textBoxInput.Bottom + (i * 30));
                labelInput.AutoSize = true;

                //Initialize textBoxes Property
                textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3);

                //Add the newly created text box to the list of input text boxes
                inputTextBoxes.Add(textBoxNewInput);

                //Add the labels and text box to the form
                this.Controls.Add(labelInput);
                this.Controls.Add(textBoxNewInput);
            }
        } 
    }
于 2012-12-31T10:56:28.523 に答える
1

これが新しいコードです

        if (!string.IsNullOrEmpty(textBoxInput.Text))
        {
            //Get the number of input text boxes to generate
            int inputNumber = Int32.Parse(textBoxInput.Text);

            if (inputTextBoxes != null && inputTextBoxes.Count > inputNumber)
            {
                int removecount = inputTextBoxes.Count - inputNumber;

                for (int i = 0; i < removecount; i++)
                {
                    TextBox t = inputTextBoxes[inputTextBoxes.Count - 1];
                    inputTextBoxes.RemoveAt(inputTextBoxes.Count - 1);
                    t.Dispose();

                    Label l = inputlabels[inputlabels.Count - 1];
                    inputlabels.RemoveAt(inputlabels.Count - 1);
                    l.Dispose();
                }

                return;
            }

        //Generate labels and text boxes
        for (int i = 1; i <= inputNumber; i++)
        {
            //Create a new label and text box
            Label labelInput = new Label();
            TextBox textBoxNewInput = new TextBox();

            //Initialize label's property
            labelInput.Text = "Product" + i;
            labelInput.Location = new Point(30, textBoxInput.Bottom + (i * 30));
            labelInput.AutoSize = true;

            //Initialize textBoxes Property
            textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3);

            //Add the newly created text box to the list of input text boxes
            inputTextBoxes.Add(textBoxNewInput);
            inputlabels.Add(labelInput);

            //Add the labels and text box to the form
            this.Controls.Add(labelInput);
            this.Controls.Add(textBoxNewInput);
        }
    }  

それでも問題がある場合はお知らせください。

于 2013-01-02T10:29:34.710 に答える
0

コントロールをフォームにドラッグドロップすると、内部のVisualStudioによってそれらのコントロールを追加するコードが作成されます。これらは、テキストボックスの作成方法と同様に作成されるため、「生成された」と見なすコントロールを識別する方法を導入する必要があります。

1つの方法は、これらのコントロールをに追加してList<Control>、「自分の」コントロールの参照を保持することです。

もう1つの方法はPanel、生成されmyPanel.Controls.Clear()たコントロールを追加するにを追加することです。これにより、追加されたコントロールのみをクリアするために使用できます。

于 2012-12-31T10:12:14.720 に答える
0

独自の変数を使用できます

inputTextBoxes.Add(textBoxNewInput);

テキストボックスを削除するには

else
{
  MessageBox.Show("Enter Value");
  this.Controls.Remove(inputTextBoxes[YourIndex/NameOfControl]);
 }
于 2012-12-31T10:13:47.257 に答える
0

追加したコントロールを追跡してから、remove を呼び出す必要があります。

this.Controls.Remove(labelInput);
this.Controls.Remove(textBoxNewInput);

追跡できる方法はいくつかあります。

  • プライベート フィールドを作成する
  • それらを辞書に保存する
  • コントロールを一意の識別子でタグ付けして、もう一度見つけられるようにします。

どちらを使用するかはあなた次第です。

于 2012-12-31T10:11:23.547 に答える