Member Avatar for leyla

I was just wondering if there was any way to validate a listbox into only allowing a letter to be entered once, no matter the case.

I am making a Hangman game and I only want the user to be able to enter a letter once, but I want the listbox to recognise the letters in both upper and lower case and disallow them once theyve already been entered.

The letters are entered through a textbox, if that helps.

Thanks in advance :)

You could try to add it to an array then search the array for it:

Dim lstString As New List(Of String)


'Assuming they have to press a button to submit their letter
Private Sub Button_Click(sender as object, e as eventargs)Handles Button1.Click

   If lstString.BinarySearch(ListBox1.Text.ToLower) <> -1 Then
       MsgBox("You can only enter a letter once!")
   Else
       lstString.Add(ListBox1.Text.ToLower)
   End If
End Sub

You could do that, or create flow layout panel containing the buttons for A-Z and disable the button on click.

Member Avatar for leyla

Thank you, it works! sort of :)
The message box comes up every time, though. Even if the letter is in the word and it hasnt been entered before. Is there any way to fix this?

My question would then be, how are they entering/chosing from a listbox.

Are you allowing only 1 char, or an entire string?

And why a listbox and not a textbox?

Member Avatar for leyla

They are just typing in a letter and then pressing guess.
I am allowing only one letter at a time.
And the listbox is used to store the used letters.

Ah, you are storing the values IN the listbox. So the code I posted will be checked every button click, therefore, it will throw a message box(testing values that are already in the listbox)

Is it possible to use a textbox to type, and a listbox to store?

GuessBox

If this is possible, you can just check to see if the item is in the listbox:

 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    If IsDuplicate(TextBox1.Text) = False Then
        ListBox1.Items.Add(TextBox1.Text)
    Else
        MsgBox("You have already guessed this letter.")
    End If
End Sub

Private Function IsDuplicate(ByVal StringIn As String) As Boolean
    'Cycle and Check
    For i = 0 To ListBox1.Items.Count - 1
        If ListBox1.Items(i).ToString.ToLower = StringIn.ToLower Then
            Return True
        End If
    Next

    'Return false (None were found)
    Return False
End Function
Member Avatar for leyla

Yes, that is what I wanted to do, but I was unsure how.

The code from the post above will test the value in the textbox before entering it into the listbox.

This should accomplish what you are wanting, as for the guessing part, you will have to drop your code in.

I hope this helps. :)

Why not have the remaining letters in a dropdown combobox. Once a letter has been guessed you remove it from the combobox. That way there is no need to validate input.

commented: True +6
Member Avatar for leyla

Thank you so much Begginnerdev :D
I have not used a drop down combo box as I have all of the code for the listbox instead, but thank you for your suggestion.

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.