1

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")
    {
    }
}
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
adam_em
  • 19
  • 2
  • you can use a [TableLayoutPanel](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.tablelayoutpanel?view=netframework-4.8). This is made for such a task – Mong Zhu Jan 29 '20 at 11:56
  • 1
    have a look at [adding-dynamic-controls-to-tablelayoutpanel-in-net-windows-form](https://stackoverflow.com/questions/42840406/adding-dynamic-controls-to-tablelayoutpanel-in-net-windows-form) ot [adding-controls-to-tablelayoutpanel-dynamically-during-runtime](https://stackoverflow.com/questions/13992429/adding-controls-to-tablelayoutpanel-dynamically-during-runtime) – Mong Zhu Jan 29 '20 at 11:57

1 Answers1

2

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

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37