Flow Layout Panel Population C#

406 views Asked by At

I'm trying to populate a Flow Layout Panel with ComboBoxes and NumbericUpDowns. The problem I'm having is using both the new NumbericUpDowns with the new ComboBoxes. Here is how I'm generating the ComboBoxes and NumericUpDowns.

// This int increments each time the code is run. It's located outside of the method below.
int captchaID = 0;

.

// Textboxes that are only for the UI, no code interaction based on text input.
string textboxText = "captchaTextbox";
TextBox newTextbox = new TextBox();
newTextbox.Name = captchaID.ToString() + textboxText;
newTextbox.Text = "";
newTextbox.Width = 175;
itemFlowPanel.Controls.Add(newTextbox);


// Combo Boxes
string comboBoxText = "captchaComboBox";
ComboBox newComboBox = new ComboBox();
newComboBox.Name = captchaID.ToString() + comboBoxText;
newComboBox.Width = 50;
newComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
itemFlowPanel.Controls.Add(newComboBox);

// This array holds my strings that are added to each ComboBox
string[] skills = new string[6];
skills[0] = "STR";
skills[1] = "DEX";
skills[2] = "CON";
skills[3] = "INT";
skills[4] = "WIS";
skills[5] = "CHA";

// This for loop is just populating my ComboBox with the array.
for (int i = 0; i < skills.Length; i++)
{
    newComboBox.Items.Add(skills[i]);
}

// Numeric Up Downs
string numericUpDownText = "captchaNumericUpDown";
NumericUpDown newNumericUpDown = new NumericUpDown();
newNumericUpDown.Name = captchaID.ToString() + numericUpDownText;
newNumericUpDown.Width = 50;
newNumericUpDown.ValueChanged += new EventHandler(captchaNumericUpDown_Click);
newNumericUpDown.ValueChanged += new EventHandler(captchaNumericUpDown_ValueChanged);
itemFlowPanel.Controls.Add(newNumericUpDown);
captchaID++;

With the current code, I'm able to edit an EventHandler that each NumericUpDown contains, but I haven't found a way to make it able to read it's corresponding combobox (which increment together with captchaID).

What I'd like to be able to do, is create a new unique event for each, but if that's not possible, a way to check the ID of the combobox would help as well.

2

There are 2 answers

0
Sinatr On BEST ANSWER

Here are quick solutions:

1) By using dictionary

Dictionary<NumericUpDown, ComboBox> _controls = new Dictionary<NumericUpDown, ComboBox>();

    // when you create comboBox - add entry with associated numericUpDown
    _controls.Add(numericUpDown1, comboBox1);

// now in the numericUpDown event you can get combobox like this
void numericUpDown_Whatever(object sender, WhateverEventArgs e)
{
    var numericUpDown = (NumericUpDown)sender;
    var comboBox = _controls[numericUpDown];
    // do something
    var selectedIndex = comboBox.SelectedIndex;
    ...
}

2) By using Tag

    // add combobox into numericUpDown Tag when you create them
    numericUpDown1.Tag = comboBox1;

// now in the numericUpDown event you can get combobox like this
void numericUpDown_Whatever(object sender, WhateverEventArgs e)
{
    var numericUpDown = (NumericUpDown)sender;
    var comboBox = (CombBox)numericUpDown.Tag;
    // do something
    var selectedIndex = comboBox.SelectedIndex;
    ...
}
2
Tim Pohlmann On

You can rewrite your captchaNumericUpDown_ events to take a ComboBox as an additional parameter and then call them like this:

newNumericUpDown.ValueChanged += (sender, args) =>
{
    captchaNumericUpDown_Click(sender, args, newComboBox);
}