【问题标题】:Set selection font size in RichTextBox for multiple fonts in vb.net在RichTextBox中为vb.net中的多种字体设置选择字体大小
【发布时间】: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


    【解决方案1】:

    代码如下: 字体大小组合框的 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 是我的富文本框。如果它是正确的,请检查这个作为答案。谢谢。

    【讨论】:

    • 错误:选择不是 System.Windows.Forms.RichTextBox iam 使用 VB2008 EXpress 的成员
    • 那么它需要框架3.5,我正在使用框架2.0,希望有其他解决方案
    • 目前迁移到更高版本的 .net 是一个不错的选择,这只是我的意见。
    • .net framework 2 只有 20 MB,3.5 是 300 MB,我的应用只有 1MB 或更少,谁为我的应用安装 300MB?
    • 这不回答问题?
    【解决方案2】:

    这里我改变了一些东西

    Public rtbTemp As New RichTextBox()

    Public Sub ChangeFontSize(ByVal rtb As RichTextBox, ByVal fontSize As Single, ByVal fntName As String)
    
        If fontSize <= 0.0 Then
            Throw New System.InvalidProgramException("Invalid font size parameter to ChangeFontSize")
        End If
        Dim rtb1start As Double = rtb.SelectionStart
        Dim len As Double = rtb.SelectionLength
        Dim rtbTempStart As Double = 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(fntName, fontSize, rtb.SelectionFont.Style)
            Return
        End If
        ' Step through the selected text one char at a time
        rtbTemp.Rtf = rtb.SelectedRtf
        For i As Double = 0 To len - 1
            rtbTemp.[Select](rtbTempStart + i, 1)
            rtbTemp.SelectionFont = New Font(fntName, fontSize, rtbTemp.SelectionFont.Style)
        Next
    
        ' Replace & reselect
        rtbTemp.[Select](rtbTempStart, len)
        rtb.SelectedRtf = rtbTemp.SelectedRtf
        rtb.[Select](rtb1start, len)
        Return
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-29
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 2010-11-23
      相关资源
      最近更新 更多