问题
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?
回答1:
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));
来源:https://stackoverflow.com/questions/40049981/check-if-selected-text-on-richtextbox-is-all-bold-or-not