-1

==> PLEASE NOTE: I -- the OP -- STRONGLY DISAGREE THIS IS A DUPLICATE <==
This question has nothing to do with "finding a control" which is the linked "duplicate" question/answer. This question, as the title states, is about incorporating a variable into the switch statement's Case.

I’m a C# novice & new to this forum; apologies if some of my terminology is incorrect or I don't yet fully understand the forum's spoken and unspoken rules.

I have a winForm used as a Prize definition template. It has 10 rows of 5 controls resulting in a table-like presentation. Selection of a Prize type in a combobox dropdown results in displaying a user input textbox specific to the selection.

[[ e.g., If the user selects an "Amount of Prize" Prize type in the dropdown a textbox for entering a monetary value is displayed & if "Percent of Budget" selected a textbox/label combination for entering a percentage is displayed. The user can only enter an Amount or a Percent and textboxes associated with Prize type selection are displayed in the same form space. ]]

The switch statement is used for linking choice and display (showing only first row event):

        private void PrizeTypeComboBox00_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectPrizeType = (string)TypeComboBox00.SelectedItem;
            switch (selectPrizeType)
            {
                case "Amount of Prize":   { AmountTextBox00.Visible = true; } break;
                case "Percent of Budget": { PercentTextBox00.Visible = true; } break;
                default: break;
            }
        }

I need to replace having 10 "PrizeTypeCombobox0X_ComboBoxChanged" event methods which are identical except for the relevant control identifiers with a single "AnyComboBox_ComboBoxChanged" event method. The control naming convention is Name00-09 so replacing the final digit of the control name with a variable (Name0X) in the "Case" statement should be possible.

I’ve tried a number of permutations utilizing various strings:

 case 1:   { controlName0[variable].action; }
 case 1:   { controlName0{variable}.action; } 
 case 1:   { controlName0(variable).action; }
 case 1:   { $"controlName0{variable}.action; }
 case 1:   { string.Format("{0}{1}{2}",controlName,variable,action); }
 case 1:   { switchOn(mstrTag, AmtSeleted); }

I’ve also explored and attempted to use – codeDOM, Dynamic Expresso, Action<>, Func<>, reflection and ICommand.

I’m sure there must be a simple straight-forward approach to get this working but I’ve not found it in 2 days trying.

Any assistance will be much appreciated.

Art Hansen
  • 57
  • 8
  • @Oliver Rogier thanx, but no it doesn't. I must have posed the question poorly. All I'm trying to do is change case "Amount of Prize": { AmountTextBox00.Visible = true; } break; TO case "Amount of Prize": { AmountTextBox0[INSERT VARIABLE].Visible = true; } break; I'll clarify the question. – Art Hansen Jan 24 '21 at 14:56
  • @.ArtHansen Thus using the duplicate should do that as exposed by the @.AshkanMobayenKhiabani answer (I did not really read it, nor your code, sorry, but your comment confirm my understanding of your goal, it seems). –  Jan 24 '21 at 14:59
  • @OlivierRogier Excuse my ignorance but I'm not making the connection between "using a duplicate" and incorporating a variable into the switch{case} statement; please clarify. – Art Hansen Jan 24 '21 at 16:16
  • @.ArtHansen Once you got the ref of the instance of an object using its variable code string name, and once you casted it to the desired type, as explained in such duplicate and the answer below, you are able to acheive your goal. But you can't write what you tried because for eg `controlName0[variable]` is an indexed array and it can't be a concatenation of a code static string name with a runtime string variable value... C# does not support that. It is the same with `$"controlName0{variable}".action` : you need to find control by name `$"controlName0{variable}`, cast, and next call `action`. –  Jan 25 '21 at 09:38

1 Answers1

1

You may create your controls programmatically and make an array:

var combos = Enumerable.Range(0,100)
.Select((x, i) => new ComboBox {Name = $"TypeComboBox{i}", Visible = false, ...})
.ToArray();

or you may just get controls by name:

ComboBox GetCombo(int index)
{
    var name = "TypeComboBox" + index.ToString().PadLeft('0',2); 
    var combo = this.Controls[name];
    return combo;
}

it is also possible to assign your existing controls to an array:

var combos = parent.Controls.OfType<ComboBox>()
             .OrderBy(c => c.Name)
             .ToArray();  //parent can be your form or whatever 
                          //control that contains your comboboxes
                      
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • @ArtHansen have a look at my second solution, this was an example just for a combobox, you can select any control like that. – Ashkan Mobayen Khiabani Jan 24 '21 at 15:33
  • (I've deleted my first comment since it didn't help clarify) I'm not trying to programmatically create/find a particular control. I'm trying to incorporate a variable into a switch{case1, case2, case3} statement so the same statement can be used by many control events. – Art Hansen Jan 24 '21 at 16:28