c# - User input validation using TextBox_Validating event -
i'm trying utilize custom method user input validation in text boxes. see missing here cannot move (focus) next text box in form!
private void textbox_validating(object sender, canceleventargs e) { textbox currenttb = (textbox)sender; if (currenttb.text == "") { messagebox.show(string.format("empty field {0 }", currenttb.name.substring(3))); e.cancel = false; } else { e.cancel = true; } } adding handler textboxes foreach loop in form constructor:
foreach(textbox tb in this.controls.oftype<textbox>().where(x => x.causesvalidation == true)) { tb.validating += textbox_validating; }
as answerd here expected behaviour of loosing focus c# validating input textbox on winforms :-
description
there many ways validate textbox. can on every keystroke, @ later time or validating event.
the validating event gets fired if textbox looses focus, illustration click on other control. if set e.cancel = true textbox don't loose focus.
msdn - control.validating event when alter focus using keyboard (tab, shift+tab, , on), calling select or selectnextcontrol methods, or setting containercontrol.activecontrol property current form, focus events occur in next order
enter
gotfocus
leave
validating
validated
lostfocus
when alter focus using mouse or calling focus method, focus events occur in next order:
enter
gotfocus
lostfocus
leave
validating
validated
c# .net winforms validation validating
No comments:
Post a Comment