搜索表单中的富文本框 [英] Search Form for a Rich Text Box

查看:131
本文介绍了搜索表单中的富文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,代码社区,这是我第一次发布.我希望这是有道理的.
我正在使用Visual Studio2005.

我正在研究一种基本上加载数据文件的表单,并且允许用户以多种方式编辑数据.我在将SEARCH TEXT工具添加到应用程序时遇到问题.

我有一种叫做frmMain的表格.它包含一个大的Rich Text Box.我创建了用于打开和关闭数据文件的按钮(其中大多数是普通的txt文件).我有一个名为btnSearch的按钮.单击后,将弹出一个名为frmSearch的新表单.该表单具有一个文本框,供用户输入他们想要搜索的文本.然后,当他们按下搜索"按钮时,应该在加载到frmMain中的文件中搜索输入的文本并突出显示它.很像Microsoft Word或Firefox或IE中的搜索功能.

我的问题是,当搜索发生时,文本的第一个实例被突出显示,但是如果文件中有更多的文本,则屏幕或页面不会向下移动,以便用户查看其他实例.如果有5个JONES文本实例,则每次单击搜索按钮都将找到全部5个实例,但是用户只能看到屏幕上已有的实例.

我如何做到这一点,以便在下面的页面中有文本实例的情况下,页面会根据找到的文本实例进行移动.

我在frmSearch上的代码:

Hello Code Community, This is my first time posting. I hope this makes sense.
I am using Visual Studio 2005.

I am working on a form that basically loads file of data and the user is allowed to edit the data in many ways. I am having trouble adding SEARCH TEXT tool to the application.

I have one form called frmMain. It contains a big Rich Text Box. I have created buttons to open and close data files (most of them are normal txt files). I have a button named btnSearch. When clicked a new form named frmSearch pops up. This form has a textbox for the user to enter the text they wish to search for. Then when they press the Search button, it is supposed to search the file loaded into frmMain for the entered text and highlight it. Much like Microsoft Word or a search function in Firefox or IE.

My problem is that when the search takes place the first instance of the text is highlighted, but if there are more of the texts in the file, the screen or page does not move down for the user to see the other instances. If there are 5 instances of the text JONES, it will find all 5 on each click of the search button, but the user can only see the ones that are already on the screen.

How can I make it so that if there are instances of the text in pages below, the page move according to the instance of the text that is found.

MY CODE on frmSearch:

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        If String.IsNullOrEmpty(txtSearch.Text) Then
            MessageBox.Show("No text to search")
        Else
            SearchFunction(frmMain.rtb.Text.ToLower, txtSearch.Text.ToLower)
        End If
    End Sub

    Dim StartIndex As Integer
    Private Sub SearchFunction(ByVal Text As String, ByVal SearhText As String)
        StartIndex = Text.IndexOf(SearhText, StartIndex + SearhText.Length)

        If StartIndex <> -1 Then
            frmMain.rtb.Select(StartIndex, SearhText.Length)
            frmMain.rtb.Select()
        Else
            MessageBox.Show("Designer has finished searching the data")
            StartIndex = 0
        End If
    End Sub
End Class



我再次希望这对社区有意义,如果问题不清楚,我深表歉意.我试图尽可能地解释它.任何帮助或意见,我们将不胜感激.谢谢.



Again I hope this makes sense to the community, I apologize if the problems isn''t clear. I tried to explain it as best as possible. Any help or comments is much appreciated. Thank You.

推荐答案

您正在使用WPF还是Windows窗体?如果是Windows窗体,则RichTextBox类中有一个方法继承了TextBoxBase,可能会起作用:
ScrollToCaret().

看看吧.
Are you using WPF, or Windows Forms? If it is Windows Forms, there is a method in the RichTextBox class, inherited from TextBoxBase, that might do the trick:
ScrollToCaret().

Take a look at it.


这篇关于搜索表单中的富文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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