Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2005.11.06;
Скачать: CL | DM;

Вниз

Возможно ли одной процедурой задать цвет объектам разных типов   Найти похожие ветки 

 
apellido   (2005-10-13 09:11) [0]

Хочу создать общую процедуру для присвоения одного и того
же свойства объектам разных типов. Никак не соображу как
этого добиться.


procedure AttrColor(pAttrValue: string; pType: string; Temp: TComponent);
var
 Tmp : TComponent;
begin
  if pType="TEdit"    then Tmp:=Temp as TEdit;
  if pType="Tlabel"   then Tmp:=Temp as TLabel;
if pAttrValue="CLBLACK"               then Tmp.Color := clBlack;
.............
end;

При компиляции сего кода выдается ошибка, что свойство
Color для объета Tmp неизвестно. Так как же быть?


 
ZeroDivide ©   (2005-10-13 09:15) [1]

http://delphimaster.net/view/1-1129118842/


 
Ega23 ©   (2005-10-13 09:16) [2]

procedure AttrColor(pAttrValue: string; pType: string; Temp: TComponent);
var
Tmp : TComponent;
begin
if pAttrValue="CLBLACK"               then Tmp.Color := clBlack;
.............
end;


 
Ega23 ©   (2005-10-13 09:17) [3]

Вру.

procedure AttrColor(pAttrValue: string; pType: string; Temp: TComponent);
begin
if pAttrValue="CLBLACK"               then Tеmp.Color := clBlack;
.............
end;


 
Reindeer Moss Eater ©   (2005-10-13 09:19) [4]

uses TypeInfo;

GetPropInfo

>then Tеmp.Color := clBlack;
У TComponent нет свойства Color


 
Ega23 ©   (2005-10-13 09:21) [5]

Опяь вру.

procedure AttrColor(pAttrValue: string; pType: string; Temp: TControl);
begin
if pAttrValue="CLBLACK"               then Tеmp.Color := clBlack;
.............
end;


 
Reindeer Moss Eater ©   (2005-10-13 09:27) [6]

Тупой вопрос:
зачем цвет передавать строкой?


 
apellido   (2005-10-13 09:34) [7]

Цвет задается строкой, так как он считывается с конфигурационного файла.

Попытки дать ответ Temp.Color := clBlack не верны, так как
у TComponent нет свойства Color.

Привильно задавать (Temp as TEdit).Color := clBlack;
                            (Temp as TLabel).Color := clBlack;
Проблема в том, как этого добиться одним кодом для Temp разных типов.


 
Ega23 ©   (2005-10-13 09:37) [8]

procedure AttrColor(pAttrValue: string; pType: string; Temp: TControl);
begin
if pAttrValue="CLBLACK"               then Tеmp.Color := clBlack;
.............
end;


 
Reindeer Moss Eater ©   (2005-10-13 09:41) [9]

С таким подходом проще создать Х процедур на каждую ветку ифа.
Будет не более коряво чем сейчас.


 
Reindeer Moss Eater ©   (2005-10-13 09:58) [10]

procedure SetColor(AObject : TObject; AColor : TColor);
var FPropInfo: PPropInfo;
begin
FPropInfo := GetPropInfo(AObject,"Color");
if (FPropInfo <> nil) and (FPropInfo^.PropType^.Kind = tkInteger) then
 SetOrdProp(AObject,FPropInfo,AColor);
end;


либо просто использовать процедуру из TypInfo:
procedure SetPropValue(Instance: TObject; const PropName: string;
 const Value: Variant);


 
apellido   (2005-10-13 11:02) [11]

Благодарю всех за участие! Проблема решена очень просто!

uses TypInfo;

procedure AttrColor(pAttrValue: string; Temp: TComponent);
begin

if pAttrValue="CLBLACK"   then
            SetOrdProp (Temp, "Color", Ord(clBlack));
.............
end;



Следует заметить, что одним параметром стало меньше и все работает!



Страницы: 1 вся ветка

Текущий архив: 2005.11.06;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.052 c
14-1129239730
ron
2005-10-14 01:42
2005.11.06
Independent Software Developers Forum (ISDEF)


4-1125602005
Vjacheslav
2005-09-01 23:13
2005.11.06
Библиотека winlite


3-1127461627
Маленький мук
2005-09-23 11:47
2005.11.06
Тригеры в MSSQL 2000


9-1119397693
!Trinix
2005-06-22 03:48
2005.11.06
Работа в закрытых помещениях.


14-1129021214
ANB
2005-10-11 13:00
2005.11.06
Чем отличается попса от рока ?