简体   繁体   English

WinForms - 根据 ComboBox 选择动态创建文本框

[英]WinForms - Dynamically create textbox based on ComboBox selection

I am trying to create a TextBox based on the selection on ComboBox dynamically based on the following steps:我正在尝试根据以下步骤动态创建基于ComboBox上的选择的TextBox

First step (Select a source from ComboBox ):第一步(从ComboBox选择一个源):

第一党卫军

Second step ( Textbox should appear based on ComboBox.SelectedValue ):第二步( Textbox应基于ComboBox.SelectedValue出现):

第二党卫军

Last step (A new ComboBox should appear below):最后一步(下面应该出现一个新的ComboBox ):

在此处输入图像描述

I have created a createTextBox function using the following code:我使用以下代码创建了一个createTextBox function:

public void createTextBox(int numPassenger)
{
    TextBox[] passengerBoxes = new TextBox[numPassenger];

    for (int u = 0; u < passengerBoxes.Count(); u++)
    {
        passengerBoxes[u] = new TextBox();
    }
    int i = 0;
    foreach (TextBox txt in passengerBoxes)
    {
        string name = "passenger" + i.ToString();

        txt.Name = name;
        txt.Text = name;
        txt.Location = new Point(244, 32 + (i * 28));
        txt.Visible = true;
        this.Controls.Add(txt);
        i++;
    }
}

Is there a way that I can modify my current function to adapt to the mentioned steps?有没有办法可以修改我当前的 function 以适应上述步骤? Additionally, how can I find the dynamically created TextBox ?此外,如何找到动态创建的TextBox

You can try the following code:您可以尝试以下代码:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        createTextBox(sender as ComboBox);
    }
    private void createTextBox(ComboBox cmb)
    {
        TextBox passengerBoxes = new TextBox();
        string name = cmb.Text;
        if (Controls.Find(name, true).Length == 0)
        {
            passengerBoxes.Name = name;
            passengerBoxes.Text = name;
            int textBoxCount = GetTextBoxCount();
            passengerBoxes.Location = new Point(244, 32 + (textBoxCount * 28));
            passengerBoxes.Visible = true;
            this.Controls.Add(passengerBoxes);

            if (cmb.Items.Count != 1)//last item remaining then we should not create new combo box
            {
                ComboBox newCombo = new ComboBox
                {
                    Location = new Point(cmb.Location.X, 32 + ((textBoxCount + 1) * 28))
                };

                foreach (string str in cmb.Items)
                    if (cmb.Text != str)
                        newCombo.Items.Add(str);

                newCombo.SelectedIndexChanged += comboBox1_SelectedIndexChanged;

                this.Controls.Add(newCombo);
            }
        }
        else
            MessageBox.Show("Textbox Already for the selected source " + name);
    }

    private int GetTextBoxCount()
    {
        int i = 0;
        foreach (Control ctl in this.Controls)
        {
            if (ctl is TextBox) i++;
        }
        return i;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM