Find and Replace dialogs

  • Having trouble converting the Find and Replace dialogs.from WPT5 to WPT9 as they have changed. The only demo I found was the FindText demo, but it was not much help.

    • In WPT5, FindDialog had multiple parameters to be able to pass in the TFindOptions, now it doesn't. The ReplaceDialog had TReplaceOptions and TotalCount, now it doesn't, but I need a total count.
    • The old event OnTextNotFound is now OnSearchReplaceMessage.

    I need to be able to find and/or replace text within multiple WPRichTexts from a database that get loaded into the same WPRichText in sequence (and saved as needed when using replace), and keep a total count of how many words were replaced.

    Basically the same logic I used in WPT5 is not working in WPT9, The only changes were commenting out those parameters previously used in WPT5, and using that other event.

    What is happening is that it works up until it finds a match, i.e. loops loading each WPRichText out of the database just fine. Once it finds a WPRichText with a match, it highlights it, but clicking on find next just displays its own "text not found" msg even if there are more matches in the current WPRichText and does not invoke the OnSearchReplaceMessage event in order for me to move on to the next WPRichText.

    It is as if the find dialog is no longer hooked to the recently loaded WPRichText. If I then click in the currently loaded WPRichText, it then continues correctly until it loads a new RichText, where it then highlights the first match and gets "unhooked" again somehow. I'm thinking I'm missing something when reloading the WPRichText during a find that is currently in progress.

    Sequence:

    1. Loads first/next WPRichText
    2. highlights the match if found else repeats step 1 because it invokes OnSearchReplaceMessage event
    3. click next
    4. displays its own "text not found", does not invoke the OnSearchReplaceMessage event
    5. I click inside the WPRichText
    6. Click next
    7. Highlights the match
    8. click next
    9. Highlights the next match if there is one, else goes to step 1 because it invokes OnSearchReplaceMessage event

    Thoughts?

    • Offizieller Beitrag

    WPTools 9.1 includes a new find dialog which also supports style replacement and can assign attributes.

    WPTools 5, which was last serviced on 14.12.2010 used the old standard dialogs.

    But the old handling is still there, see $DEFINE STANDARD_FINDDIALOG in WPCtrRich.pas and will be used if you do not use the new component on your form.

    For custom find operation as outlined above you need to use the "Finder", i.e. you want to loop through multiple database records. The standard dialog cannot do that for you since database navigation is required.

    It has a next function which locates text.

    If you replace text in a TDBWPRichText make sure to call Changing before you do so, because the database must be in edit mode.

    The not modal dialogs are ment to work with the active TWPRichText - that's the last editor which receives the focus by mouse click or a call to SetFocus.

  • Thanks, I will check out the new dialog.

    FYI, I do not use TDBWPRichText, just the normal TWPRichText. My app does not lend itself to be able to use a TDBWPRichText. Depending on what the user is doing, I load a specific record from the database and need to analyze it before loading the TWPRichText. So for spell checking all the pertinent database records, I navigate through a TreeView and pull the corresponding record via a class object. I won't go into any more detail, but using TDBWPRichText is not an option.

    So, when clicking next, and there are no more matches, I do the next treeview item that can be searched on and load that specific record. I need and event to tell me that no more matches exist in the current WPRichText where I can then move to the next treeview item and begin again, seamlessly. The treeview items are a random order of objects that relate to database objects, and the user needs to see where they are in relation to the treeview.

    I did use Finder in WPT5 and it worked well. For WPT9 I even tried setting focus in code on the newly loaded WPRichText, but had the issue I mentioned in my previous post where I had to manually focus the WPRichText to get it to find next.

    I will definitely try out the new dialog for both Find and Replace. I hope I also find how to keep a total count of the replacements as I was able to do in WPT5 (not real important, but can useful to the user).

    • Offizieller Beitrag
    Zitat

    I did use Finder in WPT5 and it worked well. For WPT9 I even tried setting focus in code on the newly loaded WPRichText, but had the issue I mentioned in my previous post where I had to manually focus the WPRichText to get it to find next.

    I don't understand. If you use the Finder the focus does not matter at all. It works on the TWPRichText which is the parent of the Finder object.

  • In WPT5 I did not need to use SetFocus. I was just trying to figure out why I have to manually focus the WPRichText in WPT9 using the standard dialog. Still haven't figured that out yet. The manual focus only works after the standard dialog itself (not your OnSerchReplaceMessage event) reports that it didn't find anything (even thought there is a match). After that dialog, I can click in the WPRichText and select find, and it works until the next load of the WPRichText.

    I tried your new dialog, and that issue is not present which is good, but have other questions/problems:

    Why does TWPSearchReplaceDialog always start with the Replace checkbox set even though my options are all cleared? I might want to find, not replace:

    WPSearchReplaceDlg1.Options := [];

    I ended up replacing my first few test texts with blank when I thought I was just doing a find. But was lucky it didn't save because the replace didn't invoke the OnChange event, which is a problem in itself.

    When doing a replace, the Count parameter in the OnSearchRepalceMessage is always 0. What is this Count? I can't find anything in the documentation, sorry.

    Is there a way to autofill the search box? For example, if my text cursor is on a word, I'd like to just do a CRTL+F and the dialog pops up with that word (WPRichText1.CPWord) in the dialog.

    One last thing for future consideration in your dialogs...

    As I've mention in the past, it would be nice if you allow the programmer to decide where the dialog should show up. Currently it always shows up in the top left corner, which is not nice on a 4K 40" monitor when my app is a relatively small form in the middle of the monitor. Like the old baseball reference "out in left field" in the literal sense. ;)

    • Offizieller Beitrag

    I don't understand why you not build your own Search and Replace dialog which works for your project as you need. My dialogs are more for a Word style program, not for one which works on multiple records.


    Use WPRichText.OpenDialog( wpdiaFind, '@SELECTED@' )

    or

    Use WPRichText.OpenDialog( wpdiaReplace, '@SELECTED@')

    to start the search and replace dialog with the selected text if you do not use the standard actions.

    The WPTools dialogs use functions FindTextMethod and ReplaceTextMethod which can be also called by custom made search&replace dialogs for more control.

  • I may have to. Just frustrating that the standard dialog and finder approach worked fine before in WPT5 but doesn't now and I cant figure out why. I just want this project of mine to be done. Arg.

    • Offizieller Beitrag

    I don't think it has to do with WPTools V5 vs V9 - there is an event interfering if the find dialog looses focus.

    The active WPRichText is provided by the global function

    function ActiveRTFEdit(p: TWPCustomRtfEdit): TWPCustomRtfEdit;

    which checks the global variable WPLastActiveRTFEdit if p is nil.

    WPLastActiveRTFEdit is set procedure TWPCustomRtfEdit.GetFocus;

    So if I would have a focus problem in a project I would place a break point in procedure TWPCustomRtfEdit.GetFocus and check when it is called while the search goes on.

    • Offizieller Beitrag
    Zitat

    What is happening is that it works up until it finds a match, i.e. loops loading each WPRichText out of the database just fine. Once it finds a WPRichText with a match, it highlights it, but clicking on find next just displays its own "text not found" msg even if there are more matches in the current WPRichText and does not invoke the OnSearchReplaceMessage event in order for me to move on to the next WPRichText.

    I assume the focus changes during loading and the OnSearchReplaceMessage is checked for a different TWPRichText (which does not have one). So this is very specific to your program. Using breakpoints it is usually quick to find the problem. You should assign the SearchReplaceMessage() to all instances.

    • Offizieller Beitrag
    Zitat

    One last thing for future consideration in your dialogs...

    As I've mention in the past, it would be nice if you allow the programmer to decide where the dialog should show up. Currently it always shows up in the top left corner, which is not nice on a 4K 40" monitor when my app is a relatively small form in the middle of the monitor. Like the old baseball reference "out in left field" in the literal sense. ;)

    There are a few dialogs which are non-modal. They are supposed to stay at their last position. This is why they don't use auto - screen center.

    You can easily modify their position (and other things) before they are showed by using the event OnShowDialog:

    Code
    procedure TForm.WPSearchReplaceDlg1ShowDialog(Sender: TObject);
    begin
       with Sender as TForm do
       begin
         Left := (Screen.Width-Width) div 2;
         Top := (Screen.Height-Height) div 2;
       end;
    end;