|
-
Jan 29th, 2008, 03:44 PM
#1
Thread Starter
Lively Member
[Resolved][2005]TextBox with just numbers and points, other stuff
Hello everyone.
We're currently working on our thesis about a preprocessor for different engineering simulations. Our intention is to make it as user friendly as possible, since the whole "simulation" idea is often considered hostile by most students. Therefore, we're working on it in Visual Basic 2005, since it's a pretty standard tool, visually appealing and friendly, both for the user and the programmer. Besides, since it's a project for the university, it is our intention to make the code as easy to understand as possible, in case other students want to work on it in the future.
However, due to our inexperience with VB (hey, we're engineers, not programmers), we've come to find several walls in our way. After some search on Google, most of them are solved, with only a few exceptions.
For example, there are quite a number of TextBoxes that we want to restrict their use to only numbers and a single point (for decimals). Since we want to have a tidy code, we would want to use a separate module, just calling a function to handle each textbox.
Now, in VB6, I've found this works:
On Module:
Code:
Function JustNumbersAndPoint(Value, KP)
If (KP > 47 And KP < 58) Or KP = 13 Or KP = 8 Then
KeyAscii = KP
Else
If KP = 46 Then
KeyAscii = KP
For H = 1 To Len(Value)
Symbol = Mid(Value, H, 1)
If Symbol = Chr(46) Then
KeyAscii = 0
H = Len(Value)
End If
Next H
End If
End If
JustNumbersAndPoint = (KeyAscii)
End Function
On each Form:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = JustNumbersAndPoint(Text1.Text, KeyAscii)
End Sub
Now that's cool and all, but how do I do that in VB2005, using the same method? (A module function to handle all textboxes when called).
Around Google, I've found several direct solutions (without using the module). The best one (I think) is this one:
Code:
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = "." Then
If Me.TextBox1.Text.IndexOf(".") > -1 Then
e.Handled = True
End If
ElseIf Char.IsNumber(e.KeyChar) = False AndAlso Char.IsControl(e.KeyChar) = False Then
e.Handled = True
End If
End Sub
Now, how can I use this in a Module and just call it whenever I want, sort of like with this -> "KeyAscii = JustNumbersAndPoint(Text1.Text, KeyAscii)" ?
Also, I'd like the function to handle numbers, just one point and allow the use of Backspace, Delete and Tab.
Last edited by Vaitork; Feb 8th, 2008 at 04:44 PM.
Reason: [Resolved]
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
|