1 Attachment(s)
[Help me please!] Textboxes and math
well, I made this thingy, but I'm stuck on making text boxes only accept numbers. I've tried
Code:
Private Sub Text1_TextChanged(By Val KeyAscii as integer)
blah blah blah
End Sub
Apparently, KeyAscii does not pick up what ever is happening when I type.
Also, I want my text boxes to be numbers since they are only to be numbers, but they are picked up as strings. help please!!
Thanks.
P.S. If you can, please input the stuffs for me for only one text box. (text1)
Re: [Help me please!] Textboxes and math
I am a noobie myself, but for the second part of your questions here are a couple functions you could try:
Code:
Convert.ToInt32(text1)
CInt(text1)
The CInt() function will round decimals, but Convert.ToInt32() will throw an exception if there is a decimal.
Hope this helps you :-)
Russ
Re: [Help me please!] Textboxes and math
Not really for the Maths forum but...
You have two things wrong in your sub declaration, first of all you should use the KeyPress event, and it should be 'ByVal' instead of 'By Val'
(Might have been a typo, don't know)
Your KeyAscii should be working now, and you can check if the KeyAscii is numerical, or the delete, backspace keys or whatever keys you need.
Re: [Help me please!] Textboxes and math
That's a very strange sub. It looks like an event handler, but it isn't a typical one by any stretch, at least not in .NET. Since I'm unclear on the language, I'm not going to suggest a solution, as I don't even remember what a VB6 event handler looks like, so it could be one of those. However, you should really ask a moderator to move this thread to the proper forum, as you will get better responses than this one.
Re: [Help me please!] Textboxes and math
Looks VB6 to me. I think he means:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
...
End Sub
Re: [Help me please!] Textboxes and math
ok. appearently, the
Code:
Private Sub Text1_KeyPress(KeyAscii as Interger)
---
End sub
that does NOT work, for
vb 9.0
and neither does the
Code:
Convert.ToInt32(text1)
CInt(text1)
. thanks anyway! where's the help section o___o?
PS: reminder, I'm using vb 9.0 express 2008 Microsoft.
Re: [Help me please!] Textboxes and math
This will work. It allows numbers and decimal points.
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) Or KeyAscii <> 46 Then
KeyAscii = 0
End If
End Sub
Re: [Help me please!] Textboxes and math
That code should allow the user to enter multiple decimal points as well, which won't give correctly-formed results. That case is easy to deal with, though, given the example.
Re: [Help me please!] Textboxes and math
Since this is .NET 2008, search for that topic in the .NET forum. The question gets answered about once a week or so, and there are a variety of solutions. Pick the one you prefer.
One option that isn't very good, but is similar to an earlier suggestion here would be to use Double.TryParse (or Integer.TryParse if decimals are not allowed). That would return True if the string was a double, and False if it was not. However, it wouldn't restrict the textbox entries to just numeric characters. It seems that there were a series of answers based on regular expressions, but I haven't dealt with this problem, so I haven't been looking for a solution.
Re: [Help me please!] Textboxes and math
hmm. the keyascii one does NOT work. do you think it would be better if I used vb 6.0??
Re: [Help me please!] Textboxes and math
It won't be better in VB6, because you'll be trapped into a language that is four versions out of date. You should be asking this in the .NET forum, not here, but here's a link to a thread that may help. The last snippet in the first post should cover most situations for you:
http://www.vbforums.com/showthread.p...number+textbox
Re: [Help me please!] Textboxes and math
I thought you were talking about VB6 since the syntax looks nothing like VB.NET.
In VB.NET it's done a tiny bit different.