Canceling lose focus on Textbox
Is there anyway from the TextBox's LostFocus function to cancel it so the user is stuck in focus.
The reason I ask is that I want to validate the string in the textbox is a certain length. Currently when a user puts in a string longer than the allowed length a Messagebox displays and then the textbox is filled with a default value.
I'd like the user to be returned to the editing mode in the textbox with their original entered string so they can delete/edit it straight away.
Thanks
Re: Canceling lose focus on Textbox
VB Code:
Private Sub Text1_LostFocus()
Text1.SetFocus
End Sub
Re: Canceling lose focus on Textbox
Quote:
Originally Posted by microalps
VB Code:
Private Sub Text1_LostFocus()
Text1.SetFocus
End Sub
That wouldn't let you leave the textbox under any condition.
You can use the maxlength property of the textbox, and it will just beep and not let any more text in.
VB Code:
Private sub text1_Validate()
if len(text1.text) <> 6 then
text1.setfocus
endif
end sub
Re: Canceling lose focus on Textbox
If you use the Validate event (which is the right thing to do), make sure the other controls on the form all have their CausesValidation property set to True. Going to a control that doesn't have it set to True will bypass the Validate event.
Re: Canceling lose focus on Textbox
My variation....
VB Code:
Private Sub TextBox_Validate(Cancel As Boolean)
If Len(TextBox) <> 6 Then
Cancel = True
End If
End Sub
Re: Canceling lose focus on Textbox
I just noticed that you said...
Quote:
Originally Posted by gommo
...Currently when a user puts in a string longer than the allowed length a Messagebox displays and then the textbox is filled with a default value....
If less than the max length is OK then all you need to do is to set the MaxLength property of the textbox. You might want to do that anyhow so you don't need to worry about validating the high end.