Click to See Complete Forum and Search --> : Entry exclusion logic


FZ1
March 4th, 2006, 07:59 PM
Let me preface this by saying I am working on a homework assignment. However, I am asking for guidance not answers. I'm stuck here. I am taking VB.NET I and we are currently working on If/Else statements etc. We did a problem in class where we wanted to limit the data entry for a textbox to numbers only. No problem. The homework assignment has a similar problem except the textbox is a state abbreviation so no numbers or special characters are allowed. OK, I can code for the no numbers but can't figure out how to prevent special characters from being entered. I have looked on the web but the 1 instance I have found code that makes reference to ASCII characters which we have not gone over in class (other than to speak about what ASCII is) and there is not even a table in the book so I'm sure this is not the route we are supposed to take. Some other code I found is also way more advanced than the stuff we are doing. A girl from my class and I worked on this for about an hour an couldn't figure it out. Any insight is appreciated - I'm sure it's something simple and I'm overlooking it. Here is the portion of code I have written dealing with this problem:

Private Sub Cancelkeys(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles uiStateTextBox.KeyPress
If (e.KeyChar >= "0" And e.KeyChar <= "9") And (e.KeyChar <> ControlChars.Back) Then
e.Handled = True
End If

Obviously the logic rules out 0-9 and I've tried to exclude anything outside of <a & >z but that seems to allow everything. I'm officially with Jack, Sawyer and Kate - LOST!

aniskhan
March 5th, 2006, 04:10 AM
u can use the ascii table (http://www.techonthenet.com/ascii/chart.php), where each each character has a ascii code
in ascii table the no from 65 to 90 is for Capital alphabets, to stop them from entry we do it like
If Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 90 Then
e.Handled = True
End If

aniskhan
March 5th, 2006, 04:14 AM
u may also use the Key Code constants , like this to disable the space
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Keys.Space Then
e.Handled = True
End If
End Sub
here are the Key code (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbgrfkeycodeconstantchangesinvisualbasicnet.asp) Constants

FZ1
March 5th, 2006, 09:18 AM
Thank you for your reply, however we are not using the ASCII tables in class yet so I don't believe it's the solution I should be using. Although I could, we are only graded on if the app functions as it's supposed to (homework anyway) not how it's coded. I have though about also excluding all of the characters individually (ie ":", ";" or the keys.* that you outlined, etc) but that seems cumbersome and inefficient and probably the worst way to code it (although it should work).

FZ1
March 6th, 2006, 07:24 AM
I guess I'll have to manually exlude each special character. Seems wasteful.

juanchoc
March 6th, 2006, 04:53 PM
Seems easyer to make a collection of strings, with each letter in upper case.
And then make a validation of the characters entered in the textbox.

aniskhan
March 6th, 2006, 07:39 PM
u can also use the isLetter ,isDigit functions.
If Not Char.IsLetter(e.KeyChar) Then
e.Handled = True
End If

aniskhan
March 6th, 2006, 08:27 PM
simply can write like this to allow only letters
If (Char.IsLetter(e.KeyChar) = False) Then
e.Handled = True
End If
e.Handled = Not Char.IsLetter(e.KeyChar)

FZ1
March 8th, 2006, 06:02 PM
[QUOTE=aniskhan]simply can write like this to allow only letters
If (Char.IsLetter(e.KeyChar) = False) Then
e.Handled = True
End If
Hey, that worked! Very cool. This textbook sucks :thumbd: