C# if comobox1 is less then 2 combobox 2 say Fail

Booney 166 Reputation points
2021-03-29T22:35:43.257+00:00

I have 2 combobox if CB1 is < 2 CB say Fail. This is what I have so far.

private void cb_score_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cb_score.SelectedItem == "2")
            {
                cb_pass_fail.Text = "Fail";
            }     
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,833 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,755 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,274 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-03-30T03:07:41.127+00:00

    Hi Booney-3048,
    First convert the value of combobox1 to int type via Int32.TryParse method and then compare with 2.
    Please refer to the following code:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        int value;  
        if (Int32.TryParse(comboBox1.Text, out value))  
        {  
      
            if (value < 2)  
            {  
      
                comboBox2.Text = "Fail";  
            }  
            else  
            {  
                comboBox2.Text = "";  
            }  
        }  
        else   
        {  
            comboBox2.Text = "";  
        }  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful