-
May 19th, 2012, 08:10 PM
#1
limit amout of user input
how i want to set text box with user only can insert value from 0 until 50 and if user insert more than 50 message box will appear using vb6
-
May 20th, 2012, 01:05 AM
#2
Re: limit amout of user input
Look up Masked Edit Control. It can accept valid ranges.
-
May 21st, 2012, 07:40 AM
#3
Re: limit amout of user input
Or you can put in some code in either the Change() event or the Validate() event.
The change event fires for each character the user enters, the validate event will fire before the TextBox looses focus. If using the validate you would go
Code:
Private Sub Text1_Validate(Cancel As Boolean)
If Val(Text1.Text) > MaxVal Then
MsgBox "Value exceeds maximum!"
Cancel = True
End If
End Sub
Setting Cancel to true will make the focus stay in Text1 to allow for editing the value.
The Validate() fires when the user attempts to go to another TextBox or Button, to continue his input. That means the user cannot leave the TextBox until the value is approved.
You could put a similar code (without the Cancel, though) int the Change() event, but this has some disadvantages.
Code:
Private Sub Text1_Change()
If Val(Text1.Text) > MaxVal Then
MsgBox "Value exceeds maximum!"
End If
End Sub
This fires the messagebox as soon as the entered value exceeds MaxVal, but after clicking away the messagebox the user can gladly continue going on to another input field, leaving the wrong value where it is.
-
May 21st, 2012, 10:04 AM
#4
Re: limit amout of user input
You can also set the maxlength property =2 to stop the box from allowing more than 2 characters.
Always use [code][/code] tags when posting code.
-
May 30th, 2012, 12:11 PM
#5
Re: limit amout of user input
Put the test in the Text Lost Focus event rather than the Text Change event to avoid the test taking place each time you key a character
-
June 1st, 2012, 06:04 AM
#6
Re: limit amout of user input
For that purpose VB objects provide the Validate() event (as I stated before), which happens BEFORE the LostFocus().
The advantage is, if your validate routine finds the user input unsatisfying, it can set the Cancel parameter and AVOID the control to loose focus.
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
|