Hi, I'm trying to work out a way i can create my own "find..." or "find next" dialog for a textbox, because i don't think Vb.net has one that you can use, and its vital that i manage to get this otherwise the program wont be much use when editing large documents and you don't want to spend half an hour finding a sentence or a word. Does anybody know of a way i can create my own using strings or maybe something else, i did think about using the split() method and i wouldn't know where to go after that, thanks.

If you are using richtextbox then it has its own find method. Just keep track where you started so you know when you went through the whole document. Keep track of where you currentlly are to start your next find.

It would make more sense having a richtext box, especially since it would be easy to highlight the words.

OK, how do i use the find method on the rtb, iv tried numerous ways but it doesn't seem to work, thanks.

Thats VB6... i tried the vb6 code upgrader but it didn't work.

Add a form to your project and call it frmFind or something like that (see attached image).
My richtextbox is called Editor.
Add this code.

Public Class frmFind
    Private PlaceHolder As Integer

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Dim x As Integer
        Dim opt As RichTextBoxFinds = 0
        If ckCaseSensitive.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
        If ckWholeWord.Checked Then opt = opt Or RichTextBoxFinds.WholeWord
        x = Form1.Editor.Find(txtFind.Text, PlaceHolder, opt)
        If x < 0 Then
            If MessageBox.Show("There is no more occurances of " & txtFind.Text & vbCrLf & "Start at begining?", "Can't Find", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                PlaceHolder = 0
                btnFind.PerformClick()
            End If
        End If
    End Sub

    Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
        Form1.Editor.SelectedText = txtReplace.Text
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindNext.Click
        PlaceHolder = Form1.Editor.SelectionStart + 1
        btnFind.PerformClick()
    End Sub

    Private Sub frmFind_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PlaceHolder = Form1.Editor.SelectionStart

    End Sub

    Private Sub btnReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceAll.Click
        Dim x As Integer
        Dim opt As RichTextBoxFinds = 0
        If ckCaseSensitive.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
        If ckWholeWord.Checked Then opt = opt Or RichTextBoxFinds.WholeWord
        PlaceHolder = 0
        Do
            x = Form1.Editor.Find(txtFind.Text, PlaceHolder, opt)
            If x < 0 Then
                Exit Do
            Else
                Form1.Editor.SelectedText = txtReplace.Text
            End If
            PlaceHolder = Form1.Editor.SelectionStart + 1
        Loop

    End Sub
End Class

and call it like this

frmFind.txtFind.Text = Editor.SelectedText
        frmFind.ShowDialog()
Comments
Working, Thank you
Attachments frmFind.gif 18.25 KB

Thats VB6... i tried the vb6 code upgrader but it didn't work.

Yeah sorry, I missed that. See the above example though, it looks good.

I have used the code above, but the richtextbox on the main screen does not reflect the change till I close the find screen. What do I need to change so it will.

The issue I get is that the original for does not highlight the text or scroll down to the highlighted text until I close the find dialog window.

I know how to do that I want a find like with internet explorer or notepad where it is only topmost for their applications not everything or if the find is showdialog it highlights the text. Pretty much how does notepad do it right.

This article has been dead for over six months. Start a new discussion instead.