MS VB 2008:字体设置练习

时间:2014-07-11 07:48:00

标签: vb.net fonts

我目前正在办公室的VB 2008中练习一些简单的代码,显然这不是我的预期。所以我正在做一些事情,我有三组选项:Font ColorSizeStyleFont Color包含基本的彩虹色。 Size的{​​{1}}大小包含10-5010's。而Style是基础,ItalicBoldUnderline

这是代码。

Public Class Form1
    Private Sub rbRed_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbRed.CheckedChanged
        txt1.ForeColor = Color.Red
    End Sub

    Private Sub rbOrange_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbOrange.CheckedChanged
        txt1.ForeColor = Color.Orange
    End Sub

    Private Sub rbYellow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbYellow.CheckedChanged
        txt1.ForeColor = Color.Yellow
    End Sub

    Private Sub rbGreen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGreen.CheckedChanged
        txt1.ForeColor = Color.Green
    End Sub

    Private Sub rbBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbBlue.CheckedChanged
        txt1.ForeColor = Color.Blue
    End Sub

    Private Sub rbIndigo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbIndigo.CheckedChanged
        txt1.ForeColor = Color.Indigo
    End Sub

    Private Sub rbViolet_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbViolet.CheckedChanged
        txt1.ForeColor = Color.Violet
    End Sub

    Private Sub rbSize10_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSize10.CheckedChanged
        txt1.Font = New Font(txt1.Font.Name, 10, txt1.Font.Style)
    End Sub

    Private Sub rbSize20_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSize20.CheckedChanged
        txt1.Font = New Font(txt1.Font.Name, 20, txt1.Font.Style)
    End Sub

    Private Sub rbSize30_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSize30.CheckedChanged
        txt1.Font = New Font(txt1.Font.Name, 30, txt1.Font.Style)
    End Sub

    Private Sub rbSize40_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSize40.CheckedChanged
        txt1.Font = New Font(txt1.Font.Name, 40, txt1.Font.Style)
    End Sub

    Private Sub rbSize50_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSize50.CheckedChanged
        txt1.Font = New Font(txt1.Font.Name, 50, txt1.Font.Style)
    End Sub

    Private Sub rbSize60_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txt1.Font = New Font(txt1.Font.Name, 60, txt1.Font.Style)
    End Sub

    Private Sub rbSize70_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txt1.Font = New Font(txt1.Font.Name, 70, txt1.Font.Style)
    End Sub

    Private Sub rbSize80_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txt1.Font = New Font(txt1.Font.Name, 80, txt1.Font.Style)
    End Sub

    Private Sub rbSize90_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txt1.Font = New Font(txt1.Font.Name, 90, txt1.Font.Style)
    End Sub

    Private Sub rbSize100_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txt1.Font = New Font(txt1.Font.Name, 100, txt1.Font.Style)
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        txt1.Font = New Font(txt1.Font.Name, FontStyle.Italic)
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        txt1.Font = New Font(txt1.Font.Name, FontStyle.Bold)
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        txt1.Font = New Font(txt1.Font.Name, FontStyle.Underline)
    End Sub
End Class

我的问题是,无论何时我选择Font Style,都会恢复原始文字大小。我知道它与Check Boxes中的代码有关。每当我在Font Style中选择一个选项时,如何才能使它们大小相同?以及如何让它们同时混淆( SAMPLE )?对你们来说这一定非常简单,但我需要你的帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

您需要一个小辅助函数,可以在现有字体上打开或关闭样式:

Private Function ChangeFontStyle(fnt As Font, style As FontStyle, enable As Boolean) As Font
    Dim newstyle = fnt.Style
    If enable Then
        newstyle = newstyle Or style
    Else
        newstyle = newstyle And (Not style)
    End If
    Return New Font(fnt, newstyle)
End Function

现在你可以写:

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    txt1.Font = ChangeFontStyle(txt1.Font, FontStyle.Italic, CheckBox1.Checked)
End Sub

对其他两个重复,现在使用FontStyle.Bold和FontStyle.Underline

在此代码中唯一有点难以理解的是使用And / Or / Not运算符。这是打开或关闭的标准方法。如果类型具有<Flags>属性,它也可以在Enum类型上工作。像FontStyle一样。