2

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
4

2 に答える 2

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 は私のリッチ テキスト ボックスです。正しい場合は、これを回答として確認してください。ありがとうございました。

于 2012-05-20T07:38:05.740 に答える