In the following example you can see that how you can change the font color and font style of all label controls using defined menu items.
Image may be NSFW.
Clik here to view.
The example requires a menu control and label controls on the windows form. See the above picture. User can change the fore color of the all used labels from the menu options.
- Public Class Form1
- Private Sub BlackToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlackToolStripMenuItem.Click
- For Each ctrl As Control In Me.Controls
- If TypeOf (ctrl) Is Label Then
- ctrl.ForeColor = Color.Black
- End If
- Next
- End Sub
- Private Sub BlueToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
- For Each ctrl As Control In Me.Controls
- If TypeOf (ctrl) Is Label Then
- ctrl.ForeColor = Color.Blue
- End If
- Next
- End Sub
- Private Sub RedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedToolStripMenuItem.Click
- For Each ctrl As Control In Me.Controls
- If TypeOf (ctrl) Is Label Then
- ctrl.ForeColor = Color.Red
- End If
- Next
- End Sub
- Private Sub BoldToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BoldToolStripMenuItem.Click
- For Each ctrl As Control In Me.Controls
- If TypeOf (ctrl) Is Label Then
- ctrl.Font = New Font(ctrl.Font.FontFamily, ctrl.Font.Size, ctrl.Font.Style Xor FontStyle.Bold)
- End If
- Next
- End Sub
- Private Sub ItalicToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ItalicToolStripMenuItem.Click
- For Each ctrl As Control In Me.Controls
- If TypeOf (ctrl) Is Label Then
- ctrl.Font = New Font(ctrl.Font.FontFamily, ctrl.Font.Size, ctrl.Font.Style Xor FontStyle.Italic)
- End If
- Next
- End Sub
- Private Sub RegularToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegularToolStripMenuItem.Click
- For Each ctrl As Control In Me.Controls
- If TypeOf (ctrl) Is Label Then
- ctrl.Font = New Font(ctrl.Font.FontFamily, ctrl.Font.Size, ctrl.Font.Style Xor FontStyle.Regular)
- End If
- Next
- End Sub
- End Class