i have few textboxes created dynamically , if want find textboxes in panels whats best way find ?
i search online , said through findname might able find our control need give every of textbox name , in wpf , name must come letters not int if put int.tostring screw . if put letters , difficult me locate them letters , numbers okay since can start 00 , +1 way can't .
i have textboxes dynamically created inside dynamic created wrappanel , add dynamic created wrappanel inside dynamic created stackpanel add stackkpanel wrappanel have created in xaml side
if ask me why need many panels because thats way can make better because of way retrieve information db , display .
here how code looks ( cut short because long):
private void populatequestion(int activityid, int taskid) { ilist<model.questionhint> lstquestionhints = qh.getrecords(taskid, activityid); stackpanel sp = new stackpanel(); foreach (model.questionhint qhm in lstquestionhints) { wrappanel wp = new wrappanel(); //some code .... if (qhm.option1.trim().length > 0 && qhm.option2.trim().length > 0) { wp.children.add(space); wp.children.add(tbox); // } sp.children.add(wp);// adding wrap panel stackpanel } // end of each loop. wrappaneltest.children.add(sp); // adding stackpanel wrappanel ( in xaml) } wrappaneltest panel created in xaml side . if have button , how should find textbox controls panels ?
i tried :
private void button1_click(object sender, routedeventargs e) // check button { int c = 0; foreach (textbox txtbox in wrappaneltest.children) { c++; } messagebox.show(c); }
but shows error ( points textbox txtbox in foreach loop):

i don't problem naming controls both letters , numbers. this:
// place dynamically create textboxes, skipped part u add wrap panel etc. for( int numcontrols = 0; numcontrols < 30; numcontrols++) { textbox box = new texbox(); box.name = "textbox" + numcontrols.tostring(); } and u find using
for(int numboxes = 0;numboxes < 30; numboxes++) { textbox box = wrappaneltest.findnyname("textbox" + numboxes.tostring(); //operate on these } as in solution dick schuerman: first, helper class find children easier:
class childcontrols { private list<object> lstchildren; public list<object> getchildren(visual p_vparent, int p_nlevel) { if (p_vparent == null) { throw new argumentnullexception("element {0} null!", p_vparent.tostring()); } this.lstchildren = new list<object>(); this.getchildcontrols(p_vparent, p_nlevel); return this.lstchildren; } private void getchildcontrols(visual p_vparent, int p_nlevel) { int nchildcount = visualtreehelper.getchildrencount(p_vparent); (int = 0; <= nchildcount - 1; i++) { visual v = (visual)visualtreehelper.getchild(p_vparent, i); lstchildren.add((object)v); if (visualtreehelper.getchildrencount(v) > 0) { getchildcontrols(v, p_nlevel + 1); } } } } and use this:
childcontrols ccchildren = new childcontrols(); foreach (object o in ccchildren.getchildren(wrappaneltest, 5)) { if (o.gettype() == typeof(textbox)) { // } } the "5" in getchildren means how many levels deep want dig. example:
- wrappaneltest
- grids
- textboxes
that want set property 3.
Comments
Post a Comment