Thursday, 15 July 2010

c# - How to get values of array textboxes generated during from onload event -



c# - How to get values of array textboxes generated during from onload event -

i having problem retrieving values of array textboxes generated runtime in onload event. here code.

from form onload event:

private void frmmain_load(object sender, eventargs e) { textbox[] txtfldnames = new textbox[15]; int x = 155, y = 65, w = 300, h = 20; (int = 0; < 15; i++) { y = y + 30; txtfldnames[i] = new textbox(); txtfldnames[i].location = new system.drawing.point(x, y); txtfldnames[i].size = new system.drawing.size(w, h); this.controls.add(txtfldnames[i]); txtfldnames[i].readonly = true; txtfldnames[i].backcolor = color.white; } }

now how access textbox values button?

private void button1_click(object sender, eventargs e) { //what here? }

i tried 1 below not work. null values. please kindly point me right direction

private void button1_click(object sender, eventargs e) { textbox[] txtfldnames = new textbox[15]; (int j = 0; j < 15; j++) { txtfldnames[j] = new textbox(); messagebox.show("" + txtfldnames[j].text); } }

here total code:

public partial class classmain : form { public frmmain() { initializecomponent(); } private void frmmain_load(object sender, eventargs e) { textbox[] txtfldnames = new textbox[15]; int x = 155, y = 65, w = 300, h = 20; (int = 0; < 15; i++) { y = y + 30; txtfldnames[i] = new textbox(); txtfldnames[i].location = new system.drawing.point(x, y); txtfldnames[i].size = new system.drawing.size(w, h); this.controls.add(txtfldnames[i]); txtfldnames[i].readonly = true; txtfldnames[i].backcolor = color.white; } } private void button1_click(object sender, eventargs e) { //what here? } }

given total code can along way:

public partial class classmain : form { // move list global scope in classmain form. textbox[] txtfldnames = new textbox[15]; public frmmain() { initializecomponent(); } private void frmmain_load(object sender, eventargs e) { int x = 155, y = 65, w = 300, h = 20; (int = 0; < 15; i++) { y = y + 30; txtfldnames[i] = new textbox(); txtfldnames[i].location = new system.drawing.point(x, y); txtfldnames[i].size = new system.drawing.size(w, h); this.controls.add(txtfldnames[i]); txtfldnames[i].readonly = true; txtfldnames[i].backcolor = color.white; } } private void button1_click(object sender, eventargs e) { //what here? // can access global array variable. (int = 0; < 15; i++) { messagebox.show(txtfldnames[i].text); } } }

and if want clean code bit:

public partial class classmain : form { // move list global scope in classmain form. textbox[] txtfldnames = new textbox[15]; public frmmain() { initializecomponent(); } private void frmmain_load(object sender, eventargs e) { int x = 155, y = 65, w = 300, h = 20; (int = 0; < 15; i++) { y = y + 30; var t = new textbox { location = new system.drawing.point(x, y), size = new system.drawing.size(w, h), readonly = true, backcolor = color.white }; txtfldnames[i] = t; this.controls.add(t); } } private void button1_click(object sender, eventargs e) { //what here? // can access global array variable. (int = 0; < txtfldnames.length; i++) { messagebox.show(txtfldnames[i].text); } } }

c# arrays winforms textbox onload-event

No comments:

Post a Comment