Dynamische Controls (z.B. Textboxes) wieder dynamisch entfernen

Don Philippo

Mitglied
Hallo,

ich hoffe ich hab den Titel des Threads einigermaßen gut rübergebracht?! ;-)

Ich erstelle in meinem Form per "Button-Click" mehrere Textboxes automatisch. Die Anzahl der Textboxen ist abhängig von der Anzahl an zurückgegebenen Daten einer zuvor aufgerufenen Methode ...

Soweit klappt auch alles, also sprich, das dynamische Erstellen der Textboxen.

Jetzt möchte ich jedoch, dass die Textboxen wieder vom Form verschwinden, wenn ich eine neue Berechnung starte und die "neuen" Textboxen erstellt werden oder wenn ich auf den Button "Clear" drücke.

Wie entfernt man dynamisch erstellte Controls?!
Hoffe ich hab's einigermaßen verständlich beschrieben und freue mich auf Eure Hilfe bzw. Ideen!?

Merci mal bis dahin!

Code:
        /// <summary>
        /// Creates Textboxes and shows the decoded values
        /// </summary>
        /// <param name="x0">x start position for byte matrix</param>
        /// <param name="y0">y start position for byte matrix</param>
        /// <param name="b">width of byte textbox</param>
        /// <param name="h">height of byte text box</param>
        /// <param name="iNoOfBoxes">number of byte textboxes have to be shown</param>
        /// <param name="sDataArray">data array includes the filtered string bytes</param>
        private void CreateTextboxes(int x0, int y0, int b, int h, int iNoOfBoxes, string[,] sDataArray)
        {
            int iWindowLength = 0;
            int iArrIdx = 3;

            if (tbCode.Text != "")
            {
                int count = 0;
                int iDist = 0;
                int zmax = 3;

                for (int z = 0; z < zmax; z++)
                {
                    for (int s = 0; s < iNoOfBoxes; s++)
                    {
                        TextBox tb = new TextBox();

                        if (z > 0)
                        {
                            tb.Bounds = new Rectangle(new Point(x0 + (s * b) + iDist, y0 + (z * (h + K_DIST))), new Size(b, h));
                        }
                        else
                        {
                            tb.Bounds = new Rectangle(new Point(x0 + (s * b) + iDist, y0 + (z * h)), new Size(b, h));
                        }

                        tb.BorderStyle = BorderStyle.Fixed3D;
                        count += 1;
                        iDist += K_DIST;
                        iWindowLength = x0 + (s * b) + iDist + 2;
                        tb.Name = "tb" + count.ToString();

                        // Text fuellen
                        tb.Text = sDataArray[z, iArrIdx + s];                        
                        
                        tb.TextAlign = HorizontalAlignment.Center;
                        tb.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                        tb.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                        this.Controls.Add(tb);

                        if (s >= iNoOfBoxes - 1)
                        {
                            count = 0;
                            iDist = 0;
                        }
                    }
                }
            }
        }
 
Problem gelöst! :)

Hab zusätzlich ein Panel in das Form eingefügt (nicht dynamisch!), in dem dann alle dynamischen Textboxen erstellt werden.

Also anstatt ...
Code:
...
this.Controls.Add(tb);
...

sieht das ganze nun so aus:

Code:
...
panel1.Controls.Add(tb);
...
(Kompletter Code-Teil siehe oben)

Die Routine für's Entfernen der zuvor dynamisch erstellten Textboxen (bzw. Controls) hab ich dann so gestaltet:

Code:
       private void RemoveControl(object sender, System.EventArgs e)
        {
            ArrayList list = new ArrayList(panel1.Controls);
            foreach (Control c in list)
            {
                panel1.Controls.Remove(c);
            }

        }

Und siehe da! BINGO! Es t!!

Hab die Lösung somit hier nochmal gepostet, damit der Thread soweit abgeschlossen werden kann. Evtl. gibt's ja noch elegantere Lösungen ... ! :D
 
Zurück