用户更改RichTextBox中所选文本的字体

时间:2016-06-22 20:48:14

标签: vb.net fonts richtextbox

这应该很容易,但它是在逃避我。我在VB.NET中有一个用户输入文本的RichTextBox。我希望他们能够选择一些文本,然后更改选择的字体属性。

2 个答案:

答案 0 :(得分:1)

以下是我已经为您快速编写的内容。

它将获得系统中所有已安装的字体并将其添加到组合框中,因此您无需手动添加它们。

每当你改变我添加的组合框的字体类型时,我都会这样做,它会更新RichTextBox的字体。

Imports System.Drawing.Text
Public Class Form1
''CREATE ANOTHER COMBOBOX TO CHANGE THE SIZE OF THE TEXT USING THE SAME METHOD
''AS THE FONT COMBOBOX.
Dim FONTSIZE = 8
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim InstalledFonts = New InstalledFontCollection
    Dim FontFamilies() As FontFamily = InstalledFonts.Families

    For Each Font As FontFamily In FontFamilies
        ComboBox1.Items.Add(Font.Name)
    Next
    ''THE END USER WONT BE ABOUT TO EDIT THE INSTALLED ITEMS IN THE COMBOBOX
    ''THE STARTING FONT IS CONSOLAS
    ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    ComboBox1.Text = "Consolas"
    End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    ''THIS WILL CHANGE THE HIGHLIGHTED "SELECTED" TEXT FONT ONLY
    ''AS ASKED FOR IN QUESTION
    RichTextBox1.SelectionFont = New Drawing.Font(ComboBox1.Text, FONTSIZE)
End Sub
End Class

您需要在表单中添加一个组合框,并为此工作添加一个richtextbox。

如果您有任何问题,请告诉我,我会尽力帮助您工作。

答案 1 :(得分:0)

谢谢Werdna,你的回答给了我一些方向。我确实使用了FontDialog,因为最后我想允许其他字体更改,例如样式和颜色。

Private Sub rtf_Notes_MouseUp(sender As Object, e As MouseEventArgs) Handles rtf_Notes.MouseUp
    'Test for right-click
    If (e.Button = Windows.Forms.MouseButtons.Right) Then
        With FontDialog1
            .ShowColor = True
            If (.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                rtf_Notes.SelectionFont = New Drawing.Font(.Font.Name, .Font.Size, .Font.Style)
                rtf_Notes.SelectionColor = .Color
            End If
        End With
    End If
End Sub