mercredi 28 septembre 2016

How to get second match from string on Visual Basic?

I'm doing Hangman Game in Visual Basic. I'm looking for typing a letter in a TextBox and clicking a button to check out. If that letter is in String, it will return position but when the word has two matches... How could I do it?

Next code only return the first match, I mean, only position of the first "A".

Dim palabra As String = "PALABRA"

Private Sub BtnComprobar_Click(sender As Object, e As EventArgs) Handles BtnComprobar.Click
    If txtComprobar IsNot "" Then
        Dim letra As String = UCase(txtComprobar.Text)

        If palabra.IndexOf(letra) > -1 Then
            Select Case palabra.IndexOf(letra)
                Case 0
                    Lbl1.Text = letra
                    LblP.ForeColor = Color.Red
                Case 1
                    Lbl2.Text = letra
                    LblA.ForeColor = Color.Red
                Case 2
                    Lbl3.Text = letra
                    LblL.ForeColor = Color.Red
                Case 4
                    Lbl4.Text = letra
                Case 5
                    Lbl5.Text = letra
                    LblB.ForeColor = Color.Red
            End Select
        Else
            errores += 1
            txtErrores.Text = CStr(errores)
        End If
        txtComprobar.Text = ""
    End If
End Sub

Thank for your help

Aucun commentaire:

Enregistrer un commentaire