English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm trying to validate the following code so it only accepts the 26 letters of the alphabet, no numbers or other illegal characters. How would I do this? I've already checked MSDN and several arrays. HELP!

Dim word As String
word = InputBox("Enter a word:", "Guessing Game!")

2007-04-21 20:52:25 · 3 answers · asked by fiftypercentrick 2 in Computers & Internet Programming & Design

3 answers

Here is a routine which can handle multiple textboxes. It uses teh KeyPress event to screen text as it is entered into the text box. The textbox TAG property is used to define what type of validation is required. Form my example I have either a, "A" for alpha only or "U" for convert to upper case

You can refine this by adding additional functionality like N for numeric and I for numbers only ect.

The e.handled = true is used to cancel the users input keypress

Private Sub multi_txtBoxKeyValidation(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress, TextBox2.KeyPress

Dim ch As Char
Dim mode, str As String
Dim num As Integer
Dim txtBox As TextBox

txtBox = sender
mode = txtBox.Tag


num = Asc(e.KeyChar) 'capture ascii value of char

Select Case mode
Case "A" 'Alpha Only
num = Asc(e.KeyChar.ToString.ToUpper)
If (num >= 65) And (num <= 90) Then
'a valid letter has been entered

Else
'a non alpha char has been entered
Beep()
MessageBox.Show("Please enter only letters")
e.Handled = True

End If

Case "U" 'Make Upper case
If (num >= 97) And (num <= 122) Then
num = num - 32 'convert to Upper case
e.KeyChar = Chr(num)
End If

Case Else
'do nothing
End Select
End Sub

2007-04-22 04:12:42 · answer #1 · answered by MarkG 7 · 0 0

A ListBox might want to be populated with the dropdown record content textile. In VB.internet that's carried out like this: ListBox1.products.upload("Item1") Listbox1.products.upload("Item2") despite the indisputable fact that, a combobox can make extra experience. you are able to take an same mind-set there too. The VB.internet IDE also lets you populate the alternative of things utilising the houses communique.

2016-10-18 03:00:48 · answer #2 · answered by ? 4 · 0 0

Do a range check on the input.
Check each new character, and before you add it to the input textbox, if they are between 'a' and 'z', or 'A' and 'Z' then add them. Otherwise...do nothing.

if(cChar >= "a"c andalso cChar <= "z"c orelse
cChar >= "A"c andalso cChar <="Z"c) then
add to thing
end if

2007-04-21 21:02:43 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers