Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Remove Textbox if text is empty</h2> <p><strong>Update:</strong> I uploaded a <a href="https://github.com/surfmuggle/StackoverflowWinformQuestions/tree/master/DeleteTextbox" rel="nofollow">project with my code</a> from below to github. You can open my project with <a href="http://www.icsharpcode.net/OpenSource/SD/Download/" rel="nofollow">sharpdevelop</a> 4.3.</p> <p>In the Event for your Textbox you can call/fire the TextBox_LeaveEvent method:</p> <pre><code>void TextBox_LeaveEvent(object sender, EventArgs e) { var tb = sender as TextBox; // add another textbox if this tb has text if(textboxList.Count&lt;5 &amp;&amp; tb.Text.Length&gt;0){ var newTextBox = getNewTextBox(textboxList.Count); textboxList.Add(newTextBox); } // remove textbox if it has no text else if(tb.Text.Length == 0){ RemoveTextBox(tb); } } </code></pre> <p>This does the following:</p> <ul> <li>if the current textbox (the sender that fired the event) has text it adds another textbox to a generic List of textboxes: <code>List&lt;TextBox&gt; textboxList = new List&lt;TextBox&gt;();</code></li> <li>if the textbox has no text (<code>tb.Text.Length == 0</code>) it is removed from the list and from the windows form by calling RemoveTextBox</li> </ul> <p>This is the method to remove a textbox</p> <pre><code>void RemoveTextBox(TextBox tb){ // this.Controls.RemoveByKey(tb.Name); int tbIndex = this.Controls.IndexOf(tb); this.Controls[tbIndex].Dispose(); textboxList.Remove(tb); } </code></pre> <p>This is the one to add the textbox dynamically to the form</p> <pre><code>TextBox getNewTextBox(int i) { var tb = new TextBox(); tb.Location = new System.Drawing.Point(220, 90 + i * 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; } </code></pre> <h3>Some pointers regarding your code</h3> <p>From the code example above I assume that you could change your nested <code>if () { if (){} else { if}</code> to something simpler by using <a href="http://www.dotnetperls.com/if" rel="nofollow">if() else if()</a> or by using switch (see <a href="http://msdn.microsoft.com/de-de/library/06tc147t%28v=VS.80%29.aspx" rel="nofollow">msdn</a> or <a href="http://www.dotnetperls.com/switch" rel="nofollow">dotnetperls</a>). As far i can tell from the code you gave you can probably remove a lot of code if you use a generic list of labels and textboxes. </p> <pre><code>string textBoxName = _text.Name; switch (textBoxName) { case "txtAcessorio2": c= 3; break; case "txtAcessorio3": c=4; break; default: c=0; break; } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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