[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
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:
If RichTextBox1.SelectionFont IsNot Nothing Then
RichTextBox1.SelectionFont = New Font("Arial", RichTextBox1.SelectionFont.Size, RichTextBox1.SelectionFont.Style)
Else
RichTextBox1.SelectionFont = New Font("Arial", RichTextBox1.Font.Size, RichTextBox1.Font.Style)
End If
Re: Change multiple selected fonts in richtextbox vb.net
i got your point, but still waiting for solution
Re: Change multiple selected fonts in richtextbox vb.net
vb Code:
RichTextBox1.Font = New Font(ToolStripComboBox2.Text, RichTextBox1.Font.Size, RichTextBox1.Font.Style)
Work?
Re: Change multiple selected fonts in richtextbox vb.net
i want to change selection font
Re: Change multiple selected fonts in richtextbox vb.net
then
vb Code:
RichTextBox1.selectionFont = New Font(ToolStripComboBox2.Text, RichTextBox1.selectionFont.Size, RichTextBox1.selectionFont.Style)
work?
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
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.
Re: Change multiple selected fonts in richtextbox vb.net
Quote:
Originally Posted by
proneal
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:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RichTextBox1.SelectionFont = New Font("Arial", RichTextBox1.SelectionFont.Size, RichTextBox1.SelectionFont.Style)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RichTextBox1.SelectionFont = New Font("Tahoma", 20)
RichTextBox1.SelectedText = "ABC "
RichTextBox1.SelectionFont = New Font("Courier New", 10)
RichTextBox1.SelectedText = "DEF"
End Sub
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.
Re: Change multiple selected fonts in richtextbox vb.net
ok i got this error
"Object reference not set to an instance of an object."
Re: Change multiple selected fonts in richtextbox vb.net
Quote:
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.
Re: Change multiple selected fonts in richtextbox vb.net
Quote:
Originally Posted by
Edgemeal
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?
Re: Change multiple selected fonts in richtextbox vb.net
Quote:
Originally Posted by
hackerspk
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
Re: [RESOLVED] Change multiple selected fonts in richtextbox vb.net