CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Guest

    searching for strings

    if I had a rich text box (richtextbox1), how would I make a FIND/REPLACE thing like in Word or Notepad?
    I tried instr but all it returns is the position of the string. I want to select it as well, and when the user clicks next, continue on...
    And how can I replace it?


  2. #2
    Join Date
    Jan 2000
    Location
    CA
    Posts
    52

    Re: searching for strings

    this is from the help file for selStart, selText, etc...



    private Sub Form_Load ()
    Text1.Text = "Two of the peak human experiences"
    Text1.Text = Text1.Text & " are good food and classical music."
    End Sub
    private Sub Form_Click ()
    Dim Search, Where ' Declare variables.
    ' get search string from user.
    Search = InputBox("Enter text to be found:")
    Where = InStr(Text1.Text, Search) ' Find string in text.
    If Where then ' If found,
    Text1.SetFocus
    Text1.SelStart = Where - 1 ' set selection start and
    Text1.SelLength = len(Search) ' set selection length.
    rem This is not in help file, but use it to
    rem set the new text into your box
    text1.seltext = ReplacementText
    else
    MsgBox "string not found." ' Notify user.
    End If
    End Sub





    Similar code will work for your rich text box.

    Note the use of the 'Where' variable. It stores the location that is returned from the Instr call.

    Good Luck...




  3. #3
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: searching for strings

    Here's an even easier way that uses the built-in find method of the RTF control :

    Place a RichTextBox and Button on your form :



    '
    private Sub Command1_Click()
    Dim lFoundAt as Long
    '
    lFoundAt = RichTextBox1.Find("a test", 1, len(RichTextBox1.Text))
    '
    If lFoundAt >= 0 then
    RichTextBox1.SelText = "a great! Test"
    End If
    '
    End Sub
    '
    private Sub Form_Load()
    RichTextBox1.Text = "This is a test"
    RichTextBox1.HideSelection = false
    '
    End Sub




    There are lots of different options you can use with the '.find' option - checkout the help that comes with VB or look in the MSDN.


    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured