Results 1 to 14 of 14

Thread: [RESOLVED] Change multiple selected fonts in richtextbox vb.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Resolved [RESOLVED] Change multiple selected fonts in richtextbox vb.net

    iam using this code to change the font in a richtextbox it's work if user select a single font if there are multiple fonts, and font sizes it dosenot work, any help please

    Code:
    If ToolStripComboBox2.Items.Contains(ToolStripComboBox2.Text) Then
    RichTextBox1.SelectionFont = New Font(ToolStripComboBox2.Text, RichTextBox1.SelectionFont.Size, RichTextBox1.SelectionFont.Style)
    End If

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Change multiple selected fonts in richtextbox vb.net

    That's because the SelectionFont is set to Nothing when there is more than one font in the selection.

    I don't know a solution, but at least you can prevent the error by doing this check
    vb Code:
    1. If RichTextBox1.SelectionFont IsNot Nothing Then
    2.             RichTextBox1.SelectionFont = New Font("Arial", RichTextBox1.SelectionFont.Size, RichTextBox1.SelectionFont.Style)
    3.  
    4.         Else
    5.             RichTextBox1.SelectionFont = New Font("Arial", RichTextBox1.Font.Size, RichTextBox1.Font.Style)
    6.         End If



  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: Change multiple selected fonts in richtextbox vb.net

    i got your point, but still waiting for solution

  4. #4
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Change multiple selected fonts in richtextbox vb.net

    vb Code:
    1. RichTextBox1.Font = New Font(ToolStripComboBox2.Text, RichTextBox1.Font.Size, RichTextBox1.Font.Style)
    Work?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: Change multiple selected fonts in richtextbox vb.net

    i want to change selection font

  6. #6
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Change multiple selected fonts in richtextbox vb.net

    then
    vb Code:
    1. RichTextBox1.selectionFont = New Font(ToolStripComboBox2.Text, RichTextBox1.selectionFont.Size, RichTextBox1.selectionFont.Style)

    work?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: Change multiple selected fonts in richtextbox vb.net

    that is what i am doing but it dose not work with multiple fonts in selection

  8. #8
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Change multiple selected fonts in richtextbox vb.net

    I am confused then.
    SelectionFont should already be chosen as default for program on startup.
    Then why wont there be a new font created and used when selected through code?

    come on calling all forum gurus to come and put us to shame...
    Spank me momma.

  9. #9
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Change multiple selected fonts in richtextbox vb.net

    Quote Originally Posted by proneal View Post
    I am confused then.
    SelectionFont should already be chosen as default for program on startup.
    Then why wont there be a new font created and used when selected through code?

    come on calling all forum gurus to come and put us to shame...
    Spank me momma.
    To get the problem, Try this
    Start new project, add RichTextBox1 and Button1, paste this code
    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         RichTextBox1.SelectionFont = New Font("Arial", RichTextBox1.SelectionFont.Size, RichTextBox1.SelectionFont.Style)
    5.     End Sub
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         RichTextBox1.SelectionFont = New Font("Tahoma", 20)
    9.         RichTextBox1.SelectedText = "ABC "
    10.         RichTextBox1.SelectionFont = New Font("Courier New", 10)
    11.         RichTextBox1.SelectedText = "DEF"
    12.     End Sub
    13.  
    14. End Class

    Select ABC or DEF, press the button, the font changed with new font name and preserve its size.
    Select BC DE, press the button, an error occur.



  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: Change multiple selected fonts in richtextbox vb.net

    ok i got this error
    "Object reference not set to an instance of an object."

  11. #11
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Change multiple selected fonts in richtextbox vb.net

    I know theres a way to change the font size of mixed text in a RTB using API but not sure how to change the font name
    WOW, your code works perfect for changing the font name and preserve font style too.



  12. #12

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: Change multiple selected fonts in richtextbox vb.net

    Quote Originally Posted by Edgemeal View Post
    I know theres a way to change the font size of mixed text in a RTB using API but not sure how to change the font name, anyway came up with this...

    Code:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            RichTextBox1.SelectionFont = New Font("Tahoma", 12.0F)
            RichTextBox1.SelectedText = "ABCD EFG HIJKL "
            RichTextBox1.SelectionFont = New Font("Courier New", 14.25F)
            RichTextBox1.SelectedText = "123 4567 890!"
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ChangeSelTextFont(RichTextBox1, "Segoe Script")
        End Sub
    
        '---
        Private Const WM_SETREDRAW As Integer = &HB
        <System.Runtime.InteropServices.DllImport("user32.dll")> _
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        End Function
    
        Private Sub ChangeSelTextFont(rtb As RichTextBox, fontName As String)
            ' get selected length
            Dim rtbEnd As Integer = rtb.SelectionLength
            If rtbEnd = 0 Then Return ' nothin to do
            ' selection start
            Dim rtbStart As Integer = rtb.SelectionStart
            ' lock rtb redraw
            SendMessage(rtb.Handle, WM_SETREDRAW, 0, 0)
            ' Change font of each char in select text
            rtb.SelectionLength = 1
            For i As Integer = rtbStart To rtbStart + (rtbEnd - 1)
                rtb.SelectionStart = i
                rtb.SelectionFont = New Font(fontName, rtb.SelectionFont.Size, rtb.SelectionFont.Style)
            Next
            ' reselect text
            rtb.SelectionStart = rtbStart
            rtb.SelectionLength = rtbEnd
            ' unlock/redraw
            SendMessage(rtb.Handle, WM_SETREDRAW, 1, 0)
            rtb.Invalidate()
        End Sub
    End Class
    Thanks for help but,
    this code work great if we have a limited text in our selection. but its worthless with large selection of text like 100000 characters. I have more than 1 lack characters in my richtextbox. any correction please?

  13. #13
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Change multiple selected fonts in richtextbox vb.net

    Quote Originally Posted by hackerspk View Post
    Thanks for help but,
    this code work great if we have a limited text in our selection. but its worthless with large selection of text like 100000 characters. I have more than 1 lack characters in my richtextbox. any correction please?
    I think you need to use the CHARFORMAT structure and send a EM_SETCHARFORMAT message to change just the font, I googled around and came up with this, NOTE I have no idea how safe this code is, but it is fast.
    I'd suggest reading up on the topic... and use the code below at your own risk!
    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            RichTextBox1.SelectionFont = New Font("Tahoma", 12.0F)
            RichTextBox1.SelectedText = "ABCD EFG HIJKL "
            RichTextBox1.SelectionFont = New Font("Courier New", 14.25F, FontStyle.Bold)
            RichTextBox1.SelectedText = "123 4567 890!"
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SetSelectionFont(Me.RichTextBox1, "Segoe Script")
            RichTextBox1.Focus()
        End Sub
    
        '--- 
        Private Const EM_SETCHARFORMAT As Integer = 1092
        Private Const SCF_SELECTION As Integer = 1
        Private Const CFM_FACE As UInteger = &H20000000
    
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure CHARFORMAT
            Public cbSize As Integer
            Public dwMask As UInteger
            Public dwEffects As UInteger
            Public yHeight As Integer
            Public yOffset As Integer
            Public crTextColor As Integer
            Public bCharSet As Byte
            Public bPitchAndFamily As Byte
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
            Public szFaceName As Char()
            ' CHARFORMAT2 from here onwards.
            Public wWeight As Short
            Public sSpacing As Short
            Public crBackColor As Integer
            Public LCID As Integer
            Public dwReserved As UInteger
            Public sStyle As Short
            Public wKerning As Short
            Public bUnderlineType As Byte
            Public bAnimation As Byte
            Public bRevAuthor As Byte
        End Structure
    
        <DllImport("user32", CharSet:=CharSet.Auto)> _
        Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As Integer, ByRef lp As CHARFORMAT) As Integer
        End Function
    
        Private Sub SetSelectionFont(rtb As RichTextBox, font As String)
            Dim fmt As New CHARFORMAT()
            fmt.cbSize = Marshal.SizeOf(fmt)
            fmt.dwMask = CFM_FACE
            Dim f As Char() = New Char(31) {}
            For i As Integer = 0 To font.Length - 1
                f(i) = font(i)
            Next
            fmt.szFaceName = f
            SendMessage(rtb.Handle, EM_SETCHARFORMAT, SCF_SELECTION, fmt)
        End Sub
    
    End Class

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: [RESOLVED] Change multiple selected fonts in richtextbox vb.net

    thanks it worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width