none
Font of Selected text in the RichTextBox Visual Studio 2019 RRS feed

  • Question

  • Good evening! I've got a problem. I need to create a text Editor on VB, where Font can be changed. NOT the whole RichTextBox must be changed - only Selected text.

    Example: I select text and click on "Format -> Font", change a font, click OK - and my selected text is of this Font. Other text must stay the same.

    I extremely need your help!

    Saturday, November 16, 2019 8:22 PM

All replies

  • Hi

    I use a Function to return a valid Font which is then applied to the RTB Selection Font

    RTB.SelectionFont = Choosefont(RTB.SelectionFont)
    
    
    	Public Function Choosefont(ByVal fon As Font) As Font
    		Dim c As New FontDialog()
    		c.Font = fon
    		c.ShowColor = False
    		c.ShowApply = True
    		c.ShowEffects = True
    		c.ShowHelp = False
    		If (c.ShowDialog() = Windows.Forms.DialogResult.OK) Then Return c.Font
    		Return fon
    	End Function
    
    


    Regards Les, Livingston, Scotland

    Saturday, November 16, 2019 9:07 PM
  • Hi,

    I used the following controls:

    The following effects are achieved:

        Private Sub Openfile_Click(sender As Object, e As EventArgs) Handles Openfile.Click
            Dim file_open As New OpenFileDialog
            file_open.Filter = "Rich Text File (*.rtf)|*.rtf| Plain Text File (*.txt)|*.txt"
            file_open.FilterIndex = 1
            file_open.Title = "Open text or RTF file"
            Dim stream_type As RichTextBoxStreamType
            stream_type = RichTextBoxStreamType.RichText
    
            If DialogResult.OK = file_open.ShowDialog() Then
                If String.IsNullOrEmpty(file_open.FileName) Then Return
                If file_open.FilterIndex = 2 Then stream_type = RichTextBoxStreamType.PlainText
                RichTextBox1.LoadFile(file_open.FileName, stream_type)
            End If
        End Sub
    
        Private Sub Font_Click(sender As Object, e As EventArgs) Handles Font.Click
            Dim result As DialogResult = FontDialog1.ShowDialog()
            If result = DialogResult.OK Then
                RichTextBox1.SelectionFont = FontDialog1.Font
            End If
        End Sub

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 18, 2019 8:40 AM
    Moderator