|
-
Nov 24th, 2000, 04:51 AM
#1
Thread Starter
New Member
Hi everybody!
I'm a VB-beginner and nee help.
How can I define an Input-Field, where the User can only write numbers and noch letters or other Signs.
Ps.: Sorry for my bad english.. I'm from Germany
-
Nov 24th, 2000, 04:57 AM
#2
Fanatic Member
Are you talking about an input box or a textbox on a form? If you're referring to a textbox, just put the property of the textbox values to numerical (you can do so in the properties windows, when you select the textbox).
When your car breaks down,
close all windows and retry 
=> please rate all users posts! <=
-
Nov 24th, 2000, 05:01 AM
#3
-
Nov 24th, 2000, 05:04 AM
#4
Thread Starter
New Member
A Textbox.. where can I do that!?!?!?!?
-
Nov 24th, 2000, 05:10 AM
#5
Fanatic Member
Well, when you build your form you can insert a textbox, by clicking on the left panel on textbox and then dragging it over your form. Is this what you are asking ?!?
VBKinght yep you're right, IsNumerical() does work fine, you should however then set the KeyPreview = True on the form itself and use the function to filter the keyboard input, so that it just doesn't print other text ;0
When your car breaks down,
close all windows and retry 
=> please rate all users posts! <=
-
Nov 24th, 2000, 05:23 AM
#6
PowerPoster
...
Hey! guy, if you're talking about the textbox control, I've another 2 method as show below. The first is a bit more complicated as compare to the second.
Code:
'Method 1
Option Explicit
Private Declare Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long)
Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Private Const ES_NUMBER = &H2000&
Private Const GWL_STYLE = (-16)
Private Sub Text1_Change()
Dim tmpValue&
Dim fAlignment&
Dim ret&
fAlignment& = ES_NUMBER
tmpValue& = GetWindowLong&(Text1.hwnd, GWL_STYLE)
ret& = SetWindowLong&(Text1.hwnd, GWL_STYLE, tmpValue& Or fAlignment&)
Text1.Refresh
End Sub
Code:
'Method 2
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 47 Or KeyAscii > 58) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
-
Nov 24th, 2000, 05:30 AM
#7
-
Nov 24th, 2000, 05:44 AM
#8
PowerPoster
KeyPreview = false
Hi! Wildcat, I juz execute the code without set the KeyPreview properties to true and yet it still return the expected result too. 
Originally posted by wildcat_2000
Hi Chris,
yes your 2nd option is the one I've suggested here below, still thanks for the code it's true - it does make things more clear to persons 
I still believe you have to set the From KeyPreview property to True for this to work properly.
Kind regards,
W.
-
Nov 24th, 2000, 05:56 AM
#9
-
Nov 24th, 2000, 07:16 AM
#10
New Member
dunno whether this might help
what about a mask input box? you can specify the location of your numbers and special characters using a mask input box.
-
Nov 24th, 2000, 07:24 AM
#11
_______
<?>
It's called a Masked Edit Control, referred to in Project/Components/Microsoft Masked Edit Control, and it it's own properties as MaskedBox1. You can set a mask, and the only thing to remember is that when you clear a maskeditbox you must first clear the mask then clear the text and then reset the mask. With that in mind it makes a very nice option.
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Nov 24th, 2000, 07:27 AM
#12
PowerPoster
MaskEdit
Will you think using MaskEdit control in this case is much more complecated?
-
Nov 24th, 2000, 01:22 PM
#13
_______
<?>
No..you just have to know the little quirk I mentioned above.
MaskEdBox1.Mask = "##########" 'however long your field
then all you can enter is numbers and backspace. No other code required.
To clear the text.
MaskEdBox1.Mask = ""
MaskEdBox1.Text = ""
MaskEdBox1.Mask = "##########" 'however long your field
'if you wanted numbers seperated by / then
MaskEdBox1.Mask = "###/###/####" 'as per phone number
'then the / would remain in the box for guidance
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Nov 24th, 2000, 04:07 PM
#14
Lively Member
Answer
You can add this code on the OnKeyPress event of any TextBox:
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack _
And Chr(KeyAscii) <> "." Then
KeyAscii = 0
End If
-
Nov 24th, 2000, 05:11 PM
#15
Hyperactive Member
Re: Answer
fnajar that would disable keys like enter and tab. use this rather:
Code:
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii > 31 Then
KeyAscii = 0
End If
If you want to enable spaces change 31 to 32
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
|