SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Find/Replace In a TextEditor Control?

Last post 07-12-2008 10:10 PM by MattWard. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 07-07-2008 9:08 PM

    Find/Replace In a TextEditor Control?

    Hi All!

     I'm trying to implement Find/Replace Functionality in by application that uses The SharpDevelop textEditor Control as it's main editor. I Have Found the following code For Find:

    Dim StartPosition As Integer

    Dim SearchType As CompareMethod

    If chkMatchCase.Checked = True Then

    SearchType = CompareMethod.Binary

    Else

    SearchType = CompareMethod.Text

    End If

    StartPosition = InStr(1, frmMain.rtbDoc.Text, txtSearchTerm.Text, SearchType)

    If StartPosition = 0 Then

    MessageBox.Show("String: " & txtSearchTerm.Text.ToString() & " not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

    Exit Sub

    End If

    frmMain.rtbDoc.Select(StartPosition - 1, txtSearchTerm.Text.Length)

    frmMain.rtbDoc.ScrollToCaret()

    frmMain.Focus()

     

    and the following for replace:

    If frmMain.rtbDoc.SelectedText.Length <> 0 Then

    frmMain.rtbDoc.SelectedText = txtReplacementText.Text

    End If

    Dim StartPosition As Integer = frmMain.rtbDoc.SelectionStart + 2

    Dim SearchType As CompareMethod

    If chkMatchCase.Checked = True Then

    SearchType = CompareMethod.Binary

    Else

    SearchType = CompareMethod.Text

    End If

    StartPosition = InStr(StartPosition, frmMain.rtbDoc.Text, txtSearchTerm.Text, SearchType)

    If StartPosition = 0 Then

    MessageBox.Show("String: '" & txtSearchTerm.Text.ToString() & "' not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

    Exit Sub

    End If

    frmMain.rtbDoc.Select(StartPosition - 1, txtSearchTerm.Text.Length)

    frmMain.rtbDoc.ScrollToCaret()

    frmMain.Focus()

     

    However these only work on richTextBox Controls and not on a TextEditor Control, Can Somebody help me tweak this code so that it workd with #Develop's TextEditorControl?

     

    Thanks!

    SmileyWolf

    Filed under: , , ,
  • 07-07-2008 9:46 PM In reply to

    Re: Find/Replace In a TextEditor Control?

    For text and text length use:   textEditor.ActiveTextAreaControl.TextArea.Document;

    For selection use:  textEditor.ActiveTextAreaControl.TextArea.SelectionManager;

    For cursor location use: textEditor.ActiveTextAreaControl.TextArea.Caret;

    To move to a particular location use: textEditorControl.ActiveTextAreaControl.JumpTo

  • 07-08-2008 9:00 AM In reply to

    Re: Find/Replace In a TextEditor Control?

    Hi and Thanks!

     You have been most helpful MattWard, however i am still struggling with the lines/colums attrinbute of the caret in the TextEditorControl, how would I go to a specifc character in the TextEditorControl as opposed to the lines/columns method which may not be entirely suitable for a find/replace option.

     The SelectionManager is also confusing me, do  I Need to put something in brackets after SelectionCollection, and if so, what?

     

    Thanks Again!

     SmileyWolf

  • 07-08-2008 7:36 PM In reply to

    Re: Find/Replace In a TextEditor Control?

    The Caret has an Offset property which you can use to move to a particular character position. The IDocument interface has two helper methods to convert between offsets and positions (PositionToOffset and OffsetToPosition) if you need them.

    With the SelectionManager to get the current selection just do something like:

    if (selectionManager.HasSomethingSelected) {

        ISelection selection = selectionManager.SelectionCollection[0];

    }

  • 07-08-2008 10:06 PM In reply to

    Re: Find/Replace In a TextEditor Control?

    Hi and Thanks!

     Thanks for the response! It's really helped me in the development of my application, however (Me Being New To C#) How would i change the position using offset, offset is marked as 'readonly'?

     Also I need to set the current selection as well as get it, how would I do this?

    Thanks Again!

     

    SmileyWolf

  • 07-09-2008 8:11 AM In reply to

    Re: Find/Replace In a TextEditor Control?

    To change the position I would use JumpTo which will put the cursor where you want it and scroll the text editor. Alternatively you can modify the Caret's Position property but you need to convert the character position to a line/col position.

    To change the current selection use the SelectionManager's SetSelection. Again you will have to convert the character position to a line/col position.

     

  • 07-12-2008 3:36 PM In reply to

    Re: Find/Replace In a TextEditor Control?

    Hi and Thanks!

     Thanks for Your Help! Thanks to you I am really getting to grips with this control, my last problem, however, is how to use the Offset/Position conversion functions that you mentioned earlier, do i need to import a specific library?

     A small example would be greatly, greatly appreciated.

    Thanks Again

    SmileyWolf

  • 07-12-2008 10:10 PM In reply to

    Re: Find/Replace In a TextEditor Control?

    The conversion methods belong to the TextEditorControl's Document object.

            /// <summary>
            /// returns the logical line/column position from an offset
            /// </summary>
            TextLocation OffsetToPosition(int offset);
           
            /// <summary>
            /// returns the offset from a logical line/column position
            /// </summary>
            int PositionToOffset(TextLocation p);

Page 1 of 1 (8 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.