C# Richtextbox文字变色问题
假定:stringstr="ThisisaJquerycode,JqueryisaverygoodJavascriptLiarbry";我想让字符串中,指定所有“Jque...
假定:string str=" This is a Jquery code,Jquery is a very good Javascript Liarbry"; 我想让字符串中,指定所有“Jquery”(或更多符合的条件),变成红色字应该如何实现?
展开
2013-05-30
展开全部
你要实现的效果是不是这样?我通过处理richtextbox的keypress事件实现的,源码如下:
string temp = string.Empty;
int index = 0,itemp =0;
string key = "hello";
private bool Check(int index, char c,string value)
{
try
{
if (value.Substring(index, 1) == c.ToString())
{
return true;
}
}
catch (Exception) { }
return false;
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (itemp == 5)
{
itemp = 0;
index = 0;
}
if (Check(itemp, e.KeyChar, key))
{
itemp++;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText(e.KeyChar.ToString());
e.Handled = true;
}
else
{
richTextBox1.Select(index, itemp);
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectionStart = richTextBox1.Text.Length;
itemp = 0;
index = 0;
}
richTextBox1.SelectionColor = Color.Black;
if (e.KeyChar == 'h' && index ==0)
{
index = richTextBox1.Text.Length - 1;
}
}
如果有问题,请追问
string temp = string.Empty;
int index = 0,itemp =0;
string key = "hello";
private bool Check(int index, char c,string value)
{
try
{
if (value.Substring(index, 1) == c.ToString())
{
return true;
}
}
catch (Exception) { }
return false;
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (itemp == 5)
{
itemp = 0;
index = 0;
}
if (Check(itemp, e.KeyChar, key))
{
itemp++;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText(e.KeyChar.ToString());
e.Handled = true;
}
else
{
richTextBox1.Select(index, itemp);
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectionStart = richTextBox1.Text.Length;
itemp = 0;
index = 0;
}
richTextBox1.SelectionColor = Color.Black;
if (e.KeyChar == 'h' && index ==0)
{
index = richTextBox1.Text.Length - 1;
}
}
如果有问题,请追问
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Richtextbox支持大部分html代码的语法,设定某些字符的颜色肯定是没问题的。
通过处理richtextbox的keypress事件实现的,源码如下:
string temp = string.Empty;
int index = 0,itemp =0;
string key = "hello";
private bool Check(int index, char c,string value)
{
try
{
if (value.Substring(index, 1) == c.ToString())
{
return true;
}
}
catch (Exception) { }
return false;
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (itemp == 5)
{
itemp = 0;
index = 0;
}
if (Check(itemp, e.KeyChar, key))
{
itemp++;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText(e.KeyChar.ToString());
e.Handled = true;
}
else
{
richTextBox1.Select(index, itemp);
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectionStart = richTextBox1.Text.Length;
itemp = 0;
index = 0;
}
richTextBox1.SelectionColor = Color.Black;
if (e.KeyChar == 'h' && index ==0)
{
index = richTextBox1.Text.Length - 1;
}
}
通过处理richtextbox的keypress事件实现的,源码如下:
string temp = string.Empty;
int index = 0,itemp =0;
string key = "hello";
private bool Check(int index, char c,string value)
{
try
{
if (value.Substring(index, 1) == c.ToString())
{
return true;
}
}
catch (Exception) { }
return false;
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (itemp == 5)
{
itemp = 0;
index = 0;
}
if (Check(itemp, e.KeyChar, key))
{
itemp++;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText(e.KeyChar.ToString());
e.Handled = true;
}
else
{
richTextBox1.Select(index, itemp);
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectionStart = richTextBox1.Text.Length;
itemp = 0;
index = 0;
}
richTextBox1.SelectionColor = Color.Black;
if (e.KeyChar == 'h' && index ==0)
{
index = richTextBox1.Text.Length - 1;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-05-30
展开全部
string str = " This is a Jquery code,Jquery is a very good Javascript Liarbry";
string SubStr = "Jquery";
richTextBox2.Text = str;
richTextBox2.Select(str.IndexOf("Jquery"), SubStr.Length);
richTextBox2.SelectionColor = Color.Red;
string SubStr = "Jquery";
richTextBox2.Text = str;
richTextBox2.Select(str.IndexOf("Jquery"), SubStr.Length);
richTextBox2.SelectionColor = Color.Red;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-05-30
展开全部
Richtextbox支持大部分html代码的语法,设定某些字符的颜色肯定是没问题的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询