Assign a value to combobox depend upon other combobox value
up vote
2
down vote
favorite
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;
}
}
}
c# winforms
add a comment |
up vote
2
down vote
favorite
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;
}
}
}
c# winforms
What happens iflist_of_combobox
have 5Finding
and 5Incomplete
?
– Calak
2 days ago
@Calak In that case it is Incomplete.
– Vini
2 days ago
edited my code below, too show you more :)
– Calak
15 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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;
}
}
}
c# winforms
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;
}
}
}
c# winforms
c# winforms
edited 2 days ago
Calak
1,799214
1,799214
asked 2 days ago
Vini
183
183
What happens iflist_of_combobox
have 5Finding
and 5Incomplete
?
– Calak
2 days ago
@Calak In that case it is Incomplete.
– Vini
2 days ago
edited my code below, too show you more :)
– Calak
15 hours ago
add a comment |
What happens iflist_of_combobox
have 5Finding
and 5Incomplete
?
– Calak
2 days ago
@Calak In that case it is Incomplete.
– Vini
2 days ago
edited my code below, too show you more :)
– Calak
15 hours ago
What happens if
list_of_combobox
have 5 Finding
and 5 Incomplete
?– Calak
2 days ago
What happens if
list_of_combobox
have 5 Finding
and 5 Incomplete
?– Calak
2 days ago
@Calak In that case it is Incomplete.
– Vini
2 days ago
@Calak In that case it is Incomplete.
– Vini
2 days ago
edited my code below, too show you more :)
– Calak
15 hours ago
edited my code below, too show you more :)
– Calak
15 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
I won't give you the full response, because your code lack of context (we didn't know nothing about cmbFinalStatus...
).
Without using distinct
result
value is set if case case 1,2,3,4 match.
List<string> all_of = new List<string>(){"Skipped", "Incomplete", "No_Finding", "Finding"};
var result = all_of.FirstOrDefault(lhs => list_of_combobox.All(rhs => rhs.Equals(lhs)));
if (
result
value is set if case 5,6,7 match.
List<string> first_of = new List<string>(){"Skipped", "Incomplete", "Finding"};
var result = first_of.FirstOrDefault(lhs => list_of_combobox.Any(rhs => rhs.Equals(lhs)));
Edit: Putting all together, we got:
// for... populating list_of_combobox
// ...
var str = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.All(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Length != 0)
{
if (str.Equals("Skipped")) str = "Skipped Not Reviewed";
}
else
{
str = new List<string>(){"Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.Any(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Equals("Skipped")) str = "Skipped Reviewed";
}
cmbFinalStatus.SelectedIndex = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped Reviewed", "Skipped Not Reviewed"}
.IndexOf(str);
cmbFinalStatus.Enabled = (cmbFinalStatus.SelectedIndex < 0);
Awesome work. Thank you.
– Vini
13 hours ago
add a comment |
up vote
1
down vote
I think you might be able to eliminate creating the distinct list and the nested ifs by first checking for the All() condtions, then later check for Any()
var items = list_of_combobox.ToList();
if (items.All(str => str.Equals("Finding")))
{
// assign final comboxbox
}
else if (items.Any(str => str.Equals("Finding")))
Also, you might want to try creating an enum of the possible values, adding these values to the ComboBox, then getting/setting the SelectedItem property rather than dealing with indexes.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I won't give you the full response, because your code lack of context (we didn't know nothing about cmbFinalStatus...
).
Without using distinct
result
value is set if case case 1,2,3,4 match.
List<string> all_of = new List<string>(){"Skipped", "Incomplete", "No_Finding", "Finding"};
var result = all_of.FirstOrDefault(lhs => list_of_combobox.All(rhs => rhs.Equals(lhs)));
if (
result
value is set if case 5,6,7 match.
List<string> first_of = new List<string>(){"Skipped", "Incomplete", "Finding"};
var result = first_of.FirstOrDefault(lhs => list_of_combobox.Any(rhs => rhs.Equals(lhs)));
Edit: Putting all together, we got:
// for... populating list_of_combobox
// ...
var str = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.All(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Length != 0)
{
if (str.Equals("Skipped")) str = "Skipped Not Reviewed";
}
else
{
str = new List<string>(){"Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.Any(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Equals("Skipped")) str = "Skipped Reviewed";
}
cmbFinalStatus.SelectedIndex = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped Reviewed", "Skipped Not Reviewed"}
.IndexOf(str);
cmbFinalStatus.Enabled = (cmbFinalStatus.SelectedIndex < 0);
Awesome work. Thank you.
– Vini
13 hours ago
add a comment |
up vote
1
down vote
accepted
I won't give you the full response, because your code lack of context (we didn't know nothing about cmbFinalStatus...
).
Without using distinct
result
value is set if case case 1,2,3,4 match.
List<string> all_of = new List<string>(){"Skipped", "Incomplete", "No_Finding", "Finding"};
var result = all_of.FirstOrDefault(lhs => list_of_combobox.All(rhs => rhs.Equals(lhs)));
if (
result
value is set if case 5,6,7 match.
List<string> first_of = new List<string>(){"Skipped", "Incomplete", "Finding"};
var result = first_of.FirstOrDefault(lhs => list_of_combobox.Any(rhs => rhs.Equals(lhs)));
Edit: Putting all together, we got:
// for... populating list_of_combobox
// ...
var str = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.All(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Length != 0)
{
if (str.Equals("Skipped")) str = "Skipped Not Reviewed";
}
else
{
str = new List<string>(){"Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.Any(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Equals("Skipped")) str = "Skipped Reviewed";
}
cmbFinalStatus.SelectedIndex = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped Reviewed", "Skipped Not Reviewed"}
.IndexOf(str);
cmbFinalStatus.Enabled = (cmbFinalStatus.SelectedIndex < 0);
Awesome work. Thank you.
– Vini
13 hours ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I won't give you the full response, because your code lack of context (we didn't know nothing about cmbFinalStatus...
).
Without using distinct
result
value is set if case case 1,2,3,4 match.
List<string> all_of = new List<string>(){"Skipped", "Incomplete", "No_Finding", "Finding"};
var result = all_of.FirstOrDefault(lhs => list_of_combobox.All(rhs => rhs.Equals(lhs)));
if (
result
value is set if case 5,6,7 match.
List<string> first_of = new List<string>(){"Skipped", "Incomplete", "Finding"};
var result = first_of.FirstOrDefault(lhs => list_of_combobox.Any(rhs => rhs.Equals(lhs)));
Edit: Putting all together, we got:
// for... populating list_of_combobox
// ...
var str = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.All(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Length != 0)
{
if (str.Equals("Skipped")) str = "Skipped Not Reviewed";
}
else
{
str = new List<string>(){"Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.Any(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Equals("Skipped")) str = "Skipped Reviewed";
}
cmbFinalStatus.SelectedIndex = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped Reviewed", "Skipped Not Reviewed"}
.IndexOf(str);
cmbFinalStatus.Enabled = (cmbFinalStatus.SelectedIndex < 0);
I won't give you the full response, because your code lack of context (we didn't know nothing about cmbFinalStatus...
).
Without using distinct
result
value is set if case case 1,2,3,4 match.
List<string> all_of = new List<string>(){"Skipped", "Incomplete", "No_Finding", "Finding"};
var result = all_of.FirstOrDefault(lhs => list_of_combobox.All(rhs => rhs.Equals(lhs)));
if (
result
value is set if case 5,6,7 match.
List<string> first_of = new List<string>(){"Skipped", "Incomplete", "Finding"};
var result = first_of.FirstOrDefault(lhs => list_of_combobox.Any(rhs => rhs.Equals(lhs)));
Edit: Putting all together, we got:
// for... populating list_of_combobox
// ...
var str = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.All(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Length != 0)
{
if (str.Equals("Skipped")) str = "Skipped Not Reviewed";
}
else
{
str = new List<string>(){"Finding", "InComplete", "Skipped"}
.Where(lhs => list_of_combobox.Any(rhs => lhs.Equals(rhs)))
.FirstOrDefault() ?? string.Empty;
if (str.Equals("Skipped")) str = "Skipped Reviewed";
}
cmbFinalStatus.SelectedIndex = new List<string>(){"Finding", "No Finding", "InComplete", "Skipped Reviewed", "Skipped Not Reviewed"}
.IndexOf(str);
cmbFinalStatus.Enabled = (cmbFinalStatus.SelectedIndex < 0);
edited 15 hours ago
answered 2 days ago
Calak
1,799214
1,799214
Awesome work. Thank you.
– Vini
13 hours ago
add a comment |
Awesome work. Thank you.
– Vini
13 hours ago
Awesome work. Thank you.
– Vini
13 hours ago
Awesome work. Thank you.
– Vini
13 hours ago
add a comment |
up vote
1
down vote
I think you might be able to eliminate creating the distinct list and the nested ifs by first checking for the All() condtions, then later check for Any()
var items = list_of_combobox.ToList();
if (items.All(str => str.Equals("Finding")))
{
// assign final comboxbox
}
else if (items.Any(str => str.Equals("Finding")))
Also, you might want to try creating an enum of the possible values, adding these values to the ComboBox, then getting/setting the SelectedItem property rather than dealing with indexes.
add a comment |
up vote
1
down vote
I think you might be able to eliminate creating the distinct list and the nested ifs by first checking for the All() condtions, then later check for Any()
var items = list_of_combobox.ToList();
if (items.All(str => str.Equals("Finding")))
{
// assign final comboxbox
}
else if (items.Any(str => str.Equals("Finding")))
Also, you might want to try creating an enum of the possible values, adding these values to the ComboBox, then getting/setting the SelectedItem property rather than dealing with indexes.
add a comment |
up vote
1
down vote
up vote
1
down vote
I think you might be able to eliminate creating the distinct list and the nested ifs by first checking for the All() condtions, then later check for Any()
var items = list_of_combobox.ToList();
if (items.All(str => str.Equals("Finding")))
{
// assign final comboxbox
}
else if (items.Any(str => str.Equals("Finding")))
Also, you might want to try creating an enum of the possible values, adding these values to the ComboBox, then getting/setting the SelectedItem property rather than dealing with indexes.
I think you might be able to eliminate creating the distinct list and the nested ifs by first checking for the All() condtions, then later check for Any()
var items = list_of_combobox.ToList();
if (items.All(str => str.Equals("Finding")))
{
// assign final comboxbox
}
else if (items.Any(str => str.Equals("Finding")))
Also, you might want to try creating an enum of the possible values, adding these values to the ComboBox, then getting/setting the SelectedItem property rather than dealing with indexes.
answered 2 days ago
C.M.
1173
1173
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208151%2fassign-a-value-to-combobox-depend-upon-other-combobox-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What happens if
list_of_combobox
have 5Finding
and 5Incomplete
?– Calak
2 days ago
@Calak In that case it is Incomplete.
– Vini
2 days ago
edited my code below, too show you more :)
– Calak
15 hours ago