c# - How declare Textbox value (using Controls.Find) in public partial class without error? -
hi have code create new variable textbox not exist yet, can created on runtime. work great, see code bellow
public void btnapagar_click(object sender, eventargs e) { textbox txtacessorio4 = (textbox)gpbcategoria.controls.find("txtacessorio4", false).firstordefault(); if (txtacessorio4 != null && txtacessorio4.text == "" && lblacessorio4.name == "lblacessorio4") { messagebox.show("perfect"); } }
problem use created variable on places in code too, i tried:
public partial class cad_produto_acessorios_novo : form { textbox txtacessorio4 = (textbox)gpbcategoria.controls.find("txtacessorio4", false).firstordefault(); } public void btnapagar_click(object sender, eventargs e) { if (txtacessorio4 != null && txtacessorio4.text == "" && lblacessorio4.name == "lblacessorio4") { messagebox.show("perfect"); } }
but had bellow error on public partial class(gpbcategoria groupbox name):
error 1 field initializer cannot reference non-static field, method, or property 'infoearth_cad_cliente.cad_produto_acessorios_novo.gpbcategoria
somebody know how solve it?
many of recent questions go same direction.
if want access control (like label or textbox) have created @ runtime need kind of container (an array or list). way .net framework checks during runtime if control present in container.
void btnapagar_clickclick(object sender, eventargs e) { // test if textbox 4 exist counting number of added textboxes if(textboxlist.count ==4 || textboxlist.count > 4) { // list , array 0 based --> index 3 4th textbox messagebox.show("perfect have " + " @ least 4 boxes , name is: " + textboxlist[3].name); }else { messagebox.show("number of textboxes not enough - add more"); } }
instead of using (textbox)gpbcategoria.controls.find("txtacessorio4", false).firstordefault();
can access list index "future" instance textbox_4
you may take @ my example project , play around it. can openend sharpdevelop 4.3.
Comments
Post a Comment