I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.
Consider, I have a list of 10 combobox whose values will be selected by user.
The combobox values are:
- Finding
- No Finding
- Incomplete
- Skipped.
Now I have a single final combobox which contains the following values.
- Finding
- No Finding
- Incomplete
- Skipped Reviewed
- Skipped Not reviewed
The final combobox should be populated depend upon the following logic
- If all values are "
Finding
", then the the final combobox should be "Finding
" - If all values are "
No_Finding
", then the the final combobox should be "No_Finding
" - If all values are "
InComplete
", then the the final combobox should be "InComplete
" If all values are "
Skipped
", then the the final combobox should be "Skipped Not Reviewed
".If any value is "
Finding
", then the final combobox should be "Finding
".- If any value is "
Incomplete
", then the final combobox should be "Incomplete
". - If any value is "
Skipped
", then the final combobox should be "Skipped Reviewed
".
With respect to the above logic conditions I have written the following that works fine.
After writing the code I feel it's not cleaner and not an easily understandable solution.
What I need now whether there is any way to refactor the following lines of code.
public void selectfinalComboxValue()
{
List<string> list_of_combobox = new List<string>();
//txtBoxValLines is an user input
for (int i = 0; i < int.Parse(txtboxvalLines.Text); i++)
{
string cmbboxValue = ((ComboBox)panel1.Controls["Add_combobox" + (i).ToString()]).Text;
list_of_combobox.Add(cmbboxValue);
}
List<string> distinct = list_of_combobox.Distinct().ToList();
if (distinct.Any(str => str.Contains("InComplete")))
{
cmbFinalStatus.SelectedIndex = 2;
cmbFinalStatus.Enabled = false;
}
else if (distinct.Any(str => str.Equals("Finding")))
{
cmbFinalStatus.SelectedIndex = 0;
cmbFinalStatus.Enabled = false;
}
else
{
cmbFinalStatus.SelectedIndex = -1;
cmbFinalStatus.Enabled = true;
}
if (distinct.Count().ToString() == "1")
{
if (distinct.Any(str => str.Equals("Skipped")))
{
cmbFinalStatus.SelectedIndex = 4;
cmbFinalStatus.Enabled = false;
}
else if (distinct.Any(str => str.Equals("No Finding")))
{
cmbFinalStatus.SelectedIndex = 1;
cmbFinalStatus.Enabled = false;
}
}
else {
if (distinct.Any(str => str.Contains("Skipped")))
{
cmbFinalStatus.SelectedIndex = 3;
cmbFinalStatus.Enabled = false;
}
}
}
list_of_combobox
have 5Finding
and 5Incomplete
? \$\endgroup\$