3

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

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

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?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Calvin
  • 605
  • 10
  • 26
  • 1
    For starters, you could break out of the for loop by adding `break;` as soon as you find something that is not bold. – Darren Wainwright Oct 14 '16 at 18:41
  • 1
    You can check `richTextBox1.SelectionFont.Bold`, it returns true if all selected text is bold. What's the problem? – Reza Aghaei Oct 14 '16 at 18:46
  • Not sure it works; did you test it @Reza? What would actually be needed is the third value for mixed weights.. – TaW Oct 14 '16 at 18:49
  • @TaW, Yes, if `SelectionFont` is not null, then `SelectionFont.Bold` returns true if all selected text is bold. `SelectionFont` is null, when the selection contains different fonts. – Reza Aghaei Oct 14 '16 at 18:53
  • 1
    @TaW The new question is a different one and probably you missed some part of new question. It seems the new question need to be reopened. In this question the OP need to check if the whole selection is bold, in new question the OP wants to detect 3 states: the whole is bold, it's mixed content and it doesn't contains bold. The answer of this question can't be used for the new one. – Reza Aghaei Oct 15 '16 at 07:36
  • @TaW It seems the OP doesn't need another answer. But It would be great if you review the post :) – Reza Aghaei Oct 15 '16 at 11:15
  • @Reza: Will do, but am a bit tied up atm.. – TaW Oct 15 '16 at 12:06
  • @TaW No problem hope you are doing well :) - I'll wait for your opinion. – Reza Aghaei Oct 15 '16 at 12:59
  • @Reza: All is fine here, just busy. The solution below indeed works; but the other one to determine all regular and mixed doesn't work here. This seems to do it: `bool mixed = !allBold && richTextBox2.SelectedRtf.Contains("\\b");` and `bool allRegular = !allBold && !mixed;` – TaW Oct 15 '16 at 15:52
  • @TaW Great. Yes, So it seems the linked post is a different / follow up question and it's not a duplicate, although this answer will help to detect one of states. Also the answer which I posted is equivalent to what you posted here in comments. – Reza Aghaei Oct 15 '16 at 18:45
  • No, I couldn't get the other answer to work, or else I wouldn't have posted this comment. Mixed was not correctly recognized. – TaW Oct 15 '16 at 19:26
  • @TaW The only difference between your comment and my code is: for detecting mixed you used `richTextBox2.SelectedRtf.Contains("\\b")` and I used `(richTextBox1.SelectedRtf.Replace(@"\\", "").IndexOf(@"\b") > -1)`. I just guarantee that if the text itself contains `@"\b"` text which is not a bold marker, I'll not recognize it as bold. Rest of code is completely equivalent. Those `&&` and those `if/else if/else` are equivalent. It's strange if it didn't work. If you post the rtf which you tested, I'll check it. – Reza Aghaei Oct 16 '16 at 05:13
  • Whoops, you are right! (I had used your else clause alone in a separate check, but without the check for allBold which the else clause __implied__.) Sorry for being so obtuse. So: The other answer is correct too! – TaW Oct 16 '16 at 07:17

1 Answers1

3

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


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));
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Hi @Reza-Aghaei Thx for the Answer. But I just realized that this morning that I am checking whether the text that is selected have all chars that in it is not all bold.Since this post is answered, I'm creating another question. Here is the question if you want to help.
    [http://stackoverflow.com/questions/40054607/check-if-selected-text-on-richtextbox-is-not-all-bold-or-mixed]
    – Calvin Oct 15 '16 at 03:28