Frank59
Frank59

Reputation: 3261

Does AvalonEdit :TextEditor have quick search/replace functionality?

I use AvalonEdit:TextEditor. Can i enable quick search dialog (on Ctrl-F for example) for this control? Or maybe someone has code for search words into AvalonEdit:TextEditor text?

Upvotes: 18

Views: 9969

Answers (7)

Lei Chi
Lei Chi

Reputation: 268

Found an implementation on code project, look nice:

https://www.codeproject.com/Tips/768408/A-Find-and-Replace-Tool-for-AvalonEdit

and you can open text replace on demand.

<avalonedit:TextEditor SyntaxHighlighting="XML" x:Name="gameListXMLText"  KeyDown="gameListXMLText_KeyDown" />

private void gameListXMLText_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
    {
        //this.Close();
    }
    else if (e.Key == Key.H && Keyboard.Modifiers == ModifierKeys.Control)
    {
        FindReplaceDialog.ShowForReplace(gameListXMLText);
    }
}

Also you can keep your build-in find:

SearchPanel.Install(this.TextArea);

Upvotes: 0

Bloggrammer
Bloggrammer

Reputation: 1111

In my case, I couldn't find the Search.Install(...) method so I used the below code to add the search functionality.

textEditor.TextArea.DefaultInputHandler.NestedInputHandlers.Add(new SearchInputHandler(textEditor.TextArea));

The search box can be activated by pressing Ctrl + F on your keyboard.

Upvotes: 0

JWP
JWP

Reputation: 6963

For Avalon Edit Version 5.0.1.0 and up, just do this:

SearchPanel.Install(XTBAvalonEditor);

Where XTBAvalonEditor is the WPF AvalonEdit control name.

Make sure to add this using statement:

using ICSharpCode.AvalonEdit.Search;

Then when the editor has focus, press CTL-F: You'll see the find control pop up in upper right hand corner.

enter image description here

Upvotes: 27

Palle Jensen
Palle Jensen

Reputation: 131

In the TextEditor constructor in the ICSharpCode.AvalonEdit project, add SearchPanel.Install(this.TextArea); and voila, using ctrl+f opens the search window.

(using the line from Stephen McDaniel's post (replace myEditor with this) also works, but the support for SearchInputHandler is being removed)

(works well with AvalonEdit inside AvalonDock with MVVM)

From:

public TextEditor() : this(new TextArea())
{
}

To:

public TextEditor() : this(new TextArea())
{
  SearchPanel.Install(this.TextArea);
}

Upvotes: 13

Stephen McDaniel
Stephen McDaniel

Reputation: 2968

There isn't much documentation about it, but AvalonEdit does have a built in SearchPanel class that sounds exactly like what you want. There is even a SearchInputHandler class that makes it trivial to get it hooked up to your editor, responding to keyboard shortcuts, etc. Here is some sample code that attached the standard search logic to an editor:

myEditor.TextArea.DefaultInputHandler.NestedInputHandlers.Add(new SearchInputHandler(myEditor.TextArea));

Here is a screenshot of what it will look like (this is taken from ILSpy which uses AvalonEdit). You can see the search control in the top right, the search options it supports, and the automatic highlighting it does of matching results.

Searching in ILSpy with SearchPanel

There isn't any support for replace...but if you just need searching, this can be a great solution.

Upvotes: 29

Ievgen
Ievgen

Reputation: 4443

ICSharpCode.AvalonEdit 4.3.1.9429

Search and highlight item.

private int lastUsedIndex = 0;
        public void Find(string searchQuery)
        {
            if (string.IsNullOrEmpty(searchQuery))
            {
                lastUsedIndex = 0;
                return;
            }

            string editorText = this.textEditor.Text;

            if (string.IsNullOrEmpty(editorText))
            {
                lastUsedIndex = 0;
                return;
            }

            if (lastUsedIndex >= searchQuery.Count())
            {
                lastUsedIndex = 0; 
            }

            int nIndex = editorText.IndexOf(searchQuery, lastUsedIndex);
            if (nIndex != -1)
            {
                var area = this.textEditor.TextArea;
                this.textEditor.Select(nIndex, searchQuery.Length);
                lastUsedIndex=nIndex+searchQuery.Length;
            }
            else
            {
                lastUsedIndex=0;
            }
        }

Replace operation:

public void Replace(string s, string replacement, bool selectedonly)
        {
            int nIndex = -1;
            if(selectedonly)
            {
                nIndex = textEditor.Text.IndexOf(s, this.textEditor.SelectionStart, this.textEditor.SelectionLength);           
            }
            else
            {
                nIndex = textEditor.Text.IndexOf(s);
            }

            if (nIndex != -1)
            {
                this.textEditor.Document.Replace(nIndex, s.Length, replacement);


    this.textEditor.Select(nIndex, replacement.Length);
        }
        else
        {
            lastSearchIndex = 0;
            MessageBox.Show(Locale.ReplaceEndReached);
        }
    }

Upvotes: 5

The last time I checked it was a "No". You will have to implement your own search/replace functionality.

http://community.icsharpcode.net/forums/p/11536/31542.aspx#31542

You can quickly add find/replace from here - http://www.codeproject.com/Articles/173509/A-Universal-WPF-Find-Replace-Dialog

Upvotes: 4

Related Questions