2016-06-22 53 views
0

這應該很簡單,但是它無法顯示。我在VB.NET中有一個用戶輸入文本的RichTextBox。我希望他們能夠選擇一些文本,然後更改選擇的字體屬性。用戶更改RichTextBox中選定文本的字體

+1

見[SelectionFont](https://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont(V = vs.110)的.aspx) – LarsTech

回答

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

豈不一個'FontDialog'對此更好? :) –

+1

哈哈哈那裏的樂趣在哪裏? – Werdna

+1

Uuh ... uuuuuh ...好的,沒什麼。 ;) –

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 
+0

好吧,所以我沒有得到足夠的睡眠。刪除重複的.ShowDialog,一切都很好。 – Terabithia

+0

高興地幫助:) – Werdna