Solved

Help with component and function

Posted on 2014-09-23
7
200 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
Comment Utility
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
Comment Utility
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 25

Expert Comment

by:Sinisa Vuk
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:hidrau
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
hummm, this is the secret TCustomRichEdit, I was doing wrong in my function parameter.

I got it Thommy, thanks a lot.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now