#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Basic text editor functionality


    Hello everybody. I desperately need some help. I'm building a simple text editor. I can't figure out how to count the characters, rows and characters in selected text in richedit field and show the info in new form. Can you help me? I'm using Delphi 2007. Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    156
    Rep Power
    3
    To get count of characters, use window message WM_GETTEXTLENGTH. Something like this:

    Code:
    function GetCountOfChars(ARichEdit: TRichEdit): Cardinal;
    begin
      Result := SendMessage(ARichEdit.Handle, 0, 0);
    end;
    Note that for this to work, the richedit must already has valid window handle.

    To get count of rows, use the richedit's Lines property. It got a Count property which explains how many lines the richedit has.

    To get count of selected characters, use the richedit's SelLength property.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    I'm not very good, can you show my an example. I know I have to use a variable to store the counting but I don't know how to do that. I suppose I need a loop. And in the other form Label#:= (my variable).

    P.S. I did it! Here is the code:
    procedure TForm1.Statistics1Click(Sender: TObject);
    begin
    Form2.Show;
    NumberOfChars:=length(RichEdit1.Text);
    NumberOfRows:=RichEdit1.Lines.Count;
    NumberOfSelChars:=RichEdit1.SelLength;
    Form2.Label5.Caption:=IntToStr(NumberOfChars);
    Form2.Label4.Caption:=IntToStr(NumberOfRows);
    Form2.Label6.Caption:=IntToStr(NumberOfSelChars);
    end;

IMN logo majestic logo threadwatch logo seochat tools logo