2

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

4

2 回答 2

3

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

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

Dim bfont As New Font(RichTextBoxFoo.Font, FontStyle.Bold)
RichTextBoxFoo.SelectionFont = bfont
于 2008-09-20T19:11:03.080 回答
3

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

    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

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

于 2008-09-22T17:49:06.320 回答