Results 1 to 8 of 8

Thread: [RESOLVED] [ask] textbox only accept hex

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2008
    Posts
    146

    Resolved [RESOLVED] [ask] textbox only accept hex

    i want to make a textbox that only can support the hex number.

    how to make the validation???

    thx a lot

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [ask] textbox only accept hex

    Here is one way of doing it:
    Code:
      Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not "1234567890ABCDEF".Contains(Char.ToUpper(e.KeyChar)) AndAlso e.KeyChar <> vbBack Then
          e.Handled = True
        End If
      End Sub
    The above will not allow the user to type in anything but hexadecimal characters, however it's not bullet proof since you could still paste something that contains something else so you should always validate the string. Passing it to the following function could do that:
    Code:
      Public Function IsHex(ByVal str As String) As Boolean
        Try
          Dim num As Long = CLng("&H" & str)
          Return True
        Catch ex As Exception
          Return False
        End Try
      End Function

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [ask] textbox only accept hex

    There is the following method which you might want to research in a bit more depth which might be of help to you:
    Code:
    Integer.Parse(stringToEvaluate, Globalization.NumberStyles.HexNumber)

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: [ask] textbox only accept hex

    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2. e.Handled = Not System.Uri.IsHexDigit(e.KeyChar)
    3. End Sub

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2008
    Posts
    146

    Re: [ask] textbox only accept hex

    thx

    it works

  6. #6
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: [ask] textbox only accept hex

    yw

    thread resolve

  7. #7
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [RESOLVED] [ask] textbox only accept hex

    Aren't you better off handling the text-changed event to cover copy-pasting?

  8. #8
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: [RESOLVED] [ask] textbox only accept hex

    Hmm, good point. Well then, there's only one sane, logical thing to do...

    ...And thats to erase the functionality of TextBoxBase's paste!
    VB Code:
    1. Dim PasteMethod As Reflection.MethodInfo = _
    2.     Array.Find(Of Reflection.MethodInfo)(DirectCast(TextBox1, TextBoxBase).GetType.GetMethods, _
    3.     New Predicate(Of Reflection.MethodInfo)(Function(meth As Reflection.MethodInfo) meth.Name = "Paste"))
    4. Dim newPaste() As Byte = {0, 0, 0, 0, 0, 0, 0, 42}
    5. Dim hndlePaste As System.Runtime.InteropServices.GCHandle = _
    6.     System.Runtime.InteropServices.GCHandle.Alloc(CType(newPaste, Object), System.Runtime.InteropServices.GCHandleType.Pinned)
    7. Dim addPaste As IntPtr = hndlePaste.AddrOfPinnedObject()
    8. System.Reflection.Emit.MethodRental.SwapMethodBody(GetType(TextBoxBase), PasteMethod.MetadataToken, addPaste, newPaste.Length, _
    9.     System.Reflection.Emit.MethodRental.JitImmediate)
    Muahahaha! :evil:

    Unfortunately, Microsoft frowns on this and won't let me modify anything that has already been loaded by the JIT.

    So since we can't do that - yeah, Text_Change event is probably best.
    </joke>
    Last edited by syntaxeater; Jul 29th, 2008 at 02:04 AM.

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