【问题标题】:VB.NET - RichTextBox - Apply formatting to selected textVB.NET - RichTextBox - 将格式应用于选定的文本
【发布时间】:2010-09-11 15:54:31
【问题描述】:

我的表单上有一个 RichTextBox 控件。我也有这个按钮,标记为 Bold,如果有人选择 RichTextBox 中的文本,然后按下按钮,所选文本变为粗体。 有什么办法吗?最终用户的简单日常任务。谢谢。

【问题讨论】:

    标签: vb.net visual-studio winforms formatting text-editor


    【解决方案1】:

    上面的一个变体,考虑到根据当前选定文本的字体信息打开/关闭粗体:

        With Me.rtbDoc
            If .SelectionFont IsNot Nothing Then
                Dim currentFont As System.Drawing.Font = .SelectionFont
                Dim newFontStyle As System.Drawing.FontStyle
    
                If .SelectionFont.Bold = True Then
                    newFontStyle = currentFont.Style - Drawing.FontStyle.Bold
                Else
                    newFontStyle = currentFont.Style + Drawing.FontStyle.Bold
                End If
    
                .SelectionFont = New Drawing.Font(currentFont.FontFamily, currentFont.Size, newFontStyle)
            End If
        End With
    

    它可能需要清理一下,我是从一个较旧的项目中提取的。

    【讨论】:

      【解决方案2】:

      您需要使用 RichTextBox 的 .SelectionFont 属性并为其分配一个具有所需样式的 Font 对象。

      示例 - 此代码将在按钮的事件处理程序中:

      Dim bfont As New Font(RichTextBoxFoo.Font, FontStyle.Bold)
      RichTextBoxFoo.SelectionFont = bfont
      

      【讨论】:

      • 哦,是的,如果未选中按钮,字体如何恢复正常?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 2010-11-20
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多