3

How to check if selected text on richtextbox that

its chars is not all bold.

For example:

notboldboldnotbold ← this is mixed.
Im not all bold ← this is not all bold

This is the code I have made, it checks selected text on richtextbox whether the text contains some bolded text or not.
its slow because its checking the char one by one using Selection.Start to Selection.Length and check if bold. If I use richTextBox1.SelectionFont.Bold it will return false because its not all bold, that means also if its mixed with bold and not bold.

bool notallbold = 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)
    {
        notallbold = false;
        richTextBox1.SelectionStart = 0;
        richTextBox1.SelectionLength = 0;
        richTextBox1.SelectionStart = start;
        richTextBox1.SelectionLength = end;
        richTextBox1.Focus();
    }
}

When checking long string, I can see the text is getting bolded when checking. Is there any efficient way than this?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Calvin
  • 605
  • 10
  • 26
  • Can we suppose all selected text uses the same font? Because if the selected text contains more than one font, `SelectionFont` returns null. – Reza Aghaei Oct 15 '16 at 05:05
  • @RezaAghaei - being bold would make it a different font, yeah? – Clockwork-Muse Oct 15 '16 at 05:06
  • No, it's the same font. – Reza Aghaei Oct 15 '16 at 05:08
  • No, fonts with different styles are explicitly considered not equal. So 'Arial' and 'ArialBold' are considered two different fonts. Besides, real-world conditions mean it's more likely that eventually you get multiple fonts in there anyways. – Clockwork-Muse Oct 15 '16 at 05:13
  • 1
    @Clockwork-Muse In general yes fonts with different styles are different, but here as mentioned in [`SelectionFont`](https://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont(v=vs.110).aspx#Anchor_1) documentation, *if the current text selection has more than one font specified, this property is null.* . I tested and if a text with a single font family contains some parts with different styles, the property is not null and it means the control decides using font family name. That's why I said it's the same font. – Reza Aghaei Oct 15 '16 at 05:20
  • 1
    @RezaAghaei - .... :headdesk: .... so, despite the two fonts not being considered equal in the backend (ie, there actually are two `Font` instances, and `Equals` returns false), it happily returns one (with whatever the style is at the start, presumably). Well, there goes the binary search idea. – Clockwork-Muse Oct 15 '16 at 06:05
  • 1
    It worth reading [this post](http://stackoverflow.com/questions/5325918/how-do-i-maintain-richtext-formatting-bold-italic-etc-when-changing-any-one-el) and [this one](http://stackoverflow.com/questions/3282384/richtextbox-syntax-highlighting-in-real-time-disabling-the-repaint). You can continue the way you are going, just turn off redraw of control or use another invisible instance to get better performance. – Reza Aghaei Oct 15 '16 at 07:17

1 Answers1

3

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}";
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • The selection font will be null if there are multiple fonts, so it could be mixed content. Also, it would probably be better not to need to replace all the escape characters first.... – Clockwork-Muse Oct 15 '16 at 05:35
  • @Clockwork-Muse I don't claim it's the best solution, but it works for all cases which selected text uses the same font name. And about `Replace(@"\\", "")` it's because `"\\b"` should not be considered as start of bold. Users which need a better solution can use an RTF parser or wait for a better solution :) – Reza Aghaei Oct 15 '16 at 05:43