1
votes

I have a VB.NET Windows Forms application that contains a combo box. Initially I have set the box's text property to "Select Department".

After the user selects an entry from the list, the text field of the box shows the item the user selected. After my code handles the processing for the item the user selected I would like to change the text field back to the original phrase ... "Select Department" ... but changing myCOMBO.Text has no effect. Still shows the last selected item.

How can I replace that selected item text?

3
Show your code first, without it we cant help you. The question is lacking the details to help you.zaggler

3 Answers

0
votes

Try using SelectedText property:

myCombo.SelectedText = "..."
0
votes

You might not realise it, but there might actually be a typo either in your code or in the combobox item. Keep in mind that when setting a combobox using the text property, the text you specify must match the combobox item precisely, so setting ComboBox1.text = "This Text" when the ComboBox item is "This Text" will fail.

However, depending on how the items are added and whether you already know at what index the "Select Department" entry is, You could just jump straight to ComboBox1.SelectedIndex = ... bearing in mind that the first index is 0, the second is 1 and so forth.

Alternatively, try finding the index of your Combobox to set it:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    If Not ComboBox1.Text.Trim() = "Select Department" Then

        'do your stuff here

        Dim the_index as integer = 0

        For a = 0 To ComboBox1.Items.Count - 1

            If ComboBox1.Items(a).ToString.Trim() = "Select Department" Then

               the_index = a

               Exit For

            End If

        Next

        ComboBox1.SelectedIndex = the_index

    End If

End Sub
-1
votes

I think you are not looking for changing the text of the combo box, but you are trying to fill the combobox items.

You have to create a new SQL command with the query you want, create a sqldatareader to fetch data from this query. Then assign it to myCOMBO.Datasource:

myCOMBO.Datasource = datareader.read()