Good day!

I have a combo box which is readonly. It is loaded with data from the database. However, there is a point that I need to set text on it, and those text is equal to one which is in its list aleady. The below code produces runtime error as my combo box is readonly. Is there any workaround to solve this issue?

combo1.text="Beach Pricee"

Thank you for giving time!

Recommended Answers

All 2 Replies

You can do it by retriving the listindex value of that particular text, which you tried to select by default.
Make a function to get the SelectedIndex.
It should be like

Privat Function CheckIndex(ByVal key As String) As Integer
    Dim Result As Integer = -1

    For i As Integer = 0 To ComboBox1.Items.Count - 1
        If ComboBox1.Items(i) = key Then
            Result = i
            Exit For
        End If
    Next

    Return Result
End Function

Call this function to retrive the listindex.

ComboBox1.SelectedIndex = CheckIndex("Beach Pricee")

Hope it should help you, though this is in vb2010. Please make changes as per your requirment.
Changes like ComboBox would be Combo, SelectedIndex would be listindex and
Items would be ListItems.

thanks Shark 1. listindex works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.