110,545
社区成员
发帖
与我相关
我的任务
分享
public partial class RichTextBox : Form
{
public RichTextBox()
{
InitializeComponent();
}
private void tSql_TextChanged(object sender, EventArgs e) //文本框改变事件
{
int index = this.tSql.SelectionStart; //记录修改的位置
this.tSql.SelectAll();
this.tSql.SelectionColor = Color.Black;
string[] keystr =...{ "select ", "from ", "where ", " and ", " or ", " order ", " by ", " desc ", " when ", " case ",
" then ", " end ", " on ", " in ", " is ", " else ", " left ", " join ", " not ", " null " };
for (int i = 0; i < keystr.Length; i++)
this.getbunch(keystr[i], this.tSql.Text);
this.tSql.Select(index, 0); //返回修改的位置
this.tSql.SelectionColor = Color.Black;
}
public int getbunch(string p, string s) //给关键字上色
{
int cnt = 0; int M = p.Length; int N = s.Length;
char[] ss = s.ToCharArray(), pp = p.ToCharArray();
if (M > N) return 0;
for (int i = 0; i < N - M + 1; i++)
{
int j;
for (j = 0; j < M; j++)
{
if (ss[i + j] != pp[j]) break;
}
if (j == p.Length)
{
this.tSql.Select(i, p.Length);
this.tSql.SelectionColor = Color.Blue;
cnt++;
}
}
return cnt;
}
}
public partial class RichTextBox : Form
{
public RichTextBox()
{
InitializeComponent();
}
private void tSql_TextChanged(object sender, EventArgs e) //文本框改变事件
{
int index = this.tSql.SelectionStart; //记录修改的位置
this.tSql.SelectAll();
this.tSql.SelectionColor = Color.Black;
string[] keystr ={ "select ", "from ", "where ", " and ", " or ", " order ", " by ", " desc ", " when ", " case ", " then ", " end ", " on ", " in ", " is ", " else ", " left ", " join ", " not ", " null " };
for (int i = 0; i < keystr.Length; i++)
this.getbunch(keystr[i], this.tSql.Text);
this.tSql.Select(index, 0); //返回修改的位置
this.tSql.SelectionColor = Color.Black;
}
public int getbunch(string p, string s) //给关键字上色
{
int cnt = 0; int M = p.Length; int N = s.Length;
char[] ss = s.ToCharArray(), pp = p.ToCharArray();
if (M > N) return 0;
for (int i = 0; i < N - M + 1; i++)
{
int j;
for (j = 0; j < M; j++)
{
if (ss[i + j] != pp[j]) break;
}
if (j == p.Length)
{
this.tSql.Select(i, p.Length);
this.tSql.SelectionColor = Color.Blue;
cnt++;
}
}
return cnt;
}
}