Solved

Help with component and function

Posted on 2014-09-23
7
217 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 26

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…

860 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