Autor Beitrag
chris300695
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Sa 03.12.11 20:35 
Hi,

und wie kann ich die TextBox aus der Form.Controls-Collection löschen? Wenn die TextBox nicht dynamisch wäre, wärs ja kein Problem, aber wie geht das nun?

Moderiert von user profile iconTh69: abgeteilt aus beliebige Anzahl von TextBoxen zur Laufzeit erstellen
chris300695 hat folgendes geschrieben:

Jetzt will ich aber noch ein Button hinzufügen, der das der list "textboxen" zuletzt hinzugefügte Element entfernt.
Mein Quellcode:
ausblenden C#-Quelltext
1:
2:
3:
4:
private void button9_Click(object sender, EventArgs e)
{
   textboxen.RemoveAt(textboxen.Count - 1);
}


Es tut sich aber nichts, wenn ich den Button9 anklicke.

Th69 hat folgendes geschrieben:

Hallo Chris,

du mußt die TextBox natürlich auch noch aus der Form.Controls-Collection löschen (denn diese wird ja angezeigt).
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4796
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 04.12.11 12:35 
Hallo Chris,

mittels der Form.Controls.Remove(control)-Methode kannst du einfach das Control (in deinem Fall die TextBox) löschen. Und an die (letzte) TextBox kommst du ja dann über deine eigene TextBox-Liste heran.
chris300695 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: So 04.12.11 14:06 
So kommt aber die Fehlermeldung, dass "textbox" im aktuellen Kontext nicht vorhanden ist:


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
private void button9_Click(object sender, EventArgs e)
{
    int i = textboxen.Count - 1;

    textboxen.RemoveAt(i);
    groupBox1.Controls.Remove(textbox);
}
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4796
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 04.12.11 14:36 
Du mußt ja eben die TextBox-Variable noch setzen:
ausblenden C#-Quelltext
1:
TextBox textbox = textboxen[i];					

Beachte, daß du dies vor dem RemoveAt machst ;-)
chris300695 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: So 04.12.11 14:43 
Vielen Dank für deine Hilfe.

Hat jetzt geklappt. ;)