==> 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.