C# event click containing more than one method -
i trying nest methods, or place methods within other methods. using c# , microsoft visual studio first time , dilemma this. have created form event click button validate user input, fine in itself, need validate more 1 input when button clicked. in calculate button’s click event handler, perform input validation on 3 text boxes (the 3 user use data entry). code efficiency want use separate methods achieve this. have tried writing more methods directly within event handler, no matter how start method public, private, static, void, main etc errors. assistance/advice appreciated.
private void btncalculate_click(object sender, eventargs e) { int txtlength = 0; if ((txtlength < 5) & (txtlength > 50)) messagebox.show("length measurement invalid" + "\r\n" + "please enter value between 5 , 50", "data invalid"); int txtwidth = 0; if ((txtwidth < 2) & (txtlength > 20)) messagebox.show("width measurement invalid" + "\r\n" + "please enter value between 2 , 20"); int txtavgdepth = 0; if ((txtavgdepth < 2) & (txtavgdepth > 4)) messagebox.show("width measurement invalid" + "\r\n" + "please enter value between 2 , 20");*/ }
changed code in method along error messages throws. original code had no syntax errors.
problem solved. thank :)
just add them class:
private void calculatebutton_click(object sender, eventargs e) { validatelength(); validatewidth(); validatedepth(); } private void validatelength() { int txtlength = lengthtextbox.value; if ((txtlength < 5) & (txtlength > 50)) messagebox.show("length measurement invalid" + "\r\n" + "please enter value between 5 , 50", "data invalid"); } private void validatewidth() { int txtwidth = widthtextbox.value; if ((txtwidth < 2) & (txtlength > 20)) messagebox.show("width measurement invalid" + "\r\n" + "please enter value between 2 , 20"); } private void validatedepth() { int txtavgdepth = depthtextbox.value; if ((txtavgdepth < 2) & (txtavgdepth > 4)) messagebox.show("depth measurement invalid" + "\r\n" + "please enter value between 2 , 4");*/ }
i guess you're trying tidy , have single validation routine:
private void calculatebutton_click(object sender, eventargs e) { validaterange(lengthtextbox.value, 5, 50, "length"); validaterange(widthtextbox.value, 2, 20, "width"); validaterange(depthtextbox.value, 2, 4, "depth"); } private void validaterange(int value, int min, int max, string msg) { if (value < min || value > max) messagebox.show( string.format("{0} measurement invalid\r\nplease enter value between {1} , {2}", msg, min, max), "data invalid"); }
Comments
Post a Comment