Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to check what type of component is the Sender Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Object Pascal
Language
Delphi 2.x
Views
158
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

The piece of code below is typical of some routines we have. This example only uses 
2 component types but sometimes there could be 7 or 8 and then the code is really 
bad.

1   procedure TEN1FORM.myonEnter(Sender: TObject);
2   begin
3     if Sender is TDBComboBox then
4       enterstr := TDBComboBox(Sender).text
5     else if Sender is TOVCDBPICTUREFIELD then
6       enterstr := TOVCdbPICTUREFIELD(Sender).text;
7   end;


Answer:

Solve 1:

8   function CheckIsType(AObject: TObject; const ClassArray: array of TClass): 
9   Integer;
10  {Return an index indicative of a type match on the object, and array of types 
11  passed}
12  begin
13    for Result := 0 to High(ClassArray) do
14      if AObject is ClassArray[Result] then
15        Exit;
16    Result := -1;
17  end;
18  
19  { ... }
20  case CheckIsType(Sender, [TdbComboBox, TOVCDBPctureField, TSpeedButton, TEdit]) of
21    0: { ... }; {TDBComboBox}
22    1: { ... }; {TOVCDBPctureField}
23    2: { ... }; {TSpeedButton}
24    3: { ... }; {TEdit}
25  end;
26  { ... }


I also use the following routine which does an exact type match:

27  function CheckType(AObject: TObject; const ClassArray: array of TClass): 
28  Integer;
29  var
30    Index: Integer;
31  begin
32    Result := -1;
33    if Assigned(AObject) then
34      for Index := 0 to High(ClassArray) do
35        if AObject.ClassType = ClassArray[Index] then
36        begin
37          Result := Index;
38          Exit;
39        end;
40  end;



Solve 2:

Let me add one more way to tackle this: use run-time type information. Text is a 
published property so RTTI exists for it. So you can use the stuff in Unit TypInfo 
to access it.

41  uses
42    TypInfo;
43  
44  function GetStrProperty(anObj: TObject; const propname: string): string;
45  var
46    PInfo: PPropInfo;
47  begin
48    result := EmptyStr;
49    PInfo := GetPropInfo(anObj.ClassInfo, propname);
50    if PInfo <> nil then
51      {found aproperty with this name, check if it has the correct type}
52      if PInfo^.Proptype^.Kind in [tkString, tkLString] then
53      begin
54        {it has! Get the string value from theproperty}
55        Result := GetStrProp(anObj, PInfo);
56      end;
57  end;
58  
59  Using this function your handler becomes
60  
61  procedure TEN1FORM.myonEnter(Sender: TObject);
62  begin
63    enterstr := GetStrProperty(sender, 'text');
64    { ... }


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC