是这样的,一个用textbox控件中实现查询字符匹配的功能:
private void editfind_Click(object sender, EventArgs e)
{
frmChild f = new frmChild();
DialogResult ret = f.ShowDialog();
if (ret == DialogResult.OK)
{
int count = 0;
for (int i = 0; i <= text1.Text.Length; i++)
{
text1.Select(i, f.QueryWord.Length - 1);
if (f.QueryWord == text1.SelectedText)
{
//问题。。。
}
}
if (count == 0)
MessageBox.Show("无匹配文字!");
}
else if (ret == DialogResult.Cancel)
f.Close();
}
其中f是一个输入要查询的字符的子窗口,基本可以无视……
现在我的思路是:通过改变所查找到的文字的字体颜色来突出显示。。。
但是系统说text1(就是textbox1).selecttext是string型,没有forecolor等属性。。。所以一筹莫展。。。
求达人给予指点:怎么突出显示,让用户知道其所查找的文字在哪里??!!
C#如何在后台中用代码改变已选中的字体的颜色
答案:2 悬赏:50
解决时间 2021-02-02 09:31
- 提问者网友:独菊痴梦
- 2021-02-01 12:45
最佳答案
- 二级知识专家网友:留下所有热言
- 2021-02-01 13:28
单纯的textbox好像很难做到
用RichTextBox吧。。
richTextBox1.Text = "abcdefghijklmn";
richTextBox1.Select(5, 5);
richTextBox1.SelectionBackColor = Color.Red;
richTextBox1.SelectionColor = Color.White;
richTextBox1.DeselectAll();
用RichTextBox吧。。
richTextBox1.Text = "abcdefghijklmn";
richTextBox1.Select(5, 5);
richTextBox1.SelectionBackColor = Color.Red;
richTextBox1.SelectionColor = Color.White;
richTextBox1.DeselectAll();
全部回答
- 1楼网友:茫然不知崩溃
- 2021-02-01 14:17
字符是没法设置颜色的.你可以把字符放在控件里.如label1.text="a" label1.forecolor=color.fromname("#99ff00"); ------------ 看样子你是在做验证码.颜色没办法完全的随机,你可以写个数组定义颜色 color[] color ={ color.black, color.red, color.blue, color.green, color.orange, color.brown, color.brown, color.darkblue }; 再定义个字符集 char[] character ={ '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'w', 'x', 'y' }; 最好再加个字体 string[] font ={ "times new roman", "ms mincho", "book antiqua", "gungsuh", "pmingliu", "impact" }; random rnd = new random(); string chkcode = string.empty; //生成验证码字符串 for (int i = 0; i < 4; i++) { chkcode += character[rnd.next(character.length)]; } //写字符串 //画验证码字符串 for (int i = 0; i < chkcode.length; i++) { string fnt = font[rnd.next(font.length)]; font ft = new font(fnt, 18); color clr = color[rnd.next(color.length)]; g.drawstring(chkcode[i].tostring(), ft, new solidbrush(clr), (float)i * 20 + 8, (float)8); }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯