Hi everyone,

I've been working on a word processor called 'Pen and Paper', and I need some help with the Find/Replace Dialogs. I'm using Visual C++ 2008 Express, and I have a Richtextbox control (txtdisplay) including a main menu. I've finished making everything such as save/open, New Document, but I need some help with Find/Replace from the Edit menu. It opens up the 'Find' dialog, but I need some way, like in notepad, for the find button (btnfind), when clicked, to select the text in txtdisplay (the richtextbox on Form1) that the user entered in the Find dialog's textbox.

Same with the Replace dialog, except after finding the text, don't select it, but replace it with the text the user typed in the 'Replace' textbox in the dialog.

Thanks in advance to any of those who can help out.

Argh... sometimes I'm dismayed that Borland's C++ version of a control is wimpier than the Delphi version.

[edit]
I just re-read your post and realized that you said Visual C++... Alas. What follows assumes the Borland TRichEdit control. I don't know what it is in VC++, but the main functionality is in the Perform() function, which just calls the richedit's Window Procedure. You can use SendMessage() instead if you like, but calling the WndProc directly would be more efficient.

Alas, I've never done any serious Win32 programming with MSVC++... (having preferred Borland's tools), and it is late so if you are still stuck a day or two from now I'll try to look up an answer that better addresses your compiler.
[/edit]

The TRichEdit control inherits a method called Perform(). You can use this with the EM_FINDTEXTEX message.

bool FindTextEx(  // returns whether or not the text was found
  TRichEdit& red,
  const std::string& text,
  bool               is_search_forward,
  bool               is_match_case,
  bool               is_match_word
  ) {
  CHARRANGE  sel;
  FINDTEXTEX ftinfo;
  UINT       flags = 0;

  // Set the range to find from the cursor to EOF (or BOF)
  red.Perform( EM_EXGETSEL, 0, &sel );
  ftinfo.chrg.cpMin = sel.cpMax;
  ftinfo.chrg.cpMax = is_search_forward ? -1 : 0;

  // Text to find
  ftinfo.lpstrText = text.c_str();

  // Flags
  if (is_search_forward) flags += FR_DOWN;
  if (is_match_case)     flags += FR_MATCHCASE;
  if (is_match_word)     flags += FR_WHOLEWORD;

  // Find it
  if (red.Perform( EM_FINDTEXTEX, flags, (LPARAM)&ftinfo ) == -1)
    return false;  // (return = no more matches)

  // Update the selected text to the found position
  red.Perform( EM_EXSETSEL, 0, (LPARAM)&ftinfo.chrgText );

  red.Invalidate();
  return true;
  }

I haven't tested this! Dumb errors may have occurred.

The ability to search backward requires Microsoft Rich Edit 2.0 or later.

There are also some flags for dealing with Arabic and Hebrew text distinctions if you have MS Rich Edit 3.0. (See here)

Hope this helps.

Sorry Duaos, but it didn't compile. But still, I appreciate your time :) thanks for trying!

Sry, I should have named the post 'Visual C++ 2008 Word processor help...'

No, it won't compile with VC++. The above code is for Borland C++.

The important part is sending and receiving messages from the rich edit control with SendMessage().

Thanks, is there any way in Visual C++ 2008 to use this? an example perhaps? thanks

Yes, replace the argument red's type with a HWND handle to your Rich Edit control and all the lines with red.Perform(...) with a call to SendMessage(): if (SendMessage( red, EM_FINDTEXTEX, flags, (LPARAM)&ftinfo ) == -1) ... etc.
Sorry for the Borland stuff again.

I think Wiki_Tiki is using Windows Forms (.NET) . :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.