我想使用组合框更改为richtextbox中的字体大小,如果我们在选择文本中使用此代码有一种字体,则可以轻松更改字体大小。
RichTextBox1.SelectionFont = New Font(SelectionFont.FontFamily, CInt(ToolStripComboBox3.Text), RichTextBox1.SelectionFont.Style)
但是如果我们的选择中有多种字体,它就不起作用了。我有另一个代码来解决这个问题。但是这个代码的唯一问题是它不仅仅是2000个字符,但是当选择文本很大时它就没用了。代码如下。任何帮助...
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
答案 0 :(得分:0)
这是代码: 用于字体大小组合框的Xaml:
<ComboBox Width="50" Name="FontsSizeCombo" FontSize="12" ItemsSource="{Binding}">
<ComboBoxItem Content="8"/>
<ComboBoxItem Content="9" />
<ComboBoxItem Content="10" />
<ComboBoxItem Content="11" />
<ComboBoxItem Content="12" />
<ComboBoxItem Content="14" />
<ComboBoxItem Content="16" />
<ComboBoxItem Content="18" />
<ComboBoxItem Content="22" />
<ComboBoxItem Content="24" />
<ComboBoxItem Content="26" />
<ComboBoxItem Content="36" />
<ComboBoxItem Content="48" />
<ComboBoxItem Content="72" />
</ComboBox>
工作Vb.Net代码:
Private Sub FontsSizeCombo_DropDownClosed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontsSizeCombo.DropDownClosed
mainRTB.Selection.ApplyPropertyValue(RichTextBox.FontSizeProperty, FontsSizeCombo.Text)
End Sub
mainRtb是我的富文本框。如果正确,请将此作为答案进行检查。谢谢。