Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help manipulating a TDBRichEdit character by character 1

Status
Not open for further replies.

sjwales

MIS
Jun 24, 2003
61
US
I have a requirement to take the contents of a TDBRichEdit with some bolded text and drop it out to a text file with all bolded characters converted to uppercase.

So for example:

Code:
[b]This[/b] sentence needs to be [b]processed[/b]

Would need to look like this after processing:
Code:
THIS sentence needs to be PROCESSED

The code I'm trying to generate would work out how big the RichEdit is, processess it character by character and then copy each character to a TMemo or a string that I could then write out to a text file.

I'm sort of familiar with this in that I'd need to select one character from the RichEdit at a time and check for fsBold in the style properties and then act accordingly on that, but if someone would be able to give me a decent example on the selecting the characters one at a time, I'd greatly appreciate it.

Thanks
Steve

stephen.wales@riotinto.com
 
Steve,

The properties of the TRichEdit that you need to look into are SelStart (used to determine/set the cursor position), SelLength (used to determine/set the length in characters of a selection) and SelAttributes (used to determine/set the associated attributes of the selection e.g. bold style).

You can find these explained in the Delphi help files.

Hope this helps!

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I tried doing something along the lines of setting SelLength := 1 and SelStart := 0 and then slowly adding 1 to SelStart and for each character, processing SelText.

Works fine for the first line.

At the end of the first line, I can keep adding 1 to SelStart all I want but it won't move the focus to the start of the second or subsequent lines.

I tested this further by placing a TRichEdit, a TButton and a TLabel on a form. I entered 3 lines of text into the TRichEdit. I clicked on the first character in the first line of the text and then started clicking the button.

SelStart increases each click. Until I get to the end of the line. SelStart continues increasing but the focus remains at the end of the first line. If I manually click at the start of the second line, SelStart is correctly recalculated an the process continues until the end of line 2. I can't work out how to programmatically advance the focus to the next line after it finishes processing a line.

From the test program described in the above paragraph:

Code:
procedure TForm1.RichEdit1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  i : integer;
begin
  i := RichEdit1.SelStart;
  Label1.Caption := IntToStr(i);
end;

procedure TForm1.RichEdit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  i : integer;
begin
  i := RichEdit1.SelStart;
  Label1.Caption := IntToStr(i);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  i,j : integer;
begin
  j := RichEdit1.SelStart + 1;
  RichEdit1.SelStart := j;
  RichEdit1.SetFocus;
  i := RichEdit1.SelStart;
  Label1.Caption := IntToStr(i);
end;

This code let me see the value of RichEdit1.SelStart based upon where ever I clicked and whereever I tried to programmatically set it....

Any more ideas?

Steve

stephen.wales@riotinto.com
 
Steve,

Change your button click code to this:
Code:
procedure TForm1.Button3Click(Sender: TObject);
var
  OriginalPos, Row: integer;
begin
  OriginalPos := RichEdit1.SelStart;
  RichEdit1.SelStart := RichEdit1.SelStart + 1;
  //checks if caret has stopped moving
  if RichEdit1.SelStart = OriginalPos then
  begin
    // get current row number
    Row := SendMessage(RichEdit1.Handle, EM_LINEFROMCHAR, RichEdit1.SelStart, 0);
    // move to next row
    RichEdit1.SelStart := SendMessage(RichEdit1.Handle, EM_LINEINDEX, Row + 1, 0);
  end;
  RichEdit1.SetFocus;
end;

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Very nice. Thank you.

Enjoy your star :)

stephen.wales@riotinto.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top