c# - Windows Form. Limit User input to a certain range -
i creating windows form program class , trying limit input 'weight' textbox 1-1000. got user input parse double reason error message created not popup @ right time intended. (the error message popup if enter digits passed 5 digits... can enter 2222 or 10000 without error)
private void weight_keypress(object sender, keypresseventargs e) { var sourcevalue = weight.text; double doublevalue; if (double.tryparse(sourcevalue, out doublevalue)) { if (doublevalue > 1000 ) { messagebox.show("cannot greater 1000"); } } }
instead of using keypress should use textchanged event because if use keypress new char not part of control text yet.
private void inputtextbox_textchanged(object sender, eventargs e) { var inputtextbox = sender textbox; var sourcevalue = inputtextbox.text; double doublevalue; if (double.tryparse(sourcevalue, out doublevalue)) { if (doublevalue > 1000) { messagebox.show("cannot greater 1000"); } } }
Comments
Post a Comment