|
-
May 19th, 2012, 06:03 AM
#13
Thread Starter
Lively Member
Re: Change multiple selected fonts in richtextbox vb.net
 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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|