Text Method doesn't work with Findclass(...) but with plain "TEdit" - delphi

I want to use findclass and findcomponent to be able to pass the sender component as parameter in a procedure.
Thank you for reading.
Edit: I use Delphi 2005
[Error]: E2003 Undeclared identifier: 'text'
TestMemo.Text := (FindComponent(VonKomponente.name) as
(Findclass(vonkomponente.ClassType.ClassName))).text; -> does not work
TestMemo.Text := (FindComponent(VonKomponente.name) as TEdit).text; -> works
procedure TFormTest.Edit7DblClick(Sender: TObject);
begin
MemoEdit((Sender as TComponent),'table','row');
end;
procedure TFormTest.MemoEdit(VonKomponente :TComponent;table,row : String);
begin
FormTestMemo.Max_Textlaenge := get_length(table,row);
FormTestMemo.Text := (FindComponent(VonKomponente.name) as
(Findclass(vonkomponente.ClassType.ClassName))).text;
If FormTestMemo.Showmodal = MrOk then
begin
...
end;
end;

What you are trying to do is not possible. You cannot pass a metaclass type determined at runtime to the as operator.
For what you are trying to do, you will have to resort to using old-style RTTI via the TypInfo unit, in this case the TypInfo.GetStrProp() function, eg:
uses
..., TypInfo;
FormTestMemo.Text := GetStrProp(VonKomponente, 'Text');
Note that not all text-based components have a Text property, some have a Caption property, eg:
uses
..., TypInfo;
var
prop: PPropInfo;
prop := GetPropInfo(VonKomponente, 'Text');
if prop = nil then
prop := GetPropInfo(VonKomponente, 'Caption');
if prop <> nil then
FormTestMemo.Text := GetStrProp(VonKomponente, prop)
else
FormTestMemo.Text := '';

Related

Get TextSettings.Font.Style property with GetObjectProp using Delphi Tokyo 10.2

I'm using Delphi's GetObjectProp function to get the properties of the form components, I get all the properties of several components, but I can not get the TextSettings.Font.Style (Bold, Italic, ...) property of components like TLabel for example. I need to know if the component text is bold or italic. The procedure I am working on trying to get these properties follows below:
procedure Tfrm1.aoClicarComponente(Sender: TObject);
var
TextSettings: TTextSettings;
Fonte: TFont;
Estilo: TFontStyle;
Componente_cc: TControl;
begin
Componente_cc := TControl(Label1);
if IsPublishedProp(Componente_cc, 'TextSettings') then
begin
TextSettings := GetObjectProp(Componente_cc, 'TextSettings') as TTextSettings;
if Assigned(TextSettings) then
Fonte := GetObjectProp(TextSettings, 'Font') as TFont;
if Assigned(Fonte) then
Estilo := GetObjectProp(Fonte, 'Style') as TFontStyle; // <-- error in this line
if Assigned(Estilo) then
Edit1.text := GetPropValue(Estilo, 'fsBold', true);
end
end;
The error displayed on the line where I marked above is.
[dcc64 Error] uPrincipal.pas(1350): E2015 Operator not applicable to this operand type
What am I doing wrong?
GetObjectProp(Fonte, 'Style') will not work since Style is not an object-based property to begin with, it is a Set-based property. And GetPropValue(Estilo, 'fsBold', true) is just plain wrong (not that you would get far enough to call it anyway), because fsBold is not a property, it is a member of the TFontStyle enum. To retreive the Style property value, you would have to use GetOrdProp(Fonte, 'Style'), GetSetProp(Fonte, 'Style'), or GetPropValue(Fonte, 'Style') instead (as an integer, string, or variant, respectively).
That being said, once you have retrieved the TextSettings object, you don't need to use RTTI at all to access its Font.Style property, just access the property directly.
Try this instead:
procedure Tfrm1.aoClicarComponente(Sender: TObject);
var
Componente_cc: TControl;
TextSettings: TTextSettings;
begin
Componente_cc := ...;
if IsPublishedProp(Componente_cc, 'TextSettings') then
begin
TextSettings := GetObjectProp(Componente_cc, 'TextSettings') as TTextSettings;
Edit1.Text := BoolToStr(TFontStyle.fsBold in TextSettings.Font.Style, true);
end;
end;
A better (and preferred) solution is to not use RTTI at all. FMX classes that have a TextSettings property also implement the ITextSettings interface for exactly this situation, eg:
procedure Tfrm1.aoClicarComponente(Sender: TObject);
var
Componente_cc: TControl;
Settings: ITextSettings;
begin
Componente_cc := ...;
if Supports(Componente_cc, ITextSettings, Settings) then
begin
Edit1.Text := BoolToStr(TFontStyle.fsBold in Settings.TextSettings.Font.Style, true);
end;
end;
Read Embarcadero's documentation for more details:
Setting Text Parameters in FireMonkey

How to Convert a TParams (TQuery) Object to TParameters (ADO) object.?

I am working on a legacy code which contains some TQuery components. I was trying to create a function which convert the TQuery Parameters into TParameters so that i can assign them into the Parameters property of an ADO Component (Like ADOQuery or ADODataSet).
I tried the following which i got from internet.
function ConvertToADOParms(Owner: TADODataset; aParams: TParams): TParameters;
var i: integer;
begin
// Convert a standard TParams object to an ADO-specific TParameters object
Result :=nil;
try
if aParams = nil then exit;
Result :=TParameters.create( Owner, TParameter);
for i:=0 to aParams.count - 1 do
begin
if aParams[i] = nil then continue;
with Result.AddParameter do
begin
Name := aParams[i].Name;
Datatype :=aParams[i].DataType;
Direction :=TParameterDirection(aParams[i].ParamType);
Size :=aParams[i].size;
Value :=aParams[i].value;
end;
end;
except
on e:exception do
begin
Result :=nil;
showmessage('Could not convert standard parameter object to ADO parameter object: '+e.message);
end;
end;
end;
But i am getting Invalid Class Typecast Error. When i debug the code i found that the error occurs at this function in ADODB unit
function TParameters.GetCommand: TADOCommand;
begin
Result := GetOwner as TADOCommand;
end;
Help Please. I am Using Delphi 5
I don't make much sense of the function prototype. It requests an owner for the collection that is returned by the function and as such should IMHO be independent.
I would simply get rid of that and operate directly with the passed ADO object. For example:
procedure FillParamsADO(Params: TParams; Dataset: TADODataset);
var
i: Integer;
begin
Dataset.Parameters.Clear;
for i := 0 to Params.Count-1 do
begin
with Dataset.Parameters.AddParameter do
begin
Name := Params[i].Name;
DataType := Params[i].DataType;
Direction := TParameterDirection(Params[i].ParamType);
Size := Params[i].Size;
Value := Params[i].Value;
end;
end;
end;

Is it possible to get components' method value (means name)?

I need to get a name of a component (TButton), that is being assigned in design-time and is seen in Object Inspector (such as Button1Click at Button1.OnClick event on events tab).
I use now TypInfo unit to get method's information via PPropInfo and I get OnClick and TNotifyEvent strings as values, but I didn't get the Button1Click as string value.
How can I get it?
string := MethodName(GetMethodProp(Button1, 'OnClick').Code);
Note that the method needs to be 'published'.
If the property and assigned method are both published, you can use this:
uses
TypInfo;
function GetEventHandlerName(Obj: TObject; const EventName: String): String;
var
m: TMethod;
begin
m := GetMethodProp(Obj, EventName);
if (m.Data <> nil) and (m.Code <> nil) then
Result := TObject(m.Data).MethodName(m.Code)
else
Result := '';
end;
s := GetEventHandlerName(Button1, 'OnClick');
The TypInfo unit (where GetMethodProp() comes from) only supports published properties.
You have to specify the object that owns the method address because TObject.MethodName() iterates the object's VMT. And the method must be published because TObject.MethodName() (which exists to facilitate DFM streaming) iterates a portion of the VMT that is filled only with the addresses of published methods.
If you are using Delphi 2010 or later, you can use Extended RTTI instead, which does not have the published limitations:
uses
Rtti;
function GetEventHandlerName(Obj: TObject; const EventName: String): String;
type
PMethod = ^TMethod;
var
ctx: TRttiContext;
v: TValue;
_type: TRttiType;
m: TMethod;
method: TRttiMethod;
s: string;
begin
Result := '';
ctx := TRttiContext.Create;
v := ctx.GetType(Obj.ClassType).GetProperty(EventName).GetValue(Obj);
if (v.Kind = tkMethod) and (not v.IsEmpty) then
begin
// v.AsType<TMethod>() raises an EInvalidCast exception
// and v.AsType<TNotifyEvent>() is not generic enough
// to handle any kind of event. Basically, the Generic
// parameter of AsType<T> must match the actual type
// that the event is declared as. You can use
// TValue.GetReferenceToRawData() to get a pointer to
// the underlying TMethod data...
m := PMethod(v.GetReferenceToRawData())^;
_type := ctx.GetType(TObject(m.Data).ClassType);
for method in _type.GetMethods do
begin
if method.CodeAddress = m.Code then
begin
Result := method.Name;
Exit;
end;
end;
end;
s := GetEventHandlerName(Button1, 'OnClick');

How to Set a Value to a Sub-Property Item using TypInfo RTTI Methods?

In my Question:
How to use “Sender” parameter with “As” operator for more then one class at a time
I choose the Remy Lebeau's Answer because it was the most dynamic tech for most situations like that. It uses the RTTI TypInfo Class.
But as I was using this class, another problem came:
How do we Set a sub-property value?
function TRemote.UpdateQuery(DataSet: TDataSet; SQL: String): Boolean;
var
PropInfo: PPropInfo;
begin
{ atualiza o código SQL padrão de um dataSet de consulta tipo View }
PropInfo := GetPropInfo(DataSet, 'SQL', []);
if not Assigned(PropInfo) then
begin
Result := False;
Exit;
end;
try
DataSet.Close;
SetPropValue(DataSet, PropInfo, SQL);
DataSet.Open;
Result := True;
except
Result := False;
end;
end;
Example:
I have a TIBQuery and I want to update the text of the SQL property.
But the SQL property is a TStrings class, so I have to use SQL.Text.
In the code above, It will raise an error "Invalid Property Type" because I got a TStrings and later I try to Set a normal String.
How to reach the SQL.Text using GetPropInfo?
Is there a common ancestor of TIBQuery and TZQuery that has the SQL property, so I can change to, instead of the TDataSet in the function parameter?
The TStrings.Text property is not accessible via RTTI in Delphi 2006. Even if it were, you do not need to use RTTI to access it anyway. Since you know the SQL property is a TStrings object, you can simply retreive the actual object pointer from the property and type-cast it to a TStrings pointer, then you can do whatever you need to do with that object, eg:
function TRemote.UpdateQuery(DataSet: TDataSet; SQL: String): Boolean;
var
PropInfo: PPropInfo;
SQLObj: TStrings;
begin
Result := False;
try
PropInfo := GetPropInfo(DataSet, 'SQL', [tkClass]);
if not Assigned(PropInfo) then Exit;
SQLObj := TStrings(GetObjectProp(DataSet, PropInfo, TStrings));
if not Assigned(SQLObj) then Exit;
DataSet.Close;
SQLObj.Text := SQL;
DataSet.Open;
Result := True;
except
end;
end;

How to use TValue.AsType<TNotifyEvent> properly?

I'm trying to use RTTI to add an event handler to a control, that may already have an event handler set. The code looks something like this:
var
prop: TRttiProperty;
val: TValue;
begin
prop := FContext.GetType(MyControl.ClassInfo).GetProperty('OnChange');
val := prop.GetValue(MyControl);
FOldOnChange := val.AsType<TNotifyEvent>;
prop.SetValue(MyControl, TValue.From<TNotifyEvent>(self.MyOnChange));
end;
I want this so I can do this in MyOnChange:
begin
if assigned(FOldOnChange) then
FOldOnChange(Sender);
//additional code here
end;
Unfortunately, the compiler doesn't seem to like the line FOldOnChange := val.AsType<TNotifyEvent>;. It says
E2010 Incompatible types: 'procedure,
untyped pointer or untyped parameter'
and 'TNotifyEvent'
Anyone know why that is or how to fix it? It looks right to me...
FOldOnChange is of a method pointer type, while AsType<TNotifyEvent> is a method. The compiler thinks you're trying to assign the method to the method pointer. The solution is to append () to the method call to force it, and use the return value of the method as the value to assign to FOldOnChange.
Here's a complete example:
uses SysUtils, Rtti;
type
TEv = procedure(Sender: TObject) of object;
TObj = class
private
FEv: TEv;
public
property Ev: TEv read FEv write FEv;
class procedure Meth(Sender: TObject);
end;
class procedure TObj.Meth(Sender: TObject);
begin
end;
procedure P;
var
ctx: TRttiContext;
t: TRttiType;
p: TRttiProperty;
v: TValue;
o: TObj;
e: TEv;
begin
t := ctx.GetType(TObj);
p := t.GetProperty('Ev');
o := TObj.Create;
try
// Set value explicitly
o.Ev := TObj.Meth;
// Get value via RTTI
v := p.GetValue(o);
//e := v.AsType<TEv>; // doesn't work
e := v.AsType<TEv>(); // works
finally
o.Free;
end;
end;
begin
try
P;
except
on e: Exception do
Writeln(e.Message);
end;
end.
The new RTTI introduced in 2010 is basically just an advanced wrapper around the older TypInfo RTTI (for now). In TypInfo, event handlers are represented by the TMethod record. Try this (untested):
var
prop: TRttiProperty;
val: TValue;
evt: TNotifyEvent;
begin
prop := FContext.GetType(MyControl.ClassInfo).GetProperty('OnChange');
val := prop.GetValue(MyControl);
TMethod(FOldOnChange) := val.AsType<TMethod>;
evt := Self.MyOnChange;
prop.SetValue(MyControl, TValue.From<TMethod>(TMethod(evt));
end;

Resources