Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with component and function

Posted on 2014-09-23
7
Medium Priority
?
240 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
[X]
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
  • 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 27

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
Technology Partners: 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

730 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