0

I'm working on a project for school but I'm stuck at this moment. I want to dynamically add comboboxes to my flowlayoutpanel which depends of the value of a numeric up and down.

So if the numeric up down value is 2 that 2 comboboxes are dynamically made, but I can't really figure how to do this, this is what I was thinking of.

public partial class ...Form: Form
{
    ComboBox[] cbChoices;

    private void nudGuests_ValueChanged(object sender, EventArgs e)
    {
        flowPanel.Controls.Clear();

        for (int i = 1; i < nudGuests.Value; i++)
        {
            cbChoices[i] = new ComboBox();
            flowPanel.Controls.Add(cbChoices[i]);
        }
    }
 }

but I can't seem to get it to work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
timouwerkerk
  • 117
  • 1
  • 4
  • 10

1 Answers1

0

There are several problems here.

Firstly, your cbChoices array is not initialzed, so do `cbChoices = new ComboBoxnudGuests.Value;

Secondly, C# uses 0-indexing (starts counting arrays at the 0th element instead of the 1st), so use for (int i = 0 ....)

Thirdly, do you really need the array of ComboBoxes or can you just add them without storing them?

This should point you in the right direction.

Davio
  • 4,609
  • 2
  • 31
  • 58