C#中如何用richTextBox进行查找内容,像电子英语字典一样

No4000 2007-10-03 10:04:23
如,当我输入a时点击"查找"按钮,richTextBox里显示的内容是与a相应的内容:a,at,add.............
谢谢
...全文
727 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
277894613 2007-10-04
  • 打赏
  • 举报
回复
richTextBox本身就支持Find,Copy,Cut,Plaster,Undo等各种常用操作
lalac 2007-10-03
  • 打赏
  • 举报
回复
RichTextBox.Find(String, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索字符串。

命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)

语法
Visual Basic(声明)
Public Function Find ( _
str As String, _
options As RichTextBoxFinds _
) As Integer

Visual Basic(用法)
Dim instance As RichTextBox
Dim str As String
Dim options As RichTextBoxFinds
Dim returnValue As Integer

returnValue = instance.Find(str, options)

C#
public int Find (
string str,
RichTextBoxFinds options
)


参数
str
要在控件中定位的文本。

options
RichTextBoxFinds 值的按位组合。



返回值
控件内找到搜索文本的位置。
备注
Find 方法搜索 str 参数中指定的文本,并返回第一个字符在控件内的位置。如果属性返回负值,则未在控件内容中找到所搜索的文本字符串。可以使用此方法创建可提供给控件用户的搜索功能。还可以使用此方法搜索要替换为特定格式的文本。例如,如果用户在控件中输入了日期,您便可以在使用控件的 SaveFile 方法之前用 Find 方法在文档中搜索所有日期并将它们替换为适当的格式。

使用此版本的 Find 方法,可以指定允许您扩大或缩小搜索范围的选项。可以指定使您得以匹配搜索单词的大小写或者搜索整个单词而不是部分单词的选项。通过在 options 参数中指定 RichTextBoxFinds.Reverse 枚举,可以在文档中由下至上对文本进行搜索,而不是采用由上至下的默认搜索方法。

注意
将 string 当作参数接受的 Find 方法无法找到包含在 RichTextBox 中多个文本行上的文本。执行这种搜索将返回值负一 (-1)。

示例
下面的代码示例在 RichTextBox 的整个内容中搜索传递到此方法文本参数中的搜索字符串的第一个实例。如果在 RichTextBox 中找到搜索字符串,此方法将返回 true 值并突出显示文本;否则返回 false。本示例还在搜索中指定匹配指定搜索字符串的大小写的选项。此示例要求此方法位于 Form 的类中,该窗体包含一个名为 richTextBox1 的 RichTextBox。


Public Function FindMyText(text As String) As Boolean
' Initialize the return value to false by default.
Dim returnValue As Boolean = False

' Ensure a search string has been specified.
If text.Length > 0 Then
' Obtain the location of the search string in richTextBox1.
Dim indexToText As Integer = richTextBox1.Find(text, RichTextBoxFinds.MatchCase)
' Determine if the text was found in richTextBox1.
If indexToText >= 0 Then
returnValue = True
End If
End If

Return returnValue
End Function



public bool FindMyText(string text)
{
// Initialize the return value to false by default.
bool returnValue = false;

// Ensure a search string has been specified.
if (text.Length > 0)
{
// Obtain the location of the search string in richTextBox1.
int indexToText = richTextBox1.Find(text, RichTextBoxFinds.MatchCase);
// Determine if the text was found in richTextBox1.
if(indexToText >= 0)
{
returnValue = true;
}
}

return returnValue;
}

111,101

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧