1

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.

Dylan
  • 47
  • 6
  • Not sure if I understood it right, but you can use `Tag` of either `ComboBox` or `NumericUpDown` control to hold instance of *linked* control, this way in the event you can extract from tag related control and check its value. Another option is to store added controls in collection (`Dictionary`?) and obtain linked control by using key lookup. – Sinatr Jun 24 '15 at 13:51
  • @Sinatr I've tried tags, but I can't figure out how to match the combobox tag with the NumericUpDown tag. I get the NumericUpDown by using (sender as NumericUpDown).Tag but I can't with the combobox as it's not the sender. – Dylan Jun 24 '15 at 15:06
  • If you get `var numericUpDown = (NumericUpDown)sender;`, then after that: `var comboBox = (ComboBox)numericUpDown.Tag;` – Sinatr Jun 24 '15 at 15:16
  • @Sinatr How can I use these to find the selected index of the combo box with the same tag as the active numericUpDown? – Dylan Jun 24 '15 at 15:37

2 Answers2

1

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);
}
Tim Pohlmann
  • 4,140
  • 3
  • 32
  • 61
  • I'm a little confused about what this code actually does/where to use it. It looks a little more advanced than what I've actually used. Could you give me a simple run down? – Dylan Jun 24 '15 at 15:05
  • `(sender, args) => { some code }` is a [Lambda Expression](https://msdn.microsoft.com/de-de/library/bb397687.aspx) representing an anonymous `EventHandler` which I add to the `ValueChanged` `Event`. If you are not familiar with the concept the following links could be useful: ["Lambda Expressions"](https://msdn.microsoft.com/de-de/library/bb397687.aspx) ["Anonymous Methods"](https://msdn.microsoft.com/de-de/library/0yw3tz5k.aspx) ["Using Lambda Expressions for EventHandlers"](http://stackoverflow.com/questions/2465040/using-lambda-expressions-for-event-handlers) – Tim Pohlmann Jun 25 '15 at 06:59
1

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;
    ...
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319