|
-
Jul 28th, 2008, 05:06 AM
#1
Thread Starter
Addicted Member
[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
-
Jul 28th, 2008, 08:01 AM
#2
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
-
Jul 28th, 2008, 08:02 AM
#3
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)
-
Jul 28th, 2008, 03:46 PM
#4
Re: [ask] textbox only accept hex
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = Not System.Uri.IsHexDigit(e.KeyChar)
End Sub
-
Jul 28th, 2008, 10:30 PM
#5
Thread Starter
Addicted Member
Re: [ask] textbox only accept hex
-
Jul 28th, 2008, 10:37 PM
#6
Re: [ask] textbox only accept hex
-
Jul 28th, 2008, 10:50 PM
#7
Re: [RESOLVED] [ask] textbox only accept hex
Aren't you better off handling the text-changed event to cover copy-pasting?
-
Jul 29th, 2008, 02:00 AM
#8
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:
Dim PasteMethod As Reflection.MethodInfo = _
Array.Find(Of Reflection.MethodInfo)(DirectCast(TextBox1, TextBoxBase).GetType.GetMethods, _
New Predicate(Of Reflection.MethodInfo)(Function(meth As Reflection.MethodInfo) meth.Name = "Paste"))
Dim newPaste() As Byte = {0, 0, 0, 0, 0, 0, 0, 42}
Dim hndlePaste As System.Runtime.InteropServices.GCHandle = _
System.Runtime.InteropServices.GCHandle.Alloc(CType(newPaste, Object), System.Runtime.InteropServices.GCHandleType.Pinned)
Dim addPaste As IntPtr = hndlePaste.AddrOfPinnedObject()
System.Reflection.Emit.MethodRental.SwapMethodBody(GetType(TextBoxBase), PasteMethod.MetadataToken, addPaste, newPaste.Length, _
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|