Question Programatically select ITEM in LISTBOX if it CONTAINS text from a textbox

undertaxxx

New member
Joined
May 28, 2011
Messages
3
Programming Experience
Beginner
Hi,

I'm new to this forum, so please don't flame me if I post anything wrong..
I'm somewhat a "noob" at coding, but I try to learn..! (not just copy paste/leech/...)

My problem:

Can I let my program select an item in a listbox based on a string (f.e. textbox.text)?

So basicly, if an item in my listbox containst a certain string (text from a textbox), it should select that item..

I've been searching for a while, but nothing is doing the job.. Could anyone give me an example of this?

Thanks in advance!
- David
 
Last edited:
Try something like this:

Dim strSearch As String = "Item1"
ListBox1.SelectedIndex = ListBox1.Items.IndexOf(strSearch)
 
I've tried that but no success.

It could be my fault though.. Maybe I put it in the wrong place..

VB.NET:
 Private Sub btnInschrijvingAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInschrijvingAdd.Click
        '****************************
        '* Voeg Cursus toe aan listbox.
        '****************************
        ' Haal gegevens op
        If gcnn.State = ConnectionState.Closed Then gcnn.Open()
        Dim cmd As OleDbCommand
        Dim strCmd As String = "SELECT fldCursus_ID, fldCursusNaam, fldCursusPrijs FROM tblCursussen " & _
            "WHERE fldCursus_ID= " & CStr(dgvBestellingen.CurrentRow.Cells(0).Value)

        cmd = New OleDbCommand(strCmd, gcnn)

        Dim strNaam As String = ""
        Dim decPrijs As Decimal = 0
        Dim strCategorie As String = ""


        Dim rd As OleDbDataReader

        rd = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        If rd.HasRows Then
            Do While rd.Read
                strNaam = rd("fldCursusNaam")
                decPrijs = rd("fldCursusPrijs")
            Loop
        End If

        rd.Close()

        ' Vul listbox
        Dim strFmt As String = "{0,-15} {1,2} {2,10}"
        Dim strAantal As String = txtInschrijvingAantal.Text

        ' controleren op dubbels
        If fntDubbel(strNaam) Then
            Dim strMsg As String = "Deze cursus staat reeds in de lijst." & vbCrLf & _
                    "Wenst u het aantal aan te passen?"
            Dim res = MessageBox.Show(strMsg, "Dubbele waarde", MessageBoxButtons.YesNo)
            If res = DialogResult.No Then
                Exit Sub
            Else
                strMsg = "Geef het gewenste aantal in."
                strAantal = InputBox(strMsg, "Gewenst aantal", strAantal)
                If strAantal <> "" Then
                    subWijzigItem(strFmt, strNaam, strAantal, decPrijs)
                    Exit Sub
                End If
                MessageBox.Show("Gelieve een geldige waarde in te geven!", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            If (txtInschrijvingAantal.Text <> "") Then

                With lstInschrijvingen.Items
                    .Add(String.Format(strFmt, strNaam, strAantal, ((decPrijs * strAantal))))
                End With
            Else
                MessageBox.Show("Gelieve een aantal in te voeren.", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If

    End Sub

    Private Function fntDubbel(ByVal strNaam As String) As Boolean
        '****************************************
        '* Controleert winkelwagentje op dubbels.
        '****************************************
        For Each itm In lstInschrijvingen.Items
            If CStr(itm).Contains(strNaam) Then
                ' dubbel
                Return True
                lstInschrijvingen.SelectedIndex = lstInschrijvingen.FindString(strNaam)
            End If
        Next

        Return False

    End Function



    Private Sub subWijzigItem(ByVal strFmt As String, ByVal strNaam As String, ByVal strAantal As String, ByVal strPrijs As String)
        '*********************
        '*
        '*********************
        subVerwijderCursus()
        With lstInschrijvingen.Items
            .Add(String.Format(strFmt, strNaam, strAantal, (strPrijs * strAantal)))
        End With

    End Sub

    Private Sub btnVerwijderen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInschrijvingDelete.Click
        '*****************************
        '* Verwijder boek uit listbox.
        '*****************************
        subVerwijderCursus()

    End Sub

    Private Sub subVerwijderCursus()
        '*********************
        '*
        '*********************
        Try
            lstInschrijvingen.Items.Remove(lstInschrijvingen.SelectedItem)
            Dim i As Integer = 0
            With lstInschrijvingen
                i = .SelectedIndex
                .Items.RemoveAt(i)
            End With
        Catch

        End Try
    End Sub
So this is my code..

5023340642.jpg


my layout



If anyone has any questions, please ask them, I really need this to work..

Basicly: When I add a selected thing from the datagrid to the textbox, it checks if it's already there. If it's already there, it will promt a box to for the desired "aantal" ('aantal' means 'number of..' in Dutch. f.e.: 6 eggs, 'aantal' would be 6).


When I did change the number it would make a new item with the desired 'aantal'.

2877d1306602955-programatically-select-item-listbox-if-contains-text-textbox-2613322089.png



The thing is: I want to delete my previous entry....
 
It is a little hard to read :), but I believe that this is part of your issue:

VB.NET:
 Private Function fntDubbel(ByVal strNaam As String) As Boolean
        '****************************************
        '* Controleert winkelwagentje op dubbels.
        '****************************************
        For Each itm In lstInschrijvingen.Items
            If CStr(itm).Contains(strNaam) Then
                ' dubbel
                Return True
                lstInschrijvingen.SelectedIndex = lstInschrijvingen.FindString(strNaam)
            End If
        Next

        Return False

    End Function

The line "Return True" and "lstInschrijvingen.SelectedIndex = lstInschrijvingen.FindString(strNaam)" need to be switched because as soon as "Return True" is called, the sub will exit so it never gets to the next line. Here it is fixed:

VB.NET:
 Private Function fntDubbel(ByVal strNaam As String) As Boolean
        '****************************************
        '* Controleert winkelwagentje op dubbels.
        '****************************************
        For Each itm In lstInschrijvingen.Items
            If CStr(itm).Contains(strNaam) Then
                ' dubbel
                lstInschrijvingen.SelectedIndex = lstInschrijvingen.FindString(strNaam)
                Return True
            End If
        Next

        Return False

    End Function
 
Sure, glad it helped!
 

Latest posts

Back
Top