CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2006
    Location
    Cincinnati
    Posts
    24

    Entry exclusion logic

    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:

    Code:
    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!
    Last edited by FZ1; March 5th, 2006 at 10:08 PM.

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Entry exclusion logic

    u can use the ascii table, 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
    Code:
    If Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 90 Then
                e.Handled = True
            End If

  3. #3
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Entry exclusion logic

    u may also use the Key Code constants , like this to disable the space
    Code:
           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 Constants
    Last edited by aniskhan; March 5th, 2006 at 05:19 AM.

  4. #4
    Join Date
    Jan 2006
    Location
    Cincinnati
    Posts
    24

    Re: Entry exclusion logic

    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).

  5. #5
    Join Date
    Jan 2006
    Location
    Cincinnati
    Posts
    24

    Re: Entry exclusion logic

    I guess I'll have to manually exlude each special character. Seems wasteful.

  6. #6
    Join Date
    Jan 2006
    Posts
    86

    Re: Entry exclusion logic

    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.

  7. #7
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Entry exclusion logic

    u can also use the isLetter ,isDigit functions.
    Code:
    If Not Char.IsLetter(e.KeyChar) Then
                e.Handled = True
            End If
    Last edited by aniskhan; March 6th, 2006 at 09:13 PM.

  8. #8
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Entry exclusion logic

    simply can write like this to allow only letters
    Code:
          If (Char.IsLetter(e.KeyChar) = False) Then
                e.Handled = True
            End If
    Code:
            e.Handled = Not Char.IsLetter(e.KeyChar)

  9. #9
    Join Date
    Jan 2006
    Location
    Cincinnati
    Posts
    24

    Re: Entry exclusion logic

    [QUOTE=aniskhan]simply can write like this to allow only letters
    Code:
          If (Char.IsLetter(e.KeyChar) = False) Then
                e.Handled = True
            End If
    Hey, that worked! Very cool. This textbook sucks
    I drive way too fast to worry about cholesterol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured