Results 1 to 4 of 4

Thread: [RESOLVED] Masked Text Box - Limit to Hex Values

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    2

    Resolved [RESOLVED] Masked Text Box - Limit to Hex Values

    I would like the user to enter a hex number in a text box in the format of

    0x#####

    For example, 0x12AD would be a valid entry. It would be nice to be able to mask all non valid hex values (non 0 to 9, A-F) with the MaskTextBox control. Can anyone help me set up the proper mask to do so (if its even possible)?

    Regards,

    Kevin

  2. #2
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: Masked Text Box - Limit to Hex Values

    I don't know if you want to handle the cases where the user leaves off the 0x at the start or anything, so I'll pretend you want that 0x everytime for now (makes the code easier)

    This code needs to be in textbox1_TextChanged

    VB Code:
    1. dim stras string = textbox1.text
    2. if not (isvalidhex(str))
    3.     ' Do error handling stuff

    And now the helper routine

    VB Code:
    1. private sub isvalidhex(byref str as string)
    2. if not str.substring(0,2) = "0x"
    3.     return false
    4. end if
    5. for i = 2 to str.length - 1
    6.     if not (isnumeric(str.substring(i-1,1) _
    7.     or tolower(str.substring(i-1,1)) = "a" _
    8.     or tolower(str.substring(i-1,1)) = "b" _
    9.     or tolower(str.substring(i-1,1)) = "c" _
    10.     or tolower(str.substring(i-1,1)) = "d" _
    11.     or tolower(str.substring(i-1,1)) = "e" _
    12.     or tolower(str.substring(i-1,1)) = "f")
    13.         return false
    14. next
    15. return true
    16. End Sub

    I just threw this together, so there may be some errors, but as long as you understand what's going on, you should be able to fix it. Any problems, post again and I'll double check it all.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Masked Text Box - Limit to Hex Values

    I don't think that there is a specific mask that will do it on its own because you can only specify types of characters that are valid, not specific characters. You'd have to provide your own masking language for the control through its MaskedTextProvider property. I know that that's possible but I don't know how it's done. The simple way is to use this mask: "\0x>AAAAA". This will place a literal "0x" in the field followed by five positions that will accept any alphanumeric input and convert it to upper case. You'd then use this code to exclude invalid letters:
    VB Code:
    1. Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MaskedTextBox1.KeyDown
    2.     If e.KeyCode > Keys.F AndAlso _
    3.        e.KeyCode <= Keys.Z AndAlso _
    4.        Not e.Control AndAlso _
    5.        Not e.Alt Then
    6.         'The user has pressed a letter key greater than F, which would be allowed by the mask, so reject it.
    7.         e.SuppressKeyPress = True
    8.  
    9.         If Me.MaskedTextBox1.BeepOnError Then
    10.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
    11.         End If
    12.     End If
    13. End Sub
    I tried using the KeyPress event but it seemed not to work for some reason.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    2

    Re: Masked Text Box - Limit to Hex Values

    Excellent. Thank you both for the responses. I was hoping I could just use the Mask propery to exlude specific characters, but looks like I was wrong. Your ways certainly work.

    Thanks again,

    Kevin

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