Link to home
Start Free TrialLog in
Avatar of --laser--
--laser--

asked on

find...replace...replace all...find next

hey everyone

im making a text editor program that will replace notepad...it has support for a lot more features than the original notepad

i have nearly everything done but the only thing i dont know how to do is make a find/replace form

i need it to be able to find text in a richtextbox and highlight it (for find)

then if the replace menu item is clicked i need it to bring up a replace form that will replace the text and put it back into the txtmain...i need optins for replace all and just replace one. i also need a find next option

i am very new to vb.net because i used to use vb6 and only just bought a copy of .net.

thanks --laser--
Avatar of arif_eqbal
arif_eqbal

If you are a bit familiar with vb6 the richTextBox programming is quite the same.

You have a find method so ues
RichTextBox1.Find("The String to Find")

in case of Replace use
RichTextBox1.SelectedText="New value"

For selection use
RichTextBox1.Select
The argument for this may be from the find method that returns the index of the occurance, and the length will be that of the string to find

For findNext you will need to set the StartIndex when using Find method

Avatar of --laser--

ASKER

i actually never used richtextbox in vb6 but it seems simple enough

how would i go about setting the startindex

and with the replace thing how can i set up a replace all?
Avatar of Mike Tomlinson
Here is how to do the replace all:

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        findAndReplace(TextBox1.Text, TextBox2.Text)
    End Sub

    Private Sub findAndReplace(ByVal find As String, ByVal replaceWith As String)
        Dim startAt As Integer
        While RichTextBox1.Find(find, RichTextBoxFinds.WholeWord) <> -1
            RichTextBox1.SelectedText = replaceWith
            startAt = startAt + replaceWith.Length
        End While
    End Sub
on form1 is the txtmain (richtextbox) and on a seperate form, FindAndReplace is the replace stuff...how do i access the rtb on form1 from findAndReplace form

in vb6 it was form1.txtmain.......

also how would i do the findnext option?
here is project written in C# for a text editor. Not sure if its gonna be able to help you but it has the basic functions of notepad.

http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=7252d02c-9eef-45a7-9984-dc0f3c47b779
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As for the find next feature, you need to simply store the value returned the the Find() function and then pass that value back in as the new start position as in the findAndReplace() example.

~IM
the startindex will be provided by the find method, find returns the index of start of the findstring, However Find automatically selects & highlights the text if found set the HideSelection property of the RichText to false

so say you want to find John in the richtextbox and get it Replaced by Laser
then

        RichTextBox1.Find("John", RichTextBoxFinds.MatchCase)
        RichTextBox1.SelectedText = "Laser"

Now for Replace All you need a loop

 Dim i As Integer
        Dim Ret As Integer
        Dim Ln As Integer = RichTextBox1.Text.Length - Len("John")
        For i = 0 To Ln
            Ret = RichTextBox1.Find("John", i, RichTextBoxFinds.MatchCase)
            If Ret >= 0 Then
                RichTextBox1.SelectedText = "Laser"
                i = Ret
            End If
        Next

ok so i have the find...replace and replace all working now how do i do the find next option...also would it be possible to add a match case checkbox and a search direction??
ok about the comment before...how do i get the value returned by the find() expresion so that i can set it as the start position for find next


is it possible to add a match case checkbox and a search direction dropdown?
also how do i display a message box if the string is not found and also a message box for how many replacements were made.
ok i got a case sensitive checkbox into it but still dont know how to do a search direction dropdown and the findnext option
right ive narrowed it down to one problem...just the find next problem


i got the case sensitive and also the reverse direction to work
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeh iv'e gotten that far already but when i set it to search in reverse it searches from the bottom and stays on the one string no matter how many times find next is pressed
ok i got rid of the search backwards feature because its too much trouble

i can do find next from the find and replace form but not from the original form that has the txtmain on it

how would i do find next using the text that is in textbox1 on FindAndReplace form from form1?
The form that is used for finding will be open for all the FindNext operation, then just have the Ret variable on form level in that form.

If you want that FindNext should work from the main form also say on a keypress even if the Find form is closed store the Ret value on the main form, and pass it whenever required to the FindAndReplace function on the Find Form

actually i already worked it out thanks anyway
i have this code for replace and it works fine until it gets to the last instance of the string found....if you click btnreplace again after you have replaced all the strings with btnreplace it just keeps inserting the replace text after the last string...why is this?
That's because the Ret value returned by the Find is -1 after it has found the last occurance. Now when you add 1 to it it becomes 0 so it does not come out of any loop that you are using and it keeps replacing wherever the cursor is

Debug and check it properly to simplify the thing you can use two variables like I used in the code I posted above
i changed it to this


    Private Sub findAndReplace(ByVal find As String, ByVal replaceWith As String)
        If F1.TxtMain.Find(find, RichTextBoxFinds.MatchCase.WholeWord) <> -1 Then
            If ChkCaseSensitive.Checked = True Then
                F1.TxtMain.Find(find, RichTextBoxFinds.MatchCase.WholeWord)
                F1.TxtMain.SelectedText = replaceWith
            Else
                F1.TxtMain.Find(find, RichTextBoxFinds.WholeWord)
                F1.TxtMain.SelectedText = replaceWith
            End If
        End If
    End Sub



and it works fine now


if you dont mind answering this...how do i use a linklabel to link to an email address so when it is clicked the user can send an email to the address??

thank you both so much for all the help!
nevermind i worked that out to...

thanks again

by the way i will split the points evenly because Idle_Mind any arif egbal you both have been great help
Handle the LinkClicked event of the Link Label

In the event handler you can use System.Diagnostics.Process.Start
to start the default mail client
or you can redirect to a page on your website wher  the user can fill in


yeh thanks but i alreadydid it... i just did like an execute thing