Ansprechen aller Contols in einem ToolStropMenu

  • VB.NET

Es gibt 16 Antworten in diesem Thema. Der letzte Beitrag () ist von Westerwälder.

    Ansprechen aller Contols in einem ToolStropMenu

    Hallo,

    wie der Titel schon sagt, möchte ich alle Controls in einem Toolstripmenu ändern. Dies gelingt mir bis auf die MenuItem's.

    VB.NET-Quellcode

    1. Dim TooLMenueEintrag As ToolStripDropDownButton
    2. For Each Toolitem As System.Windows.Forms.ToolStripItem In Me.ToolStrip_Oben.Items
    3. With Toolitem
    4. .BackColor = Color.Blue
    5. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    6. End With
    7. If TypeOf Toolitem Is ToolStripDropDownButton Then
    8. TooLMenueEintrag = New ToolStripDropDownButton
    9. TooLMenueEintrag.Name = Toolitem.Name
    10. For Each SubToolItem As ToolStripMenuItem In TooLMenueEintrag.DropDownItems
    11. ' Hier passiert nichts
    12. With Toolitem
    13. .BackColor = Color.Blue
    14. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    15. End With
    16. Next
    17. End If
    18. Next
    Gruß Markus
    Danke, habe den Fehler gesehen aber nach der Änderung tut sich leider immer noch nichts.

    VB.NET-Quellcode

    1. For Each SubToolItem As ToolStripMenuItem In TooLMenueEintrag.DropDownItems
    2. ' Hier passiert nichts
    3. With SubToolItem
    4. .BackColor = Color.Blue
    5. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    6. End With
    7. Next
    Gruß Markus
    hmm,
    der ToolStripDropDownButton (Name Menue) hat mehrere ToolStripMenuItem, die wiederum weitere ToolStripMenuItem enthalten.
    Der Code ändert den ToolStripDropDownButton, doch alles was in ihm enthalten ist (ToolStripMenuItem) wird durch den Code nicht angesprochen.
    Gruß Markus
    Jo, so ein Menü ist eine baumartige STruktur.

    Alle Knoten eines Baumes besuchen kann man mit einer rekursiven Methode.
    Also die Methode wird mit einer Liste von MenüItem aufgerufen, geht alle Items durch, macht was mit denen, und holt sich aus jedem Item die UnterItems, und ruft sich dann selbst auf, mit diesen UnterItems.
    Hallo, rekursiv ist mir schon klar, aber soweit komme ich gar nicht. Habe mal die Fehler bzw. Mängel im Code grün ausgewiesen

    VB.NET-Quellcode

    1. Private TS_Control As System.Windows.Forms.Control
    2. Private TS_Hauütelemente As System.Windows.Forms.ToolStripItem
    3. Private TS_Label As System.Windows.Forms.ToolStripLabel
    4. Private TS_DDButton As System.Windows.Forms.ToolStripDropDownButton
    5. Private TS_MenueItem As System.Windows.Forms.ToolStripMenuItem
    6. Public Sub SystemToolstrip_Formatieren(ByVal SysToolStrip As System.Windows.Forms.ToolStrip)
    7. With SysToolStrip
    8. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    9. End With
    10. For Each Me.TS_Hauütelemente In SysToolStrip.Items
    11. With Me.TS_Hauütelemente
    12. .BackColor = SystemToolStripFarbe
    13. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    14. ' Hintergrundfarbe der Label im ToolStripMenu ändern sich nicht
    15. End With
    16. If TypeOf Me.TS_Hauütelemente Is System.Windows.Forms.ToolStripDropDownButton Then
    17. Me.TS_DDButton = New System.Windows.Forms.ToolStripDropDownButton
    18. Me.TS_DDButton.Name = Me.TS_Hauütelemente.Name
    19. MessageBox.Show(Me.TS_DDButton.Name) ' Der korrekte Name wird angezeigt
    20. For Each Me.TS_MenueItem In TS_DDButton.DropDownItems
    21. MessageBox.Show(Me.TS_MenueItem.Name) ' Hier wird nichts angezeigt, obwohl einige Einträge vorhanden sind
    22. Next
    23. End If
    24. Next
    25. End Sub
    Gruß Markus

    VB.NET-Quellcode

    1. .BackColor = SystemToolStripFarbe
    Hast Du da mal Color.Green oder so probiert?

    VB.NET-Quellcode

    1. For Each Me.TS_MenueItem In TS_DDButton.DropDownItems
    2. MessageBox.Show(Me.TS_MenueItem.Name) ' Hier wird nichts angezeigt, obwohl einige Einträge vorhanden sind
    3. Next
    For Each erzeugt eine lokale Variable, mach das mal ohne Me.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Zu 1) Mit ändern der Backcolor-Zuweisung auf Color.green ändert sich nichts
    Zu 2) Lasse ich me. weg, dann wird die Variable grün mit dem Hinweis: Der Type für die Variable wird nicht abgleitet, da er an ein Feld in einem einschließenden Bereich gebunden ist ....
    Gruß Markus
    Danke für die neue Information, aber es bleibt wie es ist

    VB.NET-Quellcode

    1. Private TS_Control As System.Windows.Forms.Control
    2. Private TS_Hauptelemente As System.Windows.Forms.ToolStripItem
    3. Private TS_Label As System.Windows.Forms.ToolStripLabel
    4. Private TS_DDButton As System.Windows.Forms.ToolStripDropDownButton
    5. Private TS_MenueItem As System.Windows.Forms.ToolStripMenuItem
    6. Public Sub SystemToolstrip_Formatieren(ByVal SysToolStrip As System.Windows.Forms.ToolStrip)
    7. With SysToolStrip
    8. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    9. End With
    10. For Each Me.TS_Hauptelemente In SysToolStrip.Items
    11. With Me.TS_Hauptelemente
    12. .BackColor = SystemToolStripFarbe
    13. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    14. ' Hintergrundfarbe der Label im ToolStripMenu ändern sich nicht
    15. End With
    16. If TypeOf TS_Hauptelemente Is System.Windows.Forms.ToolStripDropDownButton Then
    17. TS_DDButton = New System.Windows.Forms.ToolStripDropDownButton
    18. TS_DDButton.Name = TS_Hauptelemente.Name
    19. MessageBox.Show(TS_DDButton.Name) ' Der korrekte Name wird angezeigt
    20. For Each BS_MenueItem As System.Windows.Forms.ToolStripMenuItem In TS_DDButton.DropDownItems.OfType(Of ToolStripMenuItem)()
    21. MessageBox.Show(BS_MenueItem.Name) ' Hier wird nichts angezeigt, obwohl einige Einträge vorhanden sind
    22. Next
    23. End If
    24. Next
    25. End Sub
    Gruß Markus
    Nun funktioniert die Geschichte, bis auf den Backcolor der Label (verstehe ich nicht)

    Hier nochmal der gesamte Code
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private TS_Control As System.Windows.Forms.Control
    2. Private TS_Hauptelemente As System.Windows.Forms.ToolStripItem
    3. Private TS_Label As System.Windows.Forms.ToolStripLabel
    4. Private TS_Button As System.Windows.Forms.ToolStripButton
    5. Private TS_DDButton As System.Windows.Forms.ToolStripDropDownButton
    6. Private TS_MenueItem As System.Windows.Forms.ToolStripMenuItem
    7. Private TS_SubMenueItem As System.Windows.Forms.ToolStripMenuItem
    8. Public Sub SystemToolstrip_Formatieren(ByVal SysToolStrip As System.Windows.Forms.ToolStrip)
    9. For Each Me.TS_Label In SysToolStrip.Items.OfType(Of ToolStripLabel)()
    10. With Me.TS_Label
    11. .Text = TS_Label.Name ' Wird reingeschrieben
    12. .BackColor = SystemToolStripFarbe ' Funktioniert nicht
    13. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    14. End With
    15. Next
    16. For Each Me.TS_Button In SysToolStrip.Items.OfType(Of ToolStripButton)()
    17. With Me.TS_Button
    18. .BackColor = SystemToolStripFarbe
    19. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    20. End With
    21. Next
    22. For Each Me.TS_DDButton In SysToolStrip.Items.OfType(Of ToolStripDropDownButton)()
    23. With Me.TS_DDButton
    24. .BackColor = SystemToolStripFarbe
    25. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    26. End With
    27. For Each Me.TS_MenueItem In Me.TS_DDButton.DropDownItems.OfType(Of ToolStripMenuItem)()
    28. With Me.TS_MenueItem
    29. .BackColor = SystemToolStripFarbe
    30. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    31. End With
    32. Call SystemToolStrip_Formatieren_SubItems(Me.TS_MenueItem)
    33. Next
    34. Next
    35. End Sub
    36. Private Sub SystemToolStrip_Formatieren_SubItems(ByVal Eintrag As System.Windows.Forms.ToolStripMenuItem)
    37. For Each Me.TS_SubMenueItem In Eintrag.DropDownItems.OfType(Of ToolStripMenuItem)()
    38. With Me.TS_SubMenueItem
    39. .BackColor = SystemToolStripFarbe
    40. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    41. End With
    42. Call SystemToolStrip_Formatieren_SubItems(Me.TS_SubMenueItem)
    43. Next
    44. End Sub


    Gruß Markus

    RodFromGermany schrieb:

    For Each erzeugt eine lokale Variable, mach das mal ohne Me.
    und schmeiß alle Variablen Private TS_XXX raus.
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Sub SystemToolstrip_Formatieren(ByVal SysToolStrip As System.Windows.Forms.ToolStrip)
    2. For Each _Label In SysToolStrip.Items.OfType(Of ToolStripLabel)()
    3. With _Label
    4. .Text = _Label.Name ' Wird reingeschrieben
    5. .BackColor = SystemToolStripFarbe ' Funktioniert nicht
    6. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    7. End With
    8. Next
    9. For Each _Button In SysToolStrip.Items.OfType(Of ToolStripButton)()
    10. With _Button
    11. .BackColor = SystemToolStripFarbe
    12. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    13. End With
    14. Next
    15. For Each _Button In SysToolStrip.Items.OfType(Of ToolStripDropDownButton)()
    16. With _Button
    17. .BackColor = SystemToolStripFarbe
    18. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    19. End With
    20. For Each _Item In _Button.DropDownItems.OfType(Of ToolStripMenuItem)()
    21. With _Item
    22. .BackColor = SystemToolStripFarbe
    23. .Font = Drawing.Font.FromHfont(Monitor.SchriftNormal.ToHfont)
    24. End With
    25. Call SystemToolStrip_Formatieren_SubItems(_Item)
    26. Next
    27. Next
    28. End Sub
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Ja und, was hat das mit Deinen Variablen zu tun?

    VB.NET-Quellcode

    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2. ToolStrip1.RenderMode = ToolStripRenderMode.System
    3. ToolStripLabel1.BackColor = Color.Red
    4. End Sub
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!