【发布时间】:2012-05-27 03:29:03
【问题描述】:
我想使用组合框更改RichTextBox 中的字体大小。如果我们使用此代码在选择文本中使用一种字体,则很容易更改字体大小。
RichTextBox1.SelectionFont = New Font(SelectionFont.FontFamily, CInt(ToolStripComboBox3.Text), RichTextBox1.SelectionFont.Style)
但如果我们在选择中有多种字体,它就不起作用了。
我有另一个选择的代码来解决这个问题。 But the only problem with this code is that it is good for less then 2000 characters - but when the selection text is large it is worthless.请看下面的代码:
Public rtbTemp As New RichTextBox()
Public Sub ChangeFontSize(ByVal rtb As RichTextBox, ByVal fontSize As Single)
'This method should handle cases that occur when multiple fonts/styles are selected
' Parameters:-
' fontSize - the fontsize to be applied, eg 33.5
If fontSize <= 0.0 Then
Throw New System.InvalidProgramException("Invalid font size parameter to ChangeFontSize")
End If
Dim rtb1start As Integer = rtb.SelectionStart
Dim len As Integer = rtb.SelectionLength
Dim rtbTempStart As Integer = 0
' If len <= 1 and there is a selection font, amend and return
If len <= 1 AndAlso rtb.SelectionFont IsNot Nothing Then
rtb.SelectionFont = New Font(rtb.SelectionFont.FontFamily, fontSize, rtb.SelectionFont.Style)
Return
End If
' Step through the selected text one char at a time
rtbTemp.Rtf = rtb.SelectedRtf
For i As Integer = 0 To len - 1
rtbTemp.[Select](rtbTempStart + i, 1)
rtbTemp.SelectionFont = New Font(rtbTemp.SelectionFont.FontFamily, fontSize, rtbTemp.SelectionFont.Style)
Next
' Replace & reselect
rtbTemp.[Select](rtbTempStart, len)
rtb.SelectedRtf = rtbTemp.SelectedRtf
rtb.[Select](rtb1start, len)
Return
End Sub
【问题讨论】:
标签: vb.net richtextbox