Autor Beitrag
bd.cole
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 46

Win XP, Vista
C# (VS 2005)
BeitragVerfasst: Fr 07.09.07 08:08 
Hallo ich habe einen panel auf dem mehrere Buttons dynamisch erstellt werden und name bekommen (Button_1 - Button_x). Jetzt möcht ich während der Laufzeit einen button löschen. Ich haben den Namen von dem Button, aber nicht mehr. Wie kann ich den löschen? Kenne halt nur panel1.Controls.remove()
Gibt es irgend ein befehl. Remove Button where name is blalba oder so? Oder wie macht man das?
Oder irgendwie: Button temp = (Button)...
Mir fällt nix ein
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Fr 07.09.07 09:10 
Hallo,

ich dachte auch, es gäbe eine Methode, die nach dem Namen sucht. Da scheint mich meine Erinnerung zu täuschen; jedenfalls finde ich in der Doku nichts. Das lässt sich aber leicht nachbauen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
private Control FindControlByName(Control parentControl, string name) {
  Control Result = null;
  foreach(Control ctrl in parentControl.Controls) {
    if (ctrl.Name == name) {
      Result = ctrl;
      break;
    }
  }
  return Result;
}

Erklärungen dafür erübrigen sich ja wohl.

Mit dem gefundenen Control kannst Du dann machen, was Du willst: Remove oder neu färben oder... Aber aufpassen: Wenn nichts gefunden wird, bleibt das Result null. Und auf die rekursive Suche in untergeordneten Controls verzichte ich hier.

Jürgen
bd.cole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 46

Win XP, Vista
C# (VS 2005)
BeitragVerfasst: Fr 07.09.07 09:31 
Thx hat funktioniert

Mfg Cole
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Fr 07.09.07 14:06 
user profile iconJüTho hat folgendes geschrieben:
ich dachte auch, es gäbe eine Methode, die nach dem Namen sucht. Da scheint mich meine Erinnerung zu täuschen; jedenfalls finde ich in der Doku nichts.

Auch wenn sich Dein Problem gelöst hat: ich habe jetzt doch eine direkte Methode (ab NET 2.0) gefunden.
Zitat:
Control.ControlCollection.Find-Methode
Sucht nach Steuerelementen anhand ihrer Name-Eigenschaft und erstellt ein Array aller übereinstimmenden Steuerelemente.

Beispiel:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
private Control FindControlByName(Control parentControl, string name) {
  Control Result = null;
  Control[] = parentControl.Controls.Find(name, true);
  if (coll != null && coll.Length > 0
    Result = ctrl;
  }
  return Result;
}

Jürgen