I am stuck on Visual Basic problem regarding combo boxes. The question states:

Assume that the Simple combo box appears as shown and that the Sorted property is set to True. Give a statement or statements that will carry out the stated task.

The combo box contains:

Dante
Goethe
Moliere
Shakespeare

Delete every item beginning with the letter M. (The code should do the job even if additional items were added to the list.)

I tried just to get it to search first. This code does that, but it only searches the combo box for soley the letter M. Since I don't know how to delete the name from the combo box once it is found, I just put message boxes there. Is there a line of code that would delete the item once it was found? How do I have it search and then delete an item based on its first letter?

Private Sub btnM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnM.Click
        Dim found As Boolean = False
        For i As Integer = 0 To cboBox.Items.Count - 1
            If (cboBox.Items.Contains("M")) Then
                found = True
            End If
        Next
        If found Then
            MsgBox("M is in the list.")
        Else
            MsgBox("M not found.")
        End If
    End Sub

see this code :
add this code to Module :

Module Module1
    Public Sub AutoComplete(ByVal cbo As ComboBox, ByVal e As System.Windows.Forms.KeyEventArgs)
        Dim iIndex As Integer
        Dim sActual As String
        Dim sFound As String
        Dim bMatchFound As Boolean

        If Not cbo.Text = "" Then 'if the text is not blank then only proceed

            If e.KeyCode = Keys.Back Then
                cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1)
            End If

            If ((e.KeyCode = Keys.Left) Or _
             (e.KeyCode = Keys.Right) Or _
             (e.KeyCode = Keys.Up) Or _
             (e.KeyCode = Keys.Down) Or _
             (e.KeyCode = Keys.PageUp) Or _
             (e.KeyCode = Keys.PageDown) Or _
             (e.KeyCode = Keys.Home) Or _
             (e.KeyCode = Keys.End)) Then
                Return
            End If


            Do

                sActual = cbo.Text

                iIndex = cbo.FindString(sActual)
                If (iIndex > -1) Then '** FOUND SECTION **

                    sFound = cbo.Items(iIndex).ToString()
                    ' Select this item from the list.

                    cbo.SelectedIndex = iIndex
                    cbo.SelectionStart = sActual.Length
                    cbo.SelectionLength = sFound.Length
                    bMatchFound = True
                Else '** NOT FOUND SECTION **

                    If sActual.Length = 1 Or sActual.Length = 0 Then
                        cbo.SelectedIndex = 0
                        cbo.SelectionStart = 0
                        cbo.SelectionLength = Len(cbo.Text)
                        bMatchFound = True

                    Else
                        cbo.SelectionStart = sActual.Length - 1
                        cbo.SelectionLength = sActual.Length - 1
                        cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1)
                    End If

                End If

            Loop Until bMatchFound

        End If

    End Sub
End Module

Add this code to form :

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With ComboBox1.Items
            .Add("Jery")
            .Add("Victor")
            .Add("fitri")
            .Add("Sari")
            .Add("Dewi")
            .Add("Denok")
        End With

    End Sub

    Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
        Call AutoComplete(ComboBox1, e)
    End Sub

run the program. when you input 'S' in combo box then press down Arrow key it will automatically display "Sari"

for more explain, visit this site

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.