检查Richtextbox上的选定文本是否全部为粗体 [英] Check if selected text on richtextbox is all bold or not

查看:109
本文介绍了检查Richtextbox上的选定文本是否全部为粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查richtextbox上的选定文本是否全部为粗体。例如:

How to check if selected text on richtextbox is all bold. For example:

asdasd asd asd←这不是全部粗体

我全部粗体←这都是粗体

asdasdasdasd ← this is not all bold
Im all bold ← this is all bold

这是我编写的代码,它可以检查其是否全部为粗体,但速度较慢,因为它使用 Selection.Start Selection.Length 并检查是否为粗体。

This is the code I have made, it can check if its all bold or not but its slow because its checking the char one by one using Selection.Start to Selection.Length and check if bold.

bool allbold = true;
int start = richTextBox1.SelectionStart;
int end = richTextBox1.SelectionLength;
for (int i = 1; i < end; i++)
{
    richTextBox1.SelectionStart = start+i;
    richTextBox1.SelectionLength = 1;
    if (!richTextBox1.SelectionFont.Bold)
    {
        allbold = false;
        richTextBox1.SelectionStart = 0;
        richTextBox1.SelectionLength = 0;
        richTextBox1.SelectionStart = start;
        richTextBox1.SelectionLength = end;
        richTextBox1.Focus();
    }
}

还有没有比这更有效的方法了?

Is there any efficient way than this?

推荐答案

您可以检查 richTextBox1.SelectionFont.Bold 。如果所有选定的文本均为粗体,则返回true。

You can check richTextBox1.SelectionFont.Bold. It returns true if all selected text is bold.

要进行测试,只需使用这样的值初始化 RichTextBox

To test, it's enough to initialize RichTextBox with such value:

richTextBox1.SelectedRtf = @"{\rtf1\fbidis\ansi\ansicpg1256\deff0" +
    @"\deflang1065{\fonttbl{\f0\fnil\fcharset0 Calibri;}}\uc1\pard\ltrpar" +
    @"\lang9\b\f0\fs72 T\fs22 his\b0  \b i\b0 s a \b t\b0 est.}";

然后以这种方式检查其他选择:

Then check different selection this way:

if (richTextBox1.SelectionFont != null)
    MessageBox.Show(string.Format("{0}", richTextBox1.SelectionFont.Bold));

这篇关于检查Richtextbox上的选定文本是否全部为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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