-
I have two text boxes, txtDecimal and txtHexadecimal. When you type into the hex box, it converts and puts the converted value in the decimal box. But, when you type stuff into the hex box, it types backwards??
Like;
I put in 12345 and it comes out 12345
put in 03ED and it comes out DE3 ??
It seems to start going backwards as soon as i use a letter
BEE comes out as EEB etc
This is driving me nuts : ) I don't know what I'm doing wrong - the code for hex - dec conversion that im using is
txtDecimal.Text = Val("&h" & txtHexadecimal)
the conversion works, but the stupid text box wont behave!!
-
What an odd text box. Oh well, try setting a label's caption or something and see if it does the same thing.
-
Lol the point of the text box is so that people can enter in the hexadecimal value that they want to convert to decimal, i need a text box, the user cant enter info into a label at runtime
you know? when the program is running and you go to enter stuff in the text box it types backwards when your entering stuff.
-
You didn't completely elaborate on your example, but I figured out what you meant. You are trying to update the Hex Text Box with the Dec Text Box's Change Event and Vise-Versa.
Like this:
Code:
Private Sub txtHexadecimal_Change()
txtDecimal.Text = Val("&h" & txtHexadecimal)
End Sub
Private Sub txtDecimal_Change()
txtHexadecimal.Text = Hex(Val(txtDecimal.Text))
End Sub
I don't completely understand what the difference is between typing a letter in the hex box, or a number, but I can see the effect with this example.
You can fix this by moving the cursors insertion point after each Hex Text assignment like so:
Code:
Private Sub txtHexadecimal_Change()
txtDecimal.Text = Val("&h" & txtHexadecimal)
End Sub
Private Sub txtDecimal_Change()
txtHexadecimal.Text = Hex(Val(txtDecimal.Text))
txtHexadecimal.SelStart = Len(txtHexadecimal.Text)
End Sub
This should fix your problem, just don't put too big of a value in the Hex Box, or you will Over-Flow the calculation and generate an error.
-
That's great! Thanks very much... but.. : ) is there any other ways to fix this? I don't want a possible overflow error...
Is this happening because i have the conversion code in the textbox's change event?
-
Both the Hex(xxxxxxxx) command and the &Hxxxxxxxx function can only handle so many characters.
The Hex() is based on a Long Integer (32bit) (-2,147,483,648 to 2,147,483,647)
The &Hxxxxxx will only accept up to 8 hex digits, or &HFFFFFFFF
If you really wanted to get passed this barrier, you would have to create a set of functions to perform the Hex to Dec and Dec to Hex conversion manually.
If you want to do this, I could write you some code.... let me know.
In the meantime, you could prevent the values from being calculated if they are too big:
Code:
Private Sub txtHexadecimal_Change()
If Len(txtHexadecimal.Text) > 8 Then Exit Sub
txtDecimal.Text = Val("&h" & txtHexadecimal)
End Sub
Private Sub txtDecimal_Change()
If Val(txtDecimal.Text) > (2 ^ 31) Or Val(txtDecimal.Text) < (-1 * (2 ^ 31)) Then Exit Sub
txtHexadecimal.Text = Hex(Val(txtDecimal.Text))
txtHexadecimal.SelStart = Len(txtHexadecimal.Text)
End Sub
Or you can just ignor the errors:
Code:
Private Sub txtHexadecimal_Change()
On Error Resume Next
txtDecimal.Text = Val("&h" & txtHexadecimal)
End Sub
Private Sub txtDecimal_Change()
On Error Resume Next
txtHexadecimal.Text = Hex(Val(txtDecimal.Text))
txtHexadecimal.SelStart = Len(txtHexadecimal.Text)
End Sub