Results 1 to 16 of 16

Thread: [2005] Applying ASCII code into VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    13

    [2005] Applying ASCII code into VB

    How do i apply ASCII code into VB?.. eg. Identifiying a character by its own character or source code.

  2. #2
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Re: [2005] Applying ASCII code into VB

    asc() and chr() still work

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    13

    Re: [2005] Applying ASCII code into VB

    Quote Originally Posted by agmorgan
    asc() and chr() still work
    Okay. So how do i identify a single character?

  4. #4
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] Applying ASCII code into VB

    To get a single character use
    VB Code:
    1. [I]String[/I].Chars(i)
    where i is the index of the character you want.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    13

    Re: [2005] Applying ASCII code into VB

    Thanks. So how do i set a Range of index? like from character 40 to 68

  6. #6
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] Applying ASCII code into VB

    String.Substring(i, l)

    i is index, l is length

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    13

    Re: [2005] Applying ASCII code into VB

    hmm.. i think my question is not clear.
    I wanna ask how do i set a range of character for ASCII. like from A-Z..

  8. #8
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] Applying ASCII code into VB

    I don't under the question. What is it that you are trying to do?

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    13

    Re: [2005] Applying ASCII code into VB

    Quote Originally Posted by 18experience
    I don't under the question. What is it that you are trying to do?
    I'm sorry if i cant make myself clear. I am trying to find set a range for ASCII code. Like from Char A - Z is 65 - 90.. so how do i apply in VB coding to set the range.

  10. #10
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] Applying ASCII code into VB

    Set a range on what? A textbox? Are you trying to stop someone from entering numbers, or anything but uppercase characters?

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    13

    Re: [2005] Applying ASCII code into VB

    Quote Originally Posted by 18experience
    Set a range on what? A textbox? Are you trying to stop someone from entering numbers, or anything but uppercase characters?
    Erm. an input.. The task is simple. To check every input its character. So i decided to use ASCII codes to check every single inputs.

  12. #12
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Applying ASCII code into VB

    Think you want to check out the KeyDown and KeyPress events of the text box. Go read about it on MSDN

    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    2.     Handles TextBox1.KeyPress
    3.  
    4.         Select Case Asc(e.KeyChar)
    5.             Case Is = 97
    6.                 MessageBox.Show("Pressed a")
    7.  
    8.             Case Is = 65
    9.                 MessageBox.Show("Pressed A")
    10.  
    11.             Case Is >= 100
    12.                 MessageBox.Show("ASC value greater than or equal to 100 found")
    13.  
    14.             Case 10 To 20
    15.                 MessageBox.Show("Case ASC value ranging from 10 to 20 found")
    16.  
    17.             Case Else
    18.                 MessageBox.Show("Unknown number - not specified")
    19.  
    20.         End Select
    21.  
    22.     End Sub
    Last edited by stimbo; Jan 16th, 2007 at 08:43 PM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    13

    Re: [2005] Applying ASCII code into VB

    Quote Originally Posted by stimbo
    Think you want to check out the KeyDown and KeyPress events of the text box. Go read about it on MSDN

    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    2.     Handles TextBox1.KeyPress
    3.  
    4.         Select Case Asc(e.KeyChar)
    5.             Case Is = 97
    6.                 MessageBox.Show("Pressed a")
    7.  
    8.             Case Is = 65
    9.                 MessageBox.Show("Pressed A")
    10.  
    11.             Case Is >= 100
    12.                 MessageBox.Show("ASC value greater than or equal to 100 found")
    13.  
    14.             Case 10 To 20
    15.                 MessageBox.Show("Case ASC value ranging from 10 to 20 found")
    16.  
    17.             Case Else
    18.                 MessageBox.Show("Unknown number - not specified")
    19.  
    20.         End Select
    21.  
    22.     End Sub
    Erm.. i dun understand the source code but thanks for the effort. What i want to know is to check every single character in a string.

    eg. if user input Hello!
    the source code will take out H and check whats ASCII code does it belong. After checking H, and is a true value, it will proceed to e and check whats its ASCII code value and if its true, the loop will go on for the rest of the words. If all true, it will tell the user its correct.

    I think i am requesting too much.. but i really do hope you guys will help me. Thanks for the effort helping me..

  14. #14
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Applying ASCII code into VB

    My last post covers almost everything you want to actually do...

    You need to loop through the string taking each character at a time:

    VB Code:
    1. For Each chr As Char In Me.TextBox1.Text   'Or a string instead of textbox
    2.            
    3.        
    4. Next

    Then within that loop check for character you are looking for - use my other code to get ASC value for it and it's done.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  15. #15

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    13

    Re: [2005] Applying ASCII code into VB

    Quote Originally Posted by stimbo
    My last post covers almost everything you want to actually do...

    You need to loop through the string taking each character at a time:

    VB Code:
    1. For Each chr As Char In Me.TextBox1.Text   'Or a string instead of textbox
    2.            
    3.        
    4. Next

    Then within that loop check for character you are looking for - use my other code to get ASC value for it and it's done.

    hmm.. kinda understand, but theres a sentence which i dun understant.

    Select Case Asc(e.KeyChar)

    what does e.keychar means?

  16. #16
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Applying ASCII code into VB

    See the bit on the KeyPress Event Sub that says:

    VB Code:
    1. ByVal e As System.Windows.Forms.KeyPressEventArgs

    The e (which is arbitrary I believe) is the value of the argument passed by the event (Or something like that).

    So you examine 'e.KeyChar' which is the Select Case bit, you are looking at the KeyChar property of the event that happened and convert it from it's initial character value into it's ASCII value using the Asc(chr here).

    I hope that makes sense. If you didn't have Asc in front and instead only had e.KeyChar then it would give you the actual letter you pressed. The .KeyChar tells it what information from 'e' it wants. Remove the KeyChar bit and you will see the intellisense gives you lots of other properties that you could get.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

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