如何在TextBox中进行搜索? [英] How can I do a Search in TextBox?

查看:267
本文介绍了如何在TextBox中进行搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在TextBox中搜索和突出显示文本??
我不想使用RichTextBox.

How Can I search and highlight texts in TextBox???
I don''t wanna use RichTextBox.

推荐答案

Textbox没有突出显示格式,最好的方法是Select文本.
http://msdn.microsoft.com/en-us/library/hk09zy8f.aspx [ ^ ]
Textboxes do not have highlight formatting, the best you can do is Select text.
http://msdn.microsoft.com/en-us/library/hk09zy8f.aspx[^]


您可以使用以下代码段,该代码段通过单击以下按钮从TextBox2内的textBox1中查找文本:
You may use this snippet, which finds a text from textBox1 inside TextBox2 by clicking on a button:
private void button1_Click(object sender, EventArgs e)
{
   int idx = textBox2.Text.IndexOf(TextBox1.Text);
   if (idx >= 0)
   {
      textBox2.SelectionStart = idx;
      textBox2.SelectionLength = TextBox1.Text.Length;
      textBox2.Focus();
   }
}


这篇关于如何在TextBox中进行搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆