关键字颜色改变(类似SQL效果)
请问大家,我想做成1个效果和MS SQL里关键字颜色改变一样的效果,现在我颜色是能变了,但是会有相应的BUG,实在不知道
怎么改,1个BUG:关键字后打出空格后,在将光标移到关键字后随便打什么字符会出现问题 2个BUG:如果把整句话粘贴复制,字体也不会改变 3个BUG:如果关键和别的字符相连了,颜色应该灰掉.
完全类似SQL的关键字机理,谢谢大家帮帮看看代码怎么改
private string[] wT = { "like","from" };
private string[] wTT2 = { "where"};
private string[] wTT3 = { "and","not" };
private string[] wTT4 = { "or" };
private string[] wTT5 = { "select" };
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
this.richTextBox1.SelectionColor = Color.Black;
if (this.richTextBox1.SelectionStart >= 2)
{
this.richTextBox1.Select((this.richTextBox1.SelectionStart = this.richTextBox1.SelectionStart - 2), 2);
foreach (string wt in wTT4)
{
if (this.richTextBox1.SelectedText.ToLower() == wt)
{
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.SelectedText = this.richTextBox1.SelectedText.ToUpper();
}
}
this.richTextBox1.SelectionLength = 0;
this.richTextBox1.SelectionStart += 2;
this.richTextBox1.SelectionColor = Color.Black;
}
if (this.richTextBox1.SelectionStart >= 3)
{
this.richTextBox1.Select((this.richTextBox1.SelectionStart = this.richTextBox1.SelectionStart - 3), 3);
foreach (string wt in wTT3)
{
if (this.richTextBox1.SelectedText.ToLower() == wt)
{
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.SelectedText = this.richTextBox1.SelectedText.ToUpper();
}
}
this.richTextBox1.SelectionLength = 0;
this.richTextBox1.SelectionStart += 3;
this.richTextBox1.SelectionColor = Color.Black;
}
if (this.richTextBox1.SelectionStart >= 4)
{
this.richTextBox1.Select((this.richTextBox1.SelectionStart = this.richTextBox1.SelectionStart - 4), 4);
foreach (string wt in wT)
{
if (this.richTextBox1.SelectedText.ToLower() == wt)
{
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.SelectedText = this.richTextBox1.SelectedText.ToUpper();
}
}
this.richTextBox1.SelectionLength = 0;
this.richTextBox1.SelectionStart += 4;
this.richTextBox1.SelectionColor = Color.Black;
}
if (this.richTextBox1.SelectionStart >= 5)
{
this.richTextBox1.Select((this.richTextBox1.SelectionStart = this.richTextBox1.SelectionStart - 5), 5);
foreach (string wt in wTT2)
{
if (this.richTextBox1.SelectedText.ToLower() == wt)
{
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.SelectedText = this.richTextBox1.SelectedText.ToUpper();
}
}
this.richTextBox1.SelectionLength = 0;
this.richTextBox1.SelectionStart += 5;
this.richTextBox1.SelectionColor = Color.Black;
}
if (this.richTextBox1.SelectionStart >= 6)
{
this.richTextBox1.Select((this.richTextBox1.SelectionStart = this.richTextBox1.SelectionStart - 6), 6);
foreach (string wt in wTT5)
{
if (this.richTextBox1.SelectedText.ToLower() == wt)
{
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.SelectedText = this.richTextBox1.SelectedText.ToUpper();
}
}
this.richTextBox1.SelectionLength = 0;
this.richTextBox1.SelectionStart += 6;
this.richTextBox1.SelectionColor = Color.Black;
}
if (this.richTextBox1.SelectionStart >= 7)
{
this.richTextBox1.Select((this.richTextBox1.SelectionStart = this.richTextBox1.SelectionStart - 7), 7);
foreach (string wtt in wTT5)
{
if (this.richTextBox1.SelectedText.ToLower() == wtt)
{
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.SelectedText = this.richTextBox1.SelectedText.ToUpper();
}
}
this.richTextBox1.SelectionLength = 0;
this.richTextBox1.SelectionStart += 7;
this.richTextBox1.SelectionColor = Color.Black;
}
}