c# - Automatically delete TextBox and label if TextBox.Text = Empty on focus leave -
i have code windows form starts 1 label
, 1 textbox
, when user starts type on textbox1
,it creates new textbox , label down (also change location of 2 buttons
, change windows form size,it happens max of 10 text box + 10 labels (side-side) like:
(label1) enter name 1: - textbox1 imput (label2) enter name 2: - textbox2 imput (label1) enter name 3: - textbox3 imput ...
it works great, have little "problem":
- my code creates new
textbox
/label
when user starts type on lasttextbox
- if user stops on
textbox7
example, code have createdtextbox8
although not needed , not contain text (is blank), - therefore
delete
automatically if user tabstextbox7
textbox8
, leavestextbox8
(without entering text tb8)
my code isn't working (will explain below) , if click on button
verify if last textbox
text empty
, if is, delete
text box , label @ side , changes localization buttons
, windows form size).
i have many problems because textbox
2 10 created @ runtime , not possible reference these "future" textbox
in code because error saying not exist in actual code.
problem of textbox
, label delete
on textbox
focus leave when textbox.text
empty
work great if focus in place in focus using mouse click if press tab delete
2 textbox
, crash , return error: index 12(can number) out of range.
see code create new textbox
, labels + resize form , windows form size on txtnomecategoria_textchanged:
public partial class cad_produto_acessorios_novo : form { string testelogico; int c; int n = 1; int n2 = 25; int n3 = 65; int n4 = 57; int n5 = 152; public cad_produto_acessorios_novo() { initializecomponent(); } private void txtnomecategoria_textchanged(object sender, eventargs e) { if (txtnomecategoria.textlength > 1) { n++; if (n <= 1) { n = 2; } if (n >= 1 && n <= 2) { n2 = n2 + 30; n3 = n3 + 30; n4 = n4 + 30; n5 = n5 + 30; gpbcategoria.size = new system.drawing.size(283, n4); this.height = n5; btnapagar.location = new point(108, n3); btnsalvar.location = new point(212, n3); textbox txt = new textbox(); txt.name = "txtacessorio" + n; txt.text = ""; txt.size = new system.drawing.size(189, 26); txt.location = new point(87, n2); testelogico = txt.name; gpbcategoria.controls.add(txt); txt.textchanged += new eventhandler(new_onchange); txt.leave += new eventhandler(erase_onleave); label lbl = new label(); lbl.name = "lblacessorio" + n; lbl.text = "acessório nº" + n + ":"; lbl.location = new point(4, n2 + 5); gpbcategoria.controls.add(lbl); } else { n--; } } }
note create 2 new event new runtime created textbox
:
txt.textchanged += new eventhandler(new_onchange); txt.leave += new eventhandler(erase_onleave);
so here go (create new textbox/label
+ resize windowsform,etc):
void new_onchange(object sender, eventargs e) { cadeianovoscampos(sender textbox, e); } private void cadeianovoscampos(textbox _text, eventargs e) { n++; if (_text.text != null) { if (_text.name == "txtacessorio2") { c = 3; } else { if (_text.name == "txtacessorio3") { c = 4; } else { if (_text.name == "txtacessorio4") { c = 5; } else { if (_text.name == "txtacessorio5") { c = 6; } else { if (_text.name == "txtacessorio6") { c = 7; } else { if (_text.name == "txtacessorio7") { c = 8; } else { if (_text.name == "txtacessorio8") { c = 9; } else { if (_text.name == "txtacessorio9") { c = 10; } } } } } } } } if (n >= 1 && n <= c) { n2 = n2 + 30; n3 = n3 + 30; n4 = n4 + 30; n5 = n5 + 30; gpbcategoria.size = new system.drawing.size(283, n4); this.height = n5; btnapagar.location = new point(108, n3); btnsalvar.location = new point(212, n3); textbox txt = new textbox(); txt.name = "txtacessorio" + n; txt.text = ""; txt.size = new system.drawing.size(189, 26); txt.location = new point(87, n2); gpbcategoria.controls.add(txt); testelogico = txt.name; btnsalvar.tag = 2; txt.textchanged += new eventhandler(new_onchange); txt.leave += new eventhandler(erase_onleave); label lbl = new label(); lbl.name = "lblacessorio" + n; lbl.text = "acessório nº" + n + ":"; lbl.location = new point(4, n2 + 5); gpbcategoria.controls.add(lbl); } else { n--; } } }
and delete textbox/labels
+ resize windowsform,etc on textbox focus leave (if empty):
void erase_onleave(object sender, eventargs e) { cadeiaapagarcampos(sender textbox, e); } private void cadeiaapagarcampos(textbox _text, eventargs e) { if (_text.text == "") { n--; if (gpbcategoria.controls.count < 4) { } else { if (n >= 1 && n <= 10) { n2 = n2 - 30; n3 = n3 - 30; n4 = n4 - 30; n5 = n5 - 30; int count = gpbcategoria.controls.count - 2; gpbcategoria.size = new system.drawing.size(283, n4); this.height = n5; btnapagar.location = new point(108, n3); btnsalvar.location = new point(212, n3); gpbcategoria.controls.remove(_text); gpbcategoria.controls.removeat(count); } } } }
its hard explain all, because code big , possible solve problem if person know how code work. if need add additional information, ask that, near, can try make work.
fast summary (can try hehe)
i want fix: 1 - delete
current textbox
, side label
created on runtime if focus
leave current textbox
(if .text
empty
): works if focus leave using mouse click on part of windows form, if use tab on textbox
focus
leave, not works, crash , return error. 2 - add button
save function check if last textbox
created empty or not , if it, delete
last created in runtime textbox/label
(side-side), resize windows form , change button
localization (can use current delete
code developed make these changes)
that's it, know big , hard understand, try big , impossible understand/fix without know how entire code works.
remove textbox if text empty
update: uploaded project code below github. can open project sharpdevelop 4.3.
in event textbox can call/fire textbox_leaveevent method:
void textbox_leaveevent(object sender, eventargs e) { var tb = sender textbox; // add textbox if tb has text if(textboxlist.count<5 && tb.text.length>0){ var newtextbox = getnewtextbox(textboxlist.count); textboxlist.add(newtextbox); } // remove textbox if has no text else if(tb.text.length == 0){ removetextbox(tb); } }
this following:
- if current textbox (the sender fired event) has text adds textbox generic list of textboxes:
list<textbox> textboxlist = new list<textbox>();
- if textbox has no text (
tb.text.length == 0
) removed list , windows form calling removetextbox
this method remove textbox
void removetextbox(textbox tb){ // this.controls.removebykey(tb.name); int tbindex = this.controls.indexof(tb); this.controls[tbindex].dispose(); textboxlist.remove(tb); }
this 1 add textbox dynamically form
textbox getnewtextbox(int i) { var tb = new textbox(); tb.location = new system.drawing.point(220, 90 + * 24); tb.name = "tb_" + i.tostring(); tb.size = new system.drawing.size(80,20); tb.text = "textbox_"+i.tostring(); //string.empty; tb.leave += new system.eventhandler(this.textbox_leaveevent); this.controls.add(tb); this.refresh(); return tb; }
some pointers regarding code
from code example above assume change nested if () { if (){} else { if}
simpler using if() else if() or using switch (see msdn or dotnetperls). far can tell code gave can remove lot of code if use generic list of labels , textboxes.
string textboxname = _text.name; switch (textboxname) { case "txtacessorio2": c= 3; break; case "txtacessorio3": c=4; break; default: c=0; break; }
nice article and very useful thank so for C# Switch
ReplyDeletenice article and very useful thank so for Bitwise Operator
ReplyDelete