RichTextBox
コンボボックス使用時のフォントサイズを変更したい。このコードを使用して、選択テキストに 1 つのフォントがある場合、フォント サイズを簡単に変更できます。
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