Hello, I have a code thats changes colors for special words and sets brackets for sql query.
But I have problem whan I press button it's makes all text blue.
I type a same code in richtextbox like this: "select aaa from bbb where a=1 or a=2 or a=3 and b=2 or b=3 and c=2 or c=3". It's make words colored like 'select, from, where, and, or to' color blue.
Can someone tell me why it's color all text blue after I press button and program adds brackets ?
Here is my code:
http://paste.php.lv/7cd1b3da1717783542b2a414d73a1e30?lang=csharp
Loading

FroglegRank #1,020 · 1.9K reputationPosted Feb 21, 2011, 3:06 AM EST
HighlightPhrase(richTextBox1,richTextBox1.Text,Color.Black);
and then called the method to color the text again
FroglegRank #1,020 · 1.9K reputationPosted Feb 21, 2011, 1:45 AM EST
private void button1_Click(object sender, EventArgs e)
{
highLightTextBlue("Select");
highLightTextBlue("where");
highLightTextBlue(" or");
}
public void highLightTextBlue(string wh)
{
int stIndex = 0;
stIndex = richTextBox1.Find(wh, stIndex, RichTextBoxFinds.MatchCase);
while (stIndex != -1)
{
if (richTextBox1.SelectedText == wh )
{
richTextBox1.SelectionLength = wh.Length;
richTextBox1.SelectionColor = Color.Blue;
}
stIndex = richTextBox1.Find(wh, stIndex + 1, RichTextBoxFinds.MatchCase);
}
}
Note = only blue text changed - you will have to create a method for other colors
Janis Peksa145 reputationPosted Feb 21, 2011, 1:51 AM EST
And I need on TextChange event not on click event.
My code works fine with text color change.
But whan I press button it's adds brackets
where needed but makes all text blue why ? :(
Maybe someone can show me how to make it right ?
FroglegRank #1,020 · 1.9K reputationPosted Feb 21, 2011, 1:58 AM EST
put
highLightTextBlue("Select");
highLightTextBlue("where");
highLightTextBlue(" or");
in richTextBox1_TextChanged event
once you type last letter it should change text color as required
Janis Peksa145 reputationPosted Feb 21, 2011, 2:02 AM EST
It's works 50/50. Selects word and color it and stay selected, I don't need it to be selected.
Try my code and you will see what I need.
And you will see what problem I have.
FroglegRank #1,020 · 1.9K reputationPosted Feb 21, 2011, 2:11 AM EST
Janis Peksa145 reputationPosted Feb 21, 2011, 2:24 AM EST
Did you try my code and see the problem ?
FroglegRank #1,020 · 1.9K reputationPosted Feb 21, 2011, 2:57 AM EST
Without knowing the full string I'm only guessing
Janis Peksa145 reputationPosted Feb 21, 2011, 2:59 AM EST
I don't know how to change all text to black.
And send send my method again.
I tried like this: richTextbox1.ForeColor = Color.Black;
But nothing happends.
Maybe you know how to change all text to black ?
Janis Peksa145 reputationPosted Feb 21, 2011, 3:13 AM EST