Using built in Find and Replace when the buttons are in the ribbon

SyntaxEditor for WPF Forum

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Version: 19.1.0687
Avatar

Hi,

I have an application with ribbon buttons and inside the documents I have SyntaxEditors. I want when I press on the Find or Replace button (in my ribbon bar), to see in the active syntax editor (which is inside the active document view) the built in Find or Replace dialog.

How can I connect that to the application commands ApplicationCommands.Find and ApplicationCommands.Replace?

Kind regards,

Daniel

Comments (19)

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Daniel,

We don't currently attach to those two particular ApplicationCommands but we can add command bindings to them for the next maintenance release (v22.1.2).  Prior to that version, you would need to add this code:

editor.CommandBindings.Add(EditorCommands.Find.CreateCommandBinding(ApplicationCommands.Find));
editor.CommandBindings.Add(EditorCommands.Replace.CreateCommandBinding(ApplicationCommands.Replace));

We use a similar technique to attach to clipboard commands like ApplicationCommands.Copy.


Actipro Software Support

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

Hi,

This is what I need.  Thank you !!

I have one small issue though. I have the syntax editor in a document window. When I go with the focus on a tool window my buttons still becomes disabled. I do want to have the find, replace ribbon buttons available.

Kind regards,

Daniel

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Daniel,

That is likely because routed commands only check for command handlers from the currently-focused element up the visual tree through to the root Window that hosts it.  If you have focus in a tool window, then focus is not within the SyntaxEditor in the document window, and thus the command handler there will not be used. 

In that kind of situation it might be better to use DelegateCommand (we have an implementation in our Shared Library) where you have programmatic control over everything.  Delegate commands don't rely on the visual tree like routed commands do, so they can avoid the issue you are encountering.  If you are new to delegate commands, there is a ton of information on them on the web if you search "wpf delegate command".  A delegate command execute action could then find the current primary document window, get its SyntaxEditor, and then fire the appropriate Find/Replace command on it programmatically.

I hope that helps!


Actipro Software Support

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

Hi,

I have already delegate commands in which I can get the active syntax editor. The problem is I do not know how to manually open the find/replace UI inside the syntax editor.

So then how can I "fire the appropriate Find/Replace command on it programmatically." ?

Kind regards,

Daniel

[Modified 2 years ago]

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

Hi,

I found in the documentation what I needed to do. (SyntaxEditor.ActiveView.OverlayPanes[OverlayPaneKeys.Search] as SearchOverlayPane;)

Kind regards,

Daniel

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Daniel,

You could also wrap our existing command like this:

editor.ActiveView.ExecuteEditAction(EditorCommands.Find);

Our command has some functionality in it such as updating the find text from the selection, setting the scope, etc.  Therefore, you might want to do this instead.


Actipro Software Support

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

Hi,

I would like to add some message boxes here. For example:

- when the string searched for in find or replace does not exist inside the document. Currently the UI control have a red border. On pressing the search button I would like to display a message that the text was not found. (Like in visual studio)

- when replacing some text on multiple lines I would like to show a message with the number of occurrences where that text was replaced.

Kind regards,

Daniel

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Daniel,

The SyntaxEditor.ViewSearch event (described in this documentation topic) provides an ISearchResultSet that will tell you everything like the ISearchOptions used, whether the search wrapped, and the results (if any).


Actipro Software Support

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

Can I somehow change the tooltips on the buttons for the find/replace control? I want to have support for changing the language inside my application.

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

All visible strings are standard .NET string resources in the SyntaxEditor assembly.  You can use our String Resource Browser in the Sample Browser app to see them.  They begin with "UIEditorSearchView".  That String Resource Browser also gives code that you can use to customize them.


Actipro Software Support

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

Can I find an example somewhere?

Kind regards,

Daniel

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

The String Resource Browser we mentioned outputs C# code you can use.  For example:

ActiproSoftware.Products.SyntaxEditor.SR.SetCustomString(
   ActiproSoftware.Products.SyntaxEditor.SRName.UIEditorSearchViewFindAllButtonText.ToString(), "Fin_d All");


Actipro Software Support

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

Thanks for the response.

It is working but I want to be able to change the language in runtime. There is some update method or something which I can call to force the update. Currently it seems that I need to restart my ap to see the new strings.

Kind regards,

Daniel

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Daniel,

Some resource strings are loaded in WPF templates or initialized as dependency property defaults.  For those kinds of scenarios, unfortunately an app restart is needed since they can't really be updated otherwise.


Actipro Software Support

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

Hi,

For my app changing the language in real time is important. There is support for creating a custom search/replace overlay control the same as the existing one so I can have the tooltip strings as custom dependency properties.

Kind regards,

Daniel

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

If this is not possible at the moment maybe you can create an enhancement on your side. I think that for all the strings wich appear in UI there should be a possibility to change them in runtime.

I do not think that only my app needs runtime changing of the language.

Kind regards,

Daniel

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Daniel,

We will log the suggestion of adding dependency properties for each string resource.

As a workaround for now, you can trigger the template to reapply.  This will update all the string resources in use, if you do this after updating the string resources:

var searchPane = editor.ActiveView.OverlayPanes[OverlayPaneKeys.Search] as SearchOverlayPane;
if (searchPane != null) {
	editor.ActiveView.OverlayPanes.Remove(OverlayPaneKeys.Search);
	editor.ActiveView.OverlayPanes.AddSearch(searchPane.IsReplaceVisible);
}


Actipro Software Support

Posted 2 years ago by Daniel Constantin - ModuleWorks GmbH
Avatar

Hi,

It worked. Thanks!

The only strings for which this does not work is the ones from the search scope. Those seems to not change.

Kind regards,

Daniel

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Daniel,

Thanks, we found the issue there and added a new public ApplyStringResources method for the next build.  After you change your string resources, you can call this to refresh everything, without the code snippet above needed.

(editor.ActiveView.OverlayPanes[OverlayPaneKeys.Search] as SearchOverlayPane)?.ApplyStringResources();

If you need to update the ComboBox strings in the meantime before that code update is out, you'll have to look into the SearchOverlayPane for the search scope ComboBox and then update the ComboBoxItem.Content values programmatically.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.