VB.NET - RichTextBox - 将格式应用于选定的文本 [英] VB.NET - RichTextBox - Apply formatting to selected text

查看:277
本文介绍了VB.NET - RichTextBox - 将格式应用于选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的窗体上有一个 RichTextBox 控件。如果有人选择了 RichTextBox 中的文本,然后按下按钮,则所选文本变为粗体。粗体 / strong>有办法做到这一点?简单的日常任务,为最终用户。感谢。

解决方案

根据当前所选文本的字体信息,考虑切换粗体打开/关闭的变体。

 使用Me.rtbDoc 
如果.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
否则
newFontStyle = currentFont.Style + Drawing.FontStyle.Bold
End If

.SelectionFont =新的Drawing.Font(currentFont.FontFamily,currentFont.Size,newFontStyle)
End If
End With

可能需要清理一下,从一个较旧的项目。

I have a RichTextBox control on my form. I also have this button, labeled Bold, that I want, if someone selects text in the RichTextBox, then presses the button, the selected text turns bold. Any way to do that? Simple, everyday task for end users. Thanks.

解决方案

A variation on the above that takes into consideration switching bold on/off depending on the currently selected text's font info:

    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

It may need cleaned up a bit, I pulled this from an older project.

这篇关于VB.NET - RichTextBox - 将格式应用于选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆