FontStyle
枚举应用了属性,这Flags
意味着您可以将值与Or
运算符组合。例如,要同时应用粗体和斜体,您可以使用:
FontStyle.Bold Or FontStyle.Italic
您拥有的代码实际上是切换一种样式,最正确的方法是这样的:
myFont = New Font(myFont, myFont.Style Xor FontStyle.Bold)
如果不是,则将应用Bold
它,如果它不影响任何其他样式,则将其删除,因此您可以有两个单独的按钮或菜单项,每个按钮或菜单项切换一种样式。
如果您希望能够有选择地应用多种样式,例如使用CheckBoxes
then 我会建议这样的事情:
Dim style = FontStyle.Regular
If useBold Then
style = style Or FontStyle.Bold
End If
If useItalic Then
style = style Or FontStyle.Italic
End If
myFont = New Font(myFont, style)
编辑:
这是我刚刚整理的一个更完整的示例,用于Buttons
切换样式:
Private Sub boldButton_Click(sender As Object, e As EventArgs) Handles boldButton.Click
ToggleStyle(FontStyle.Bold)
End Sub
Private Sub italicButton_Click(sender As Object, e As EventArgs) Handles italicButton.Click
ToggleStyle(FontStyle.Italic)
End Sub
Private Sub underlineButton_Click(sender As Object, e As EventArgs) Handles underlineButton.Click
ToggleStyle(FontStyle.Underline)
End Sub
Private Sub ToggleStyle(style As FontStyle)
Me.Label1.Font = New Font(Me.Label1.Font, Me.Label1.Font.Style Xor style)
End Sub
这是一个CheckBoxes
用于选择样式的类似示例:
Private Sub boldCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles boldCheckBox.CheckedChanged
SetStyle()
End Sub
Private Sub italicCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles italicCheckBox.CheckedChanged
SetStyle()
End Sub
Private Sub underlineCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles underlineCheckBox.CheckedChanged
SetStyle()
End Sub
Private Sub SetStyle()
Dim style = FontStyle.Regular
If Me.boldCheckBox.Checked Then
style = style Or FontStyle.Bold
End If
If Me.italicCheckBox.Checked Then
style = style Or FontStyle.Italic
End If
If Me.underlineCheckBox.Checked Then
style = style Or FontStyle.Underline
End If
Me.Label1.Font = New Font(Me.Label1.Font, style)
End Sub