Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with component and function

Posted on 2014-09-23
7
Medium Priority
?
251 Views
Last Modified: 2014-09-23
Hello Guys,

I am really in doubts how to do this:

Please, take a look at my function

procedure ChangeWordColor(RE: TrichEdit; TheWord: String; Color: TColor; Style: TFontStyles);
var
  i, CharPos, noChars: Integer;
  cmp : TComponent;
begin
  CharPos := 0;
  noChars := 0;
  for i := 0 to Pred(cmp.Lines.Count) do
  Begin
    noChars := noChars + Length(RE.Lines[i]);
    CharPos := RE.FindText(TheWord, CharPos, noChars, [stWholeWord]);
    RE.SelStart := CharPos;
    RE.SelLength := Length(TheWord);

    if RE.SelAttributes.Color = clRed then
    Begin
      RE.SelAttributes.Color := color;
      RE.SelAttributes.Style := Style;
    End;
  End;
end;

Open in new window


I would like to turn this function able to use some kind of richedit that I have as component in my delphi, today, this
function only accept TrichEdit, but I have

* TsRichEdit
* TwwdbRichEdit
* TrxRichEdit
* TRichEdit

I thought to change the parameter to

ChangeWordColor(RE: Tcomponent ; TheWord: String; Color: TColor; Style: TFontStyles);

But I didn't how to go on inside the procedure.

I need a help so I can learn how to.

thanks
0
Comment
Question by:hidrau
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 40339076
Simply use Overloading to pass different RichEdit types...

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1          : TRichEdit;
    sRichEdit           : TsRichEdit;
    wwdbRichEdit1: TwwdbRichEdit
    rxRichEdit1       : TrxRichEdit1;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

 procedure ChangeWordColor(RE: TRichEdit; TheWord: String; Color: TColor; Style: TFontStyles);overload;
 procedure ChangeWordColor(RE: TsRichEdit; TheWord: String; Color: TColor; Style: TFontStyles);overload;
 procedure ChangeWordColor(RE: TrxRichEdit; TheWord: String; Color: TColor; Style: TFontStyles);overload;
 procedure ChangeWordColor(RE: TwwdbRichEdit; TheWord: String; Color: TColor; Style: TFontStyles);overload;

implementation


procedure ChangeWordColor(RE: TRichEdit; TheWord: String; Color: TColor; Style: TFontStyles);overload;
begin
   ...
end;

procedure ChangeWordColor(RE: TsRichEdit; TheWord: String; Color: TColor; Style: TFontStyles);overload;
begin
   ...
end;

procedure ChangeWordColor(RE: TrxRichEdit; TheWord: String; Color: TColor; Style: TFontStyles);overload;
begin
   ...
end;

procedure ChangeWordColor(RE: TwwdbRichEdit; TheWord: String; Color: TColor; Style: TFontStyles);overload;
begin
   ...
end;

Open in new window

0
 
LVL 1

Author Comment

by:hidrau
ID: 40339109
hummm,

I have thought about it but I think that was another way to do that where I could have only one function.

is there any other way? Something like:

if Re is TrichEdit them cmp := TRichEdit(re)

where I could have a variable where could assume the component that I am passing.

did you get it?
0
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 40339131
I think that this won't work because some richedit components (like RichView) are not inherited from original one. If they are (at least you use) - then it is possible.
Why not use only one TRichEdit component or one which suits you best?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:hidrau
ID: 40339158
I understood what you told me Sinisa, but all component that I use it seems to come from the same.

TsrichEdit comes from AlphaSkins component
TwwdbRichEdit comes from infopower component

Well, I only use two kind, TsrichEdit that inherited the original one and wwdbRichEdit that I am not sure now.

But to do something generic, how would it be? This is that I'd like to know that is, if it is possible.
You can give an example with any function and I adapt here on mine.
0
 
LVL 19

Expert Comment

by:Thommy
ID: 40339199
if all your RichEdit components inherit from TCustomRichEdit, then you can do as below:

Procedure ChangeWordColor2(RE:TCustomRichEdit;TheWord:string; TheColor: TColor; TheStyle: TFontStyles);
Var
FoundAt : integer;
len: integer;
start_pos: integer;
begin
    start_pos:=0;
    len:=RE.GetTextLen - Start_Pos;
    FoundAt:=RE.FindText(TheWord,start_pos,len,[stWholeWord]);
    while FoundAt <> -1 do
    begin
      RE.SelStart := FoundAt;
      RE.SelLength := Length(TheWord);
//      if RE.SelAttributes.Color = clRed then Begin
        RE.SelAttributes.Style := TheStyle;
        RE.SelAttributes.Color := TheColor;
        RE.SelText :=TheWord;
//      End;
      start_pos:=RE.SelStart+RE.SelLength;
      len:=RE.GetTextLen - Start_Pos;
      FoundAt:=RE.FindText(TheWord,start_pos,len,[]);
    end;
end;

Open in new window

0
 
LVL 19

Accepted Solution

by:
Thommy earned 2000 total points
ID: 40339281
I know that TrxRichEdit like TRichEdit derives from TCustomRichEdit.

So you can at least use TrxRichEdit and TRichEdit together with my above posted procedure passing a TCustomRichEdit and using the same Find/Replace coding...
0
 
LVL 1

Author Comment

by:hidrau
ID: 40339287
hummm, this is the secret TCustomRichEdit, I was doing wrong in my function parameter.

I got it Thommy, thanks a lot.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

879 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