问题
i want to compare the selecteditem of combobox with the selecteditem of other combobox for this i have registered all the comboboxes in a list and named it "panel1kilist" now the problem i am facing is that when there are same items in two comboboxes first the messagebox shows "no mattch found" and then it shows "match found" actually it goes to the else statement of inner loop first and then to if statement kindly help
private void button1_Click(object sender, EventArgs e)
{
bool check = false;
bool check1 = false;[![in this image you can see that there are two same items but message is showing "no match found"][1]][1]
try[![after clicking on ok button of message box showing "no match found" this message box shows up][1]][1]
{
for (int i = 1; i < panel1kilist.Count; i++)
{
for (int j = i + 1; j < panel1kilist.Count; j++)
{
if (panel1kilist[i].SelectedItem.ToString() == panel1kilist[j].SelectedItem.ToString())
{
if (check == false)
{
MessageBox.Show("match found");
}
check = true;
}
else
{
if (check1 == false)
{
MessageBox.Show("no match found");
}
check1 = true;
}
}
}
}
catch (System.NullReferenceException)
{
MessageBox.Show("please fill all the boxes first");
}
}
回答1:
Your question is not really clear but I still try to give you some help. As mentioned in the comments there are several small issues in your code:
1. The outter for-loop
for (int i = 1; i < panel1kilist.Count; i++)
You define i = 1, which means you skip the very first item in your panel1kilist because lists and arrays start at index 0
2. Use of your bool variables
if (panel1kilist[i].SelectedItem.ToString() == panel1kilist[j].SelectedItem.ToString())
{
if (check == false)
{
MessageBox.Show("match found");
}
check = true;
}
else
{
if (check1 == false)
{
MessageBox.Show("no match found");
}
check1 = true;
}
You define your bool variables before starting your for-loops. So whenever you set your variables check and check1 to true, the if conditions check == false and check1 == false will never return true anymore. So you will never get any message except the very first "Match found" and "no match found".
As it is not quite clear what you intended to do with your bool variables, you could just remove those if conditions and only write:
if (panel1kilist[i].SelectedItem.ToString() == panel1kilist[j].SelectedItem.ToString())
{
MessageBox.Show("match found");
}
else
{
MessageBox.Show("no match found");
}
3. Small code improvements
Instead of using the "standard" for-loop, you could use the foreach-loop:
foreach (var combobox in panel1kilist)
{
foreach (var comboboxToMatch in panel1kilist.Skip(1))
{
if(combobox.SelectedItem.ToString() == comboboxToMatch.SelectedItem.ToString())
{
MessageBox.Show("match found");
}
else
{
MessageBox.Show("no match found");
}
}
}
Advantages:
-> You do not need to take care of any int declarations and how long the for-loop is meant to be running
-> You directly make use of your objects in runtime and have them at hand if you want to process them further
来源:https://stackoverflow.com/questions/59863686/compare-the-items-in-list-in-c-sharp