Check if selected text on richtextbox is not all bold or mixed [C#] [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-04 11:37:21

In an RTF text, \b indicates start of a bold part of text. So you can first check if richTextBox1.SelectionFont.Bold is true, then it means the text is all bold, otherwise, if the selected rtf contains \b it means the content is mixes, otherwise there is no bold text in selected text:

private void button1_Click(object sender, EventArgs e)
{
    if (richTextBox1.SelectionFont == null)
        return;
    if (richTextBox1.SelectionFont.Bold)
        MessageBox.Show("All text is Bold");
    else if (richTextBox1.SelectedRtf.Replace(@"\\", "").IndexOf(@"\b") > -1)
        MessageBox.Show("Mixed Content");
    else
        MessageBox.Show("Text doesn't contain Bold");
}

To test the solution, it's enough to initialize the RichtextBox with such value:

this.richTextBox1.SelectedRtf = @"{\rtf1\fbidis\ansi\ansicpg1256\deff0\deflang1065" +
    @"{\fonttbl{\f0\fnil\fcharset0 Calibri;}}\uc1\pard\ltrpar" +
    @"\lang9\b\f0\fs22 T\b0 his is a \b test}";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!