我是在DiyFunction.cs类中新增加了一个变量集合variableDic,变量因为要实时提取,所以没有同自定义关键词的集合wordDic放在一起,各自搞一个集合,好管理。
代码:
public class DiyFunction
{
public static Dictionary<string, string> variableDic = new Dictionary<string, string>();
然后在TextChanged事件中提取变量(正则提取)
代码:
void rtb_TextChanged(object sender, EventArgs e)
{
DiyFunction.variableDic.Clear();//清空变量集合
rex = new Regex("(\\b[A-Z])[a-zA-Z]*");
mc = rex.Matches(tSql.Text);
foreach (Match m in mc)
{
if (!DiyFunction.variableDic.ContainsKey(m.Value))
{
DiyFunction.variableDic.Add(m.Value, m.Value);
}
int startIndex = m.Index;
int StopIndex = m.Length;
tSql.Select(startIndex, StopIndex);
tSql.SelectionColor = Color.DarkGoldenrod;
//tSql.SelectionFont = new Font(tSql.SelectionFont, FontStyle.Bold);
}
}
最后在KeyDown事件中匹配就可以了
//从变量集合中取词和输入词进行匹配
foreach (KeyValuePair<string, string> str in DiyFunction.variableDic)
{
if (str.Key.Length >= keyValue.Length)
{
if (str.Key.Substring(0, keyValue.Length).ToLower() == keyValue)
{
//lb.Items.Add(new KeyValuePair<string, string>(str.Key, str.Value.w2));
if (!string.IsNullOrEmpty(str.Value))
{
list.Add(new KeyValuePair<string, string>(str.Key.Replace("\\", ""), str.Value.Replace("\\[", "[").Replace("\\]", "]").Replace("\\r\\n", "\r\n").Replace("\\n", "\n")));
if (str.Key.Length > lbsize)
{
lbsize = str.Key.Length;
}
}
}
}
}