Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically delete TextBox and label if TextBox.Text = Empty on focus leave
    primarykey
    data
    text
    <p>I have code that windows form starts with only 1 <code>Label</code> and 1 <code>TextBox</code>, when user starts to type on <code>TextBox1</code>,It creates a new TextBox and label down (also change location of 2 <code>Buttons</code> and change windows form size,It happens to max of 10 text box + 10 labels (side-side) like:</p> <pre><code>(label1) Enter Name 1: - Textbox1 Imput (label2) Enter Name 2: - Textbox2 Imput (label1) Enter Name 3: - Textbox3 Imput ... </code></pre> <p>It works great, but have a little "problem":</p> <ul> <li>my code creates a new <code>TextBox</code>/<code>Label</code> when user starts to type on last <code>TextBox</code></li> <li>if the user stops on <code>Textbox7</code> for example, my code will have created <code>TextBox8</code> although it is not needed and does not contain text (is blank), </li> <li>therefore I would like <code>Delete</code> it automatically if the user tabs from <code>TextBox7</code> to <code>TextBox8</code> and leaves <code>TextBox8</code> (without entering text into tb8)</li> </ul> <p>My code isn't working perfectly (will explain below) and if I click on <code>Button</code> that will verify if last <code>TextBox</code> text is <code>Empty</code> and if it is, will <code>Delete</code> text box and label at side and changes localization for <code>Buttons</code> and windows form size). </p> <p>I have too many problems because <code>TextBox</code> 2 to 10 are created at runtime and its not possible to reference these "future" <code>TextBox</code> in code because I get error saying that it not exist in actual code.</p> <p>Problem of <code>TextBox</code> and label <code>Delete</code> on <code>TextBox</code> focus leave when <code>TextBox.Text</code> is <code>Empty</code> is that it work great if I focus in another place in my focus using mouse click but if I press tab it <code>Delete</code> 2 <code>TextBox</code> and crash and return error: The index 12(can be any number) is out of range.</p> <p>See my code to create new <code>TextBox</code> and labels + resize form and windows form size on txtNomecategoria_TextChanged:</p> <pre><code>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 &gt; 1) { n++; if (n &lt;= 1) { n = 2; } if (n &gt;= 1 &amp;&amp; n &lt;= 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--; } } } </code></pre> <p>note that it create 2 new event for new runtime created <code>TextBox</code>: </p> <pre><code>txt.TextChanged += new EventHandler(new_onchange); txt.Leave += new EventHandler(erase_onLeave); </code></pre> <p>So here we go (create new <code>TextBox/Label</code> + resize windowsform,etc):</p> <pre><code>void new_onchange(object sender, EventArgs e) { cadeianovoscampos(sender as 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 &gt;= 1 &amp;&amp; n &lt;= 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--; } } } </code></pre> <p>And delete <code>TextBox/Labels</code> + resize windowsform,etc on Textbox focus leave (if Empty): </p> <pre><code>void erase_onLeave(object sender, EventArgs e) { cadeiaapagarcampos(sender as TextBox, e); } private void cadeiaapagarcampos(TextBox _text, EventArgs e) { if (_text.Text == "") { n--; if (gpbCategoria.Controls.Count &lt; 4) { } else { if (n &gt;= 1 &amp;&amp; n &lt;= 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); } } } } </code></pre> <p>Its really to hard explain all, because code is big and its only possible solve problem if person know how code work. If I need add any additional information, only ask for that, its near, but with your help I can try to make it work. </p> <hr> <p>Fast summary (can try hehe)</p> <p>I want fix: 1 - <code>Delete</code> current <code>TextBox</code> and side <code>Label</code> created on runtime if <code>Focus</code> leave current <code>TextBox</code> (if <code>.Text</code> is <code>Empty</code>): It works only if I Focus leave using mouse click on any part of windows form, but if i use tab on <code>TextBox</code> to <code>Focus</code> leave, it just not works, crash and return error. 2 - Add <code>Button</code> Save Function to check if last <code>TextBox</code> created is Empty or not and if it, it will <code>Delete</code> last created in runtime <code>TextBox/Label</code> (side-side), resize windows form and change <code>Button</code> localization (can use current <code>Delete</code> code that I developed to make these changes)</p> <p>That's it, but I know that its big and hard to understand, I will try but its really big and its impossible understand/fix without know how entire code works.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload