adam_em
adam_em

Reputation: 19

How to dynamically add or remove textboxes?

I would like to make a tool on my form that would allow the user to add or remove textboxes using a [+] and [-] button. This should only be possible if the items "*.doc" or "*.docx" are selected in a ComboBox.

I have tried this for the .doc thingy:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    switch (cmbExtension.Text)
    {
        case "Other...":
            string extensionName = Interaction.InputBox("Enter the new extension's name (for example *.txt): ", "New extension!");
            File.AppendAllText(strPath, "\n" + extensionName);
            // string extensionFunction =  Interaction.InputBox("Enter the type of file it's supposed to be (for example Microsoft Word 2016): ", "Give us an idea.");

            cmbExtension.Items.Clear();
            LoadLines();
            break;
        case "*.doc":
            btnPlus.Show();

            break;
        case "*.docx":
            btnPlus.Show();

            break;
        default:
            btnPlus.Hide();
            break;
    }

    // As well as using similar code in these things, now empty:

    if (cmbExtension.Text == "Other...")
    {
    }
    if (cmbExtension.Text == "*.doc" || cmbExtension.Text == "*.docx")
    {
    }
}

Upvotes: 1

Views: 551

Answers (1)

Eliahu Aaron
Eliahu Aaron

Reputation: 4572

You can use a FlowLayoutPanel in which you add and remove the textboxes.

To add a TextBox to a FlowLayoutPanel (or any container control) use:

TextBox textBox = new TextBox();
this.flowLayoutPanel1.Controls.Add(textBox);

To remove the last added TextBox from FlowLayoutPanel (or any container control) use:

int count = this.flowLayoutPanel1.Controls.Count;
if (count > 0)
{
    this.flowLayoutPanel1.Controls[count - 1].Dispose();
}

Hear is an simple example with:

  • A FlowLayoutPanel with FlowDirection set to TopDown
  • Two buttons named btnPlus and btnMinus
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        cmbExtension.Items.Add("*.doc");
        cmbExtension.Items.Add("*.docx");
        cmbExtension.Items.Add("Other...");

        btnPlus.Hide();
        btnMinus.Hide();
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (cmbExtension.Text)
        {
            case "Other...":
                // ...
                btnPlus.Hide();
                btnMinus.Hide();
                break;
            case "*.doc":
                btnPlus.Show();
                btnMinus.Show();
                break;
            case "*.docx":
                btnPlus.Show();
                btnMinus.Show();
                break;
            default:
                btnPlus.Hide();
                btnMinus.Hide();
                break;
        }
    }

    private void btnPlus_Click(object sender, EventArgs e)
    {
        TextBox textBox = new TextBox();
        this.flowLayoutPanel1.Controls.Add(textBox);
    }

    private void btnMinus_Click(object sender, EventArgs e)
    {
        int count = this.flowLayoutPanel1.Controls.Count;
        if (count > 0)
        {
            this.flowLayoutPanel1.Controls[count - 1].Dispose();
        }
    }
}

Snapshots:

enter image description here

Upvotes: 2

Related Questions