Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I have a dilemma, I have my program functioning perfectly right now, all I have to do is take pre-existing information in my rich edit for example:

NOMBRE: #########

APPELIDOS: #########

EMAIL: #########

TELEFONO DE ESTUDIANTE: #########

TELEFONO DE #########: #########

TELEFONO DE #########: #########

NIVEL DE LENGUAJE: *********



Now what I want to do is Replace the '*********' with another string through the use of an update button and a Tcombobox, but I dont have heads or tails on how to do the code, please help me!!!

What I have tried:

Everything and google hasnt helped me.
Posted
Updated 23-Aug-16 11:30am
v2

1 solution

Insert ReplaceDialog component.
Add new ReplaceText event (Object Explorer -> Events bookmark)
Paste below code (for RichEdit component):
Delphi
procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
 
var
  SelPos: Integer;
begin
  with TReplaceDialog(Sender) do
  begin
    SelPos := Pos(FindText, RichEdit1.Lines.Text);
    if SelPos > 0 then
    begin
      RichEdit1.SelStart := SelPos - 1;
      RichEdit1.SelLength := Length(FindText);
      { Replace selected text with ReplaceText }
      RichEdit1.SelText := ReplaceText;
    end
    else MessageDlg(Concat('No more matches for: "', FindText, '" have been found in the current document'), mtError, [mbOk], 0);
 
  end;
 
end;


Now, insert new button and add OnClick event for it (as the same way you've added it previously) and paste below code:
Delphi
ReplaceDialog1.Execute;


And finally, add OnFind event to the ReplaceDialog

Delphi
procedure TForm1.ReplaceDialogFind(Sender: TObject);
var
  FoundAt: LongInt;
  StartPos, ToEnd: Integer;
begin
  with RichEdit1 do
  begin
{ if there's selected text, start looking from that place, in other case, start looking from the beginning }
    if SelLength <> 0 then
      StartPos := SelStart + SelLength
    else
      StartPos := 0;
 
  { find the end of text }
    ToEnd := Length(Text) - StartPos;
 
    FoundAt := FindText(ReplaceDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(ReplaceDialog1.FindText);
    end;
  end;
end;


That's all!

Another way:
How to search and replace text in a RichEdit - Delphi Tips - CJC Delphi[^]
 
Share this answer
 
v2
Comments
Crytach Daiguren 25-Aug-16 6:10am    
Ok I apologize for not being very clear, I have a Tcombobox that has preloaded options to update my rich edit by replacing a keyword in my rich edit, in this case for example: I select "C1 level" in my Tcombobox I click btnUpdate, in my rich edit "NIVEL DE LENGUAJE: *********" must be changed to "NIVEL DE LENGUAJE: C1 LEVEL"
Maciej Los 25-Aug-16 10:02am    
Well, you have to use FindText function in a while-loop to find a 'NIVEL DE LEGUAJE' text and replace ##### (hashes) with destination text. That's all! Foolow the link i've provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900