Text box exacty length

dimeanel

Member
Joined
Apr 12, 2006
Messages
7
Programming Experience
Beginner
I have one text box which must be exactry 13 characters length. I define max length 13 characters in windows form properties but I do not know what should I do in a case when user input less that 13 characters. Do I need some code or what?
 
you could set an input mask, like ************* . that should do the trick... you could also use code. for example, on the textbox1_changed event, check the length and if it's too short popup a messagebox
 
nononono.... don't use the changed event for that.... you'll get a msgbox on every key stroke. Bad juju. Try the Validating (not the ValidatED, but the one with the ING) event.... if the textbox.text.lenght is less than what you need it to be, give the user a message and set the eventargs.cancel flag to true.

-tg
 
ManicCW said:
or you can use lostfocus event and in event:

if me.MyTextbox.Lenght < 13 then
----
end if
You should not be using the LostFocus event at all, except in very specific circumstances. You should be using the Leave event rather than the LostFocus event, but that is inappropriate here. The Validating event will prevent the control losing focus if it fails validation, which is what you'd want under these circumstances.
VB.NET:
If myTextBox.Text.Length <> 13 Then
    MessageBox.Show("Please enter 13 characters.")
    e.Cancel = True
End If
If you use the Validating event you also have the ability to prevent the message being displayed if the user presses a Cancel button by setting its CausesValidation property to False. That way if the tex is les than 13 characters and the user presses the Cancel button the field is not validated and the form can close. If you used the Leave event you'd not be able to Cancel without filling the field with a value that you didn't intend to keep anyway.
 
jmcilhinney said:
You should not be using the LostFocus event at all, except in very specific circumstances. You should be using the Leave event rather than the LostFocus event, but that is inappropriate here. The Validating event will prevent the control losing focus if it fails validation, which is what you'd want under these circumstances.
VB.NET:
If myTextBox.Text.Length <> 13 Then
    MessageBox.Show("Please enter 13 characters.")
    e.Cancel = True
End If
If you use the Validating event you also have the ability to prevent the message being displayed if the user presses a Cancel button by setting its CausesValidation property to False. That way if the tex is les than 13 characters and the user presses the Cancel button the field is not validated and the form can close. If you used the Leave event you'd not be able to Cancel without filling the field with a value that you didn't intend to keep anyway.

Thanks for your help,

Your idea with TextBox Lenght and Messbox is very good, but I also need help about exactly code for validation event. I tried to wright my self but I made some mistake.
 
here is some code that insures the text is the exact length by adding spaces or removing characters if too long. This may help, not sure.

VB.NET:
[COLOR=blue]Private Function [/COLOR][COLOR=black]SetText([/COLOR][COLOR=blue]ByVal [/COLOR][COLOR=black]value [/COLOR][COLOR=blue]as string[/COLOR][COLOR=black]) [/COLOR][COLOR=blue]as string[/COLOR]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] value [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 13[/SIZE]
[SIZE=2]value &= [/SIZE][SIZE=2][COLOR=#800000]" "
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] value.Length < 13 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = value.Length + 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 13
value &= [/SIZE][SIZE=2][COLOR=#800000]" "
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] value.Length > 13 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] NewString [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 13
NewString &= Mid(value, x, 1)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]value = NewString
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][COLOR=blue]Return[/COLOR]  value
 
[COLOR=blue]End Function[/COLOR]
[/SIZE]


maybe this can help you customize the situation in some way... just play with it if you need to. Or then again, this may the exact opposite of what your looking.
 
dimeanel said:
Thanks for your help,

Your idea with TextBox Lenght and Messbox is very good, but I also need help about exactly code for validation event. I tried to wright my self but I made some mistake.
I already provided it. The message notifies the user and setting e.Cancel to True will prevent the control losing focus, so the user will be unable to leave that control until they enter a valid value.
 
Back
Top