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 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.ManicCW said:or you can use lostfocus event and in event:
if me.MyTextbox.Lenght < 13 then
----
end if
If myTextBox.Text.Length <> 13 Then
MessageBox.Show("Please enter 13 characters.")
e.Cancel = True
End If
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.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.VB.NET:If myTextBox.Text.Length <> 13 Then MessageBox.Show("Please enter 13 characters.") e.Cancel = True End If
[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]
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.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.