Alle Label in einer Form Farbe ändern

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 15 Antworten in diesem Thema. Der letzte Beitrag () ist von aepping.

    Alle Label in einer Form Farbe ändern

    Hallo Community,
    ich habe schon ein bisschen gegoogelt, aber nichts gefunden, was nur bei Labeln die Farbe ändert.
    Bis jetzt schaut es bei mir so aus:

    VB.NET-Quellcode

    1. ​ColorDialog1.ShowDialog()
    2. Label1.ForeColor = ColorDialog1.Color
    3. Label2.ForeColor = ColorDialog1.Color
    4. Label3.ForeColor = ColorDialog1.Color
    5. Label4.ForeColor = ColorDialog1.Color
    6. Label5.ForeColor = ColorDialog1.Color

    Und immer so weiter...
    Und ja, ich arbeite mit den Standardnamen, ist mir aber egal.
    Was ich gerne hätte, wäre soetwas:

    VB.NET-Quellcode

    1. ​Dim allelabel as control = label
    2. label.ForeColor = ColorDialog1.Color


    Gibt es soetwas, und wenn ja, wie geht es?

    Danke für eure Hilfe,

    Michdi :)
    Die beste maschinelle Übersetzung der Welt - DeepL Übersetzer
    Alle Zitate, die ich seit dem 1.9.2017 übersetzt habe, wurden vollautomatisch mit DeepL übersetzt.



    VB.NET-Quellcode

    1. ​For Each lbl As Label In Controls.OfType(Of Label)()
    2. lbl.ForeColor = ColorDialog1.Color
    3. Next


    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Dann musst Du halt explizit die Controls des TabControls abrufen.

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:

    VB.NET-Quellcode

    1. ​For Each lbl As Label In Controls.OfType(Of Label)()
    2. If TypeOf lbl Is TabControl Then
    3. For Each child In lbl.Controls
    4. If TypeOf child Is Label Then
    5. CType(child, Label).BackColor = Color.Red
    6. End If
    7. Next
    8. End If
    9. Next


    Klappt irgendwie auch nicht so ganz :(
    Bin noch neu
    Die beste maschinelle Übersetzung der Welt - DeepL Übersetzer
    Alle Zitate, die ich seit dem 1.9.2017 übersetzt habe, wurden vollautomatisch mit DeepL übersetzt.



    Was machst Du da? Warum prüfst Du nochmal explizit den Typ, wenn die Collection schon nur Labels enthält? Und dann Prüfung auf TabControl?
    Du musst unbedingt verstehen, was Du machst.
    msdn.microsoft.com/en-us/libra…l.controls(v=vs.110).aspx

    Anscheinend bist Du wirklich noch ganz neu, da Dir die absoluten Basics fehlen. Ein Grundlagenbuch wäre daher wohl eine gute Option. ;)

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Ich habe es mir jetzt mal ein bisschen angeguckt, habe es jetzt so gelöst:

    VB.NET-Quellcode

    1. ​For Each lbl As Label In TabControl1.Controls.OfType(Of Label)()
    2. lbl.BackColor = Color.Red
    3. Next

    Es passiert aber trotzdem nichts...
    Die beste maschinelle Übersetzung der Welt - DeepL Übersetzer
    Alle Zitate, die ich seit dem 1.9.2017 übersetzt habe, wurden vollautomatisch mit DeepL übersetzt.



    Sicher, dass diese Routine auch ausgeführt wird?

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Danke, jetzt klappt es endlich!
    Kann man das noch irgendwie zusammenfassen? Also so z.B. :

    VB.NET-Quellcode

    1. ​For Each lbl As Label In TabPage1.Controls.OfType(Of Label)() and For Each lbl1 As Label In TabPage2.Controls.OfType(Of Label)()

    Und so weiter?
    Weil ich habe 11 Seiten, und ich brauche das für Knöpfe, Label, Textboxen, Listen, Hintergrund... Das ist sonst so viel
    Die beste maschinelle Übersetzung der Welt - DeepL Übersetzer
    Alle Zitate, die ich seit dem 1.9.2017 übersetzt habe, wurden vollautomatisch mit DeepL übersetzt.



    @ThuCommix Jou, beachte aber, dass das Schlüsselwort ​Next ist. ;)

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    So hat man das dynamisch und kann gezielt verschiedene ControlTypen ansprechen, egal wie weit die Hierarchie reicht

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Iterator Function ListControls(ByVal TopControl As Control, ByVal ControlType As Type) As IEnumerable(Of Control)
    3. For Each ctrl as Control In TopControl.Controls.OfType(Of Control)()
    4. If ctrl.GetType Is ControlType Then Yield ctrl
    5. If ctrl.Controls.Count > 0 Then
    6. For Each child As Control In ListControls(ctrl, ControlType)
    7. If child.GetType Is ControlType Then Yield child
    8. Next
    9. End If
    10. Next
    11. End Function
    12. Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    13. For Each c As Control In ListControls(Me, GetType(Label))
    14. c.ForeColor = SystemColors.Control
    15. Next
    16. End Sub
    17. End Class

    And i think to myself... what a wonderfuL World!

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Eddy“ ()

    Hallo Eddy,
    ich bin auf deinen post gestoßen, weil ich die Schriftgröße aller controls einer tabpage mit der Tastenkombination "Ctrl und +" um 1 erhöhen möchte. Dein Code funktioniert nur bei mir nicht, ich erhalte die Fehlermeldung
    Fehler 15 Die Anweisung kann nicht außerhalb einer Methode/eines mehrzeiligen lambda-Ausdrucks stehen.
    Mein Compiler mag das "Iterator" nicht, aber ich verstehe nicht warum.
    Ich bin noch nicht so lange dabei... O:-)
    Danke für einen Tipp und viele Grüße,
    Andi
    Hallo aepping,

    leigt daran das du wohl ein aelteres Framework als 4.5 nutzt. Ab FW 4.5 gibt es Iterator wie auch Yield.

    So klappt das auch mit aelteren Framworks. Wenn du allerdings verschiedene Controls aendern mochtest, also nicht nur Labels, musst die die Methode ein wenig aendern, oder mehrfach aufrufen.

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub ChangeFont(ByVal TopControl As Control, ByVal ControlType As Type, ByVal font As Font)
    3. For Each ctrl As Control In TopControl.Controls.OfType(Of Control)()
    4. If ctrl.GetType Is ControlType Then ctrl.Font = font
    5. If ctrl.Controls.Count > 0 Then
    6. For Each child As Control In ctrl.Controls
    7. If child.GetType Is ControlType Then ctrl.Font = font
    8. If child.Controls.Count > 0 Then ChangeFont(child, ControlType, font)
    9. Next
    10. End If
    11. Next
    12. End Sub
    13. Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    14. ChangeFont(Me, GetType(Label), New Font(Font.FontFamily, 10))
    15. End Sub
    16. End Class
    And i think to myself... what a wonderfuL World!