c# - C#I have made is a coI have made is a co -


the calculator have made combination of textboxes , radiobuttons , when 'calculate' clicked, message box check whole program , (if necessary) display message box saying 'the following areas still need completing' , list areas.

i know how code basic if li have made co't know how check multiple textboxes , radiobuttons in 1 go.

the current code button is:

private void button4_click(object sender, eventargs e) {     drawforce = (area2 * (strengthcoeff / (workhardexp + 1)) * (math.pow(math.log(area1 / area2), workhardexp + 1)));     drawforce = math.round(drawforce, 2);     textbox7.text = drawforce.tostring() + " n"; } 

i don't wish copy enter code onto here hope suffice

thanks

if understand correctly, you're looking way check many controls on form in loop. there couple of ways this.

one way create class-level collection on form holds references controls. example, this:

private ienumerable<textbox> textboxes = new list<textbox> {     textbox1,     textbox2,     textbox3 }; 

then can examine in loop:

var errors = new list<string>(); foreach (var textbox in textboxes)     if (string.isnullorwhitespace(textbox.text))         errors.add(string.format("{0} empty.", textbox.name)); // errors contains list of validation messages 

the downside you'd need manually keep collection updated if change form. if there aren't going many changes, shouldn't problem.

another option loop through controls collection of form. this:

var errors = new list<string>(); foreach (var control in controls)     if (control textbox)         if (string.isnullorwhitespace((control textbox).text))             errors.add(string.format("{0} empty.", (control textbox).name)); // errors contains list of validation messages 

this more dynamic, little more brittle well. if there text boxes nested in other controls won't find those, you'd need make more recursive loop through control.controls well. little ugly, , might not worth if form isn't change. either way, might this:

private ilist<string> geterrorsforcontrols(controlcollection controls) {     var errors = new list<string>();     foreach (var control in controls)     {         if (control textbox)             if (string.isnullorwhitespace((control textbox).text))                 errors.add(string.format("{0} empty.", (control textbox).name));          errors = errors.concat(geterrorsforcontrols(control.controls));     }     return errors; } 

then in code you'd invoke method:

var errors = geterrorsforcontrols(controls); 

to add validation other types of controls besides text boxes, add other class-level collections or add other type checks in loops.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -