Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Labels / buttons in ein Array Packen?

Empfohlene Antworten

Veröffentlicht

Hallo.

Ich hab in meiner Form ca. 10 Labels. Alle mit dem Namen: label1, label2, etc.

Ich möchte gerne mit einem Klick allen einen Bestimmten namen geben. ( Quelle aus einer Datei )

Normalerweise müsste ist ja immer das tun:

label1->Text = ....

etc.

Wie kann das in eine Schleife packen? Es wird ja immer die Selbe Aktion durchgeführt.

Ich hab mir das so vorgestellt:


for(int i = 0; i<11;++i)

{

labeli = ... ;

}

Danke für antworten

Dafür gibt es nur 2 Alternativen:

1) Entweder Du gehst in der Schleife über das übergeordnete Control und lässt Dir alle darin eingebetteten Controls geben und prüfst ob die Control-Klasse Label-Klasse ist. Dann kannst Du das Control mit einem explizitem Typecast in Label casten und dann den Text zuweisen.

2) Du initialisierst zu Anfang ein Array mit Deinen Labels und läufst das durch.

Variante 1) dynmisch über Controls. Voraussetzung: die Labels sind alle direkt im Form und nicht in einem untergeordneten Panel, da das nicht rekursiv ist. Wenn die Labels in einem untergeordneten Control sind, musst Du dessen Controls benutzen.

Aber Vorsicht in den Compiler-Settings muss RTTI (RunTimeTypeInformation) angeschaltet sein. Außerdem ist in c++ ein dynamic_cast zur instance-of-Ermittlung kein guter Code.


for ( int i=0, n=Controls->Count; i<n; ++i )

    if ( Label* label = dynamic_cast<Label*>(Controls[i]))

		label->Text ...

Vielleicht gibt es ja in C++.NET direkt eine Implementierung dafür. Variante 2) der ganz pragmatische Weg

Label* labels[] = {&label1, &label2, &label3, ...};

for ( int i=0, n=sizeof(labels)/sizeof(Label*);  i<n;  ++i )

	labels[i]->Text ...

  • 2 Wochen später...
  • Autor

Tut mir leid, dass ich erst so Spät antworte, aber ich war im Urlaub.


Label* labels[] = {&label1, &label2, &label3, ...};

for ( int i=0, n=sizeof(labels)/sizeof(Label*);  i<n;  ++i )

	labels[i]->Text ...

Der Hier gefällt mir nicht, weil ich jedes Label selbst eingeben muss ( label1,label2,label3, ... )

Der Andere Hingegen sieht besser aus. Aber dafür kompliziert

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.