62,074
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace SQLAnalysis
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public static Hashtable ht = new Hashtable();
private void Form2_Load(object sender, EventArgs e)
{
ht.Add("select", Color.Blue);
ht.Add("insert", Color.Blue);
ht.Add("in", Color.Blue);
ht.Add("from", Color.Blue);
ht.Add("where", Color.Blue);
ht.Add("left", Color.DeepPink);
ht.Add("join", Color.Blue);
ht.Add("*", Color.Gray);
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (this.richTextBox1.Text.Trim().Length > 0)
{
int index = this.richTextBox1.SelectionStart;
Change(index);
richTextBox1.Select(index, 0);
richTextBox1.SelectionColor = Color.Black;
}
}
private void Change(int i)
{
int index = i;
bool IsNull = IsKong(index);
int start = GetWordStart(index);
int end = GetWordEnd(index);
richTextBox1.Select(start, end - start);
string word = richTextBox1.SelectedText;
if (ht[word] != null)
{
Color c = (Color)ht[word];
richTextBox1.SelectionColor = c;
}
else
{
richTextBox1.SelectionColor = Color.Black;
}
if (IsNull)
{
index = index - 1;
Change(index);
}
}
private int GetWordStart(int end)
{
int temp = end;
while (temp > 0)
{
temp = temp - 1;
if (richTextBox1.Text[temp] == ' ' || richTextBox1.Text[temp] == '\n')
{
return temp + 1;
}
}
return 0;
}
private int GetWordEnd(int start)
{
int temp = start;
while (temp < richTextBox1.Text.Length)
{
if (richTextBox1.Text[temp] == ' ')
{
return temp;
}
temp = temp + 1;
}
return richTextBox1.Text.Length;
}
private bool IsKong(int index)
{
int temp = index;
richTextBox1.Select(index - 1, 1);
return richTextBox1.SelectedText == " " ? true : false;
}
}
}