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.
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!")
You could do that, or create flow layout panel containing the buttons for A-Z and disable the button on click.
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?
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
MsgBox("You have already guessed this letter.")
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 false (None were found)