Conditon statement for combo box control.

K. MOHANA PRIYA 56 Reputation points
2020-08-29T08:04:10.793+00:00

I am doing my window form project, in which I am using the combo box control.

private void button5_Click(object sender, EventArgs e)
{
con.Open();
if (comboBox1.SelectedItem.ToString() == "DOCTOR DETAILS")
{
Form2 f2 = new Form2();
f2.Show();
}
else if (comboBox1.SelectedItem.ToString() == "PATIENT DETAILS")
{
Form3 f3 = new Form3();
f3.Show();
}
else if (comboBox1.SelectedItem.ToString() == "OPTICAL DETAILS")
{
Form4 f4 = new Form4();
f4.Show();
}
else if (comboBox1.SelectedItem.ToString() == "FEEDBACK DETAILS")
{
Form5 f5 = new Form5();
f5.Show();
}
else
MessageBox.Show("no table is selected");
con.Close();
}

This is the code I have written when I debug it show form3,form4 and form5 but when I select the DOCTOR DETAILS in the combo box it doesn't show me the form1 instead it show message box that "no table is selected".
so anyone pls tell me what mistake I have made and how to rectify it.

Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
963 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,846 Reputation points
    2020-08-30T19:33:25.167+00:00

    Try this. Their might be a space on the end of the string

     if (comboBox1.SelectedItem.ToString().Trim() == "DOCTOR DETAILS")
     {
    

1 additional answer

Sort by: Most helpful
  1. Dylan Zhu-MSFT 6,406 Reputation points
    2020-08-31T05:15:30.993+00:00

    Hi KMOHANAPRIYA,

    Please confirm if the item is exactly equal to "DOCTOR DETAILS".

    You could define a value for selected item, then set a breakpoint at judgement condition to help you check it:
    21482-annotation-2020-08-31-10262328.jpg

    Best Regards,
    Dylan