hi how to use combo box selected item

Harry Potter 21 Reputation points
2022-11-08T13:20:38.38+00:00

hi im beginner of c#
and i want to select my item but i got error

heres my code
if (comboBox1.SelectedItem "test";)

heres the error
Cannot implicitly convert type 'object' to 'bool'

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-11-23T09:42:02.447+00:00

    Hi @Harry Potter ,
    According to ComboBox.SelectedText Property.

    When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected. In this case, getting the SelectedText property retrieves an empty string, and setting the SelectedText property adds the specified value to the beginning of the text.

    So you might need metroComboBox1.SelectedItem = "Oxygen Api" or metroComboBox1.SelectedItem.toString = "Oxygen Api".
    Best Regards.
    Jiachen Li


    If the answer 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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-11-08T13:51:00.447+00:00

    Use this

    if (comboBox1.SelectedText == "test")  
    {  
          
    }  
    else  
    {  
          
    }  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.