Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Tmemo,Trichedit, etc DELPHI 3......

Posted on 1998-09-07
10
249 Views
Last Modified: 2010-04-03
I want to show a Txt file in  differents colors and fonts.

ie: test 'monday' in black, text saturday in red and italic, etc

and at the same time if the user change the text and types theses texts in the component then show in theses color too?

any help with this components or any other will be very useful.



Thanks in advance

0
Comment
Question by:alaingated
10 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1338848
Try using TRichEdit with format capabilities...Its now late on my land so I do not want your question be unattended. So, search for the TRichEdit in help, but before that look at the demo in your Delphi\demos\richedit\ directory for an extended notepad application with delphi.
Bye Igor
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338849
Tell me your e-mail address and I'll send you a component that does that....

Regards,
Viktor Ivanov
0
 
LVL 4

Expert Comment

by:itamar
ID: 1338850
Also take a look at this:

" Rich edit control with extended capabilities. Extra formatting
    properties (highlight, indices), URL detection, multilevel undo/redo, support for Unicode strings.
    Requires RICHED20.DLL. (ver. 1.1, added 5/10/98, updated 5/31/98)  by Alexander
    Obukhov " at:

http://sunsite.icm.edu.pl/delphi/ftp/d30free/riched98.zip

IHTH

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 4

Accepted Solution

by:
erajoj earned 20 total points
ID: 1338851
Hi,
Is this what you want?:
(Lots of room for improvement below :)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    procedure RichEdit1Change(Sender: TObject);
    procedure RichEdit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
  private
    m_iFirstLine : Integer;
    m_rgsLookup  : TStringList;
    procedure SyntaxHighLight( iStart, iEnd: Integer );
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.SyntaxHighLight( iStart, iEnd: Integer );
var
  iIndex, iPos : Integer;
  iStartToken, iEndToken, iLastToken: Integer;
  sToken  : string;
  bInToken: Boolean;
begin
  with RichEdit1 do begin
    bInToken := False;
    iLastToken  := iStart; iStartToken := iStart; iEndToken   := iStart;
    for iIndex := iStart to iEnd do
    begin
      if ( IsCharAlphaNumeric( Text[ iIndex ] ) ) then begin
        if ( not bInToken ) then iStartToken := iIndex;
        iEndToken := iIndex;
        bInToken := True;
      end else begin
        if ( bInToken ) then begin
          sToken := Copy( Text, iStartToken, iEndToken - iStartToken + 1 );
          Caption := sToken;
          if ( m_rgsLookup.Find( sToken, iPos ) ) then begin
            SelStart  := iLastToken;
            SelLength := iStartToken - iLastToken;
            with SelAttributes do begin
              Color := clWindowText;
              Style := [  ];
            end;
            SelStart  := iStartToken - 1;
            SelLength := iEndToken - iStartToken + 1;
            with SelAttributes do begin
              Color := clRed;
              Style := [ fsItalic ];
            end;
            SelText   := m_rgsLookup[ iPos ];
            iLastToken := iEndToken + 1;
          end;
        end;
        bInToken := False;
      end;
    end;
    SelStart  := iLastToken;
    SelLength := iEnd - iLastToken;
    with SelAttributes do begin
      Color := clWindowText;
      Style := [  ];
    end;
  end;
end;

procedure TForm1.RichEdit1Change(Sender: TObject);
var
  iStart, iEnd, iMax, iSelStart, iSelLength: Integer;
begin
  with RichEdit1 do if Tag = 0 then begin
    Enabled := False; // reduces flicker
    Tag := 1;
    iSelStart  := SelStart;
    iSelLength := SelLength;

    iStart := iSelStart;
    iEnd   := iSelStart + iSelLength;
    iMax   := Length( Text );
    while ( iStart > 0 ) and ( not IsCharAlphaNumeric( Text[ iStart ] ) ) do Dec( iStart );
    while ( iStart > 0 ) and ( IsCharAlphaNumeric( Text[ iStart ] ) ) do Dec( iStart );
    while ( iEnd   <= iMax ) and ( not IsCharAlphaNumeric( Text[ iEnd ] ) ) do Inc( iEnd );
    while ( iEnd   <= iMax ) and ( IsCharAlphaNumeric( Text[ iEnd ] ) ) do Inc( iEnd );
    if ( iStart < iEnd ) then SyntaxHighLight( iStart, iEnd );

    SelStart  := iSelStart;
    SelLength := iSelLength;
    Tag := 0;
    Enabled := True;
    try SetFocus except end;
  end;
end;

procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
//  m_iFirstLine := SendMessage( RichEdit1.Handle, EM_LINEFROMCHAR, RichEdit1.SelStart, 0 );
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  m_rgsLookup  := TStringList.Create;
  with m_rgsLookup do begin
    Sorted := True;
    Add( 'Saturday' );
    Add( 'Sunday' );
  end;
end;

end.

/// John
0
 

Author Comment

by:alaingated
ID: 1338852
Thanks a lot,
 It works fine whe you type in, but it does not work properly loading a txt file
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1338853
You must use SyntaxHighLight after loading on the whole file:

procedure TForm1.LoadFile( sFileName: string );
var
  iStart, iEnd, iMax, iSelStart, iSelLength: Integer;
begin
  with RichEdit1 do if Tag = 0 then begin
    Enabled := False; // reduces flicker
    Tag := 1;
    Lines.LoadFromFile( sFileName );
    iSelStart  := SelStart;
    iSelLength := SelLength;

    iStart := 0;
    iEnd   := iSelStart;
    if ( iStart < iEnd ) then SyntaxHighLight( iStart, iEnd );

    SelStart  := iSelStart;
    SelLength := iSelLength;
    Tag := 0;
    Enabled := True;
    try SetFocus except end;
  end;
end;
 
.or something like that...

/// John
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1338854
Hi again,
The code above is painfully slow and doesn't work properly, so I felt I had to make at least some improvements to deserve the points. The new code is much faster:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, Menus;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Load1: TMenuItem;
    OpenDialog1: TOpenDialog;
    StatusBar1: TStatusBar;
    procedure RichEdit1Change(Sender: TObject);
    procedure RichEdit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure Load1Click(Sender: TObject);
  private
    m_iFirstLine : Integer;
    m_rgsLookup  : TStringList;
    procedure SyntaxHighLight( iStart, iEnd: Integer; bRemoveAttr: Boolean );
    procedure LoadFile( sFileName: string );
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.SyntaxHighLight( iStart, iEnd: Integer; bRemoveAttr: Boolean );
const
  ValidChars = [ '0'..'9','A'..'Z','a'..'z'];

var
  iIndex, iPos : Integer;
  iStartToken, iEndToken, iLastToken: Integer;
  sToken  : string;
  bInToken: Boolean;
  sText   : string;
begin
  with RichEdit1 do begin
    bInToken := False;
    iLastToken  := iStart; iStartToken := iStart; iEndToken   := iStart;
    sText := Text; // copy text once
    for iIndex := iStart to iEnd do
    begin
//      if ( IsCharAlphaNumeric( Text[ iIndex ] ) ) then begin
      if ( sText[ iIndex ] in ValidChars ) then begin
        if ( not bInToken ) then iStartToken := iIndex;
        iEndToken := iIndex;
        bInToken := True;
      end else begin
        if ( bInToken ) then begin
          sToken := Copy( sText, iStartToken, iEndToken - iStartToken + 1 );
          if ( m_rgsLookup.Find( sToken, iPos ) ) then begin
            if ( bRemoveAttr ) then begin
              SelStart  := iLastToken;
              SelLength := iStartToken - iLastToken + 1;
              with SelAttributes do begin
                Color := clWindowText;
                Style := [  ];
              end;
            end;
            SelStart  := iStartToken - 1;
            SelLength := iEndToken - iStartToken + 1;
            with SelAttributes do begin
              Color := clBLue;
              Style := [ fsBOLD ];
            end;
            SelText   := m_rgsLookup[ iPos ];
            iLastToken := iEndToken + 1;
          end;
        end;
        bInToken := False;
      end;
    end;
    SelStart  := iLastToken;
    SelLength := iEnd - iLastToken;
    with SelAttributes do begin
      Color := clWindowText;
      Style := [  ];
    end;
  end;
end;

procedure TForm1.RichEdit1Change(Sender: TObject);
var
  iStart, iEnd, iMax, iSelStart, iSelLength: Integer;
begin
  with RichEdit1 do if Tag = 0 then begin
    Enabled := False; // reduces flicker
    Tag := 1;
    iSelStart  := SelStart;
    iSelLength := SelLength;

    iStart := iSelStart;
    iEnd   := iSelStart + iSelLength;
    iMax   := Length( Text );
    while ( iStart > 0 ) and ( not IsCharAlphaNumeric( Text[ iStart ] ) ) do Dec( iStart );
    while ( iStart > 0 ) and ( IsCharAlphaNumeric( Text[ iStart ] ) ) do Dec( iStart );
    while ( iEnd   <= iMax ) and ( not IsCharAlphaNumeric( Text[ iEnd ] ) ) do Inc( iEnd );
    while ( iEnd   <= iMax ) and ( IsCharAlphaNumeric( Text[ iEnd ] ) ) do Inc( iEnd );
    if ( iStart < iEnd ) then SyntaxHighLight( iStart, iEnd, True );

    SelStart  := iSelStart;
    SelLength := iSelLength;
    Tag := 0;
    Enabled := True;
    try SetFocus except end;
  end;
end;

procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
//  m_iFirstLine := SendMessage( RichEdit1.Handle, EM_LINEFROMCHAR, RichEdit1.SelStart, 0 );
end;

procedure TForm1.LoadFile( sFileName: string );
var
  iStart, iEnd, iMax, iSelStart, iSelLength: Integer;
begin
  with RichEdit1 do if Tag = 0 then begin
    Screen.Cursor := crHourGlass;
    Enabled := False; // reduces flicker
    Tag := 1;
    Lines.LoadFromFile( sFileName );
    iSelStart  := SelStart;
    iSelLength := SelLength;

    iStart := 0;
    iEnd   := Length( Text );
    if ( iStart < iEnd ) then SyntaxHighLight( iStart, iEnd, False );

    SelStart  := iSelStart;
    SelLength := iSelLength;
    Tag := 0;
    Enabled := True;
    Screen.Cursor := crDefault;
    try SetFocus except end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  m_rgsLookup  := TStringList.Create;
  with m_rgsLookup do begin
    Sorted := True;
    Add( 'Saturday' );
    Add( 'Sunday' );
    Add( 'saturdays' );
    Add( 'sundays' );
  end;
end;

procedure TForm1.Load1Click(Sender: TObject);
begin
  if ( OpenDialog1.Execute ) then begin
    LoadFile( OpenDialog1.FileName );
  end;
end;

end.

/// John
0
 

Author Comment

by:alaingated
ID: 1338855
Hi! i just found that if i want to use not a word, but a frase ie " Sunday morning"," Saturday night", it does not work because the space character in.

any idea how can i sort this?

thanks again
0
 

Author Comment

by:alaingated
ID: 1338856
How can i do if i dont want to upgade the text  until i make all the SelAttributes. Because is my tokens are character then is very slow and the flicker  is too much, I really would like to avoid it.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1338857
Sorry Alain, I haven't got time to help you for a while.
Ask this as a new question with low points <50 so that others may help you.
Refer to this question and mention that I tried but am not able to help you any further.
If I find anything or discover something helpful to you, I will help you as much as I can. No promises though.

/// John

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question