c# - How to get Text from Textbox created during runtime -
i have created winform app in user can set how many textboxes want (range 1-99) using code create textboxes during runtime
(int = 0; < calculation.num; i++) { textbox txtrun = new textbox(); txtrun.name = "txtbox" + i; txtrun.location = new system.drawing.point(35, 50 + (20 * i) * 2); txtrun.size = new system.drawing.size(75, 25); this.controls.add(txtrun); } suppose user create 2 textboxes , enter data in each textbox , click calculate button want get textboxes data , divide 100
see picture want txtbox1 , txtbox2 data

edit 3:
this whole code
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace gpa_calculatior__new_ { public partial class form1 : form { int j = 0; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { //label + marks obtained textbox (int = 0; < calculation.num; i++) { label lblcount = new label(); lblcount.name = "lblcount" + i; lblcount.location = new system.drawing.point(5, 55 + (20 * i) * 2); lblcount.size = new system.drawing.size(20, 30); lblcount.text = (i + 1).tostring(); this.controls.add(lblcount); textbox txtrun = new textbox(); txtrun.name = "txtbox" + i; txtrun.location = new system.drawing.point(35, 50 + (20 * i) * 2); txtrun.size = new system.drawing.size(75, 25); this.controls.add(txtrun); } //creating textbox total marks (j = 0; j < calculation.num; j++) { textbox txtrun = new textbox(); txtrun.name = "totaltxtbox" + j; txtrun.location = new system.drawing.point(160, 50 + (20 * j) * 2); txtrun.size = new system.drawing.size(50, 25); txtrun.text = "100"; txtrun.enabled = false; this.controls.add(txtrun); } // creating 2 buttons (calculate , back) (int k = 0; k < 2; k++) { button btn = new button(); btn.name = "btn" + k; btn.location = new system.drawing.point(20 + (k *110), 60 + (20 * j) * 2); btn.size = new system.drawing.size(90, 30); if (k == 0) btn.text = "back"; else btn.text = "calculate"; btn.click += button_click; this.controls.add(btn); } //just giving free space in last label lbl = new label(); lbl.name = "lbl" + j; lbl.location = new system.drawing.point(30, 90 + (20 * j) * 2); lbl.size = new system.drawing.size(90, 30); lbl.text = ""; this.controls.add(lbl); //********************************************** } //caculate , button function private void button_click(object sender, eventargs e) { button btn = sender button; if (btn.name.equals("btn1")) { (int = 0; < calculation.num; i++) { } } else { gpa_calculator mainform = new gpa_calculator(); mainform.show(); this.hide(); } } private void checkbox1_checkedchanged(object sender, eventargs e) { (j = 0; j < 10; j++) { } } private void form1_formclosed(object sender, formclosedeventargs e) { application.exit(); } } }
var sum = this.controls.oftype<textbox>() .where(t => char.isdigit(t.name.reverse().take(1).firstordefault()) && t.enabled) .select(t => { double i; if (!double.tryparse(t.text, out i)) { return 0d; } return / 100d; }) .sum();
Comments
Post a Comment