Solved

Help with component and function

Posted on 2014-09-23
7
231 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
Independent Software Vendors: 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

624 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