Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-07

how to get Type of Sender or Components[i] in order to Cast?

15 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Péter Gábor
to Bob B.

If you typecast something you can use the fields and properties of the type you use for casting, for example:

if TCheckBox(Sender).Checked then {do_something}  ;

or

if TCheckBox(Components[I]).Checked then {do_something}  ;

2015-07-27 00:31 keltez?ssel, Bob B. ?rta:

Show quoted text 5 lines
how to get Type of Sender or Components[i] in order to Cast? Thanks. Bob B.
P?ter G?bor

Reply by email

Péter Gábor
to Péter Gábor

Sorry!
I was "misreading" your mail... you want to know the type of them.

You can compare the type of them:

if Sender = TButton then { do something} ;

or you can get the name of the class

ShowMessage( Sender.classname );

2015-07-27 07:30 keltez?ssel, P?ter G?bor ?rta:

Show quoted text 17 lines
If you typecast something you can use the fields and properties of the type you use for casting, for example: if TCheckBox(Sender).Checked then {do_something} ; or if TCheckBox(Components[I]).Checked then {do_something} ; 2015-07-27 00:31 keltez?ssel, Bob B. ?rta: > how to get Type of Sender or Components[i] in order to Cast? > Thanks. > > Bob B. >
P?ter G?bor

Reply by email

Mark Morgan Lloyd
to Péter Gábor: Sorry! I was "misreading" your mail... you want to know the type of them. You can compare…
P?ter G?bor wrote 6 lines
Sorry! I was "misreading" your mail... you want to know the type of them. You can compare the type of them: if Sender = TButton then { do something} ;

Can this be elegantly put into a case statement?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

JuuS
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 10 lines
P?ter G?bor wrote: > Sorry! > I was "misreading" your mail... you want to know the type of them. > > You can compare the type of them: > > if Sender = TButton then { do something} ; Can this be elegantly put into a case statement?

Hi, I just tried it and no you can't. At least not directly, there may be a way of casting the Sender object to ordinal or string but I'm not sure that is efficient or elegant.

Why not simply iterate the control List:

for i := 0 to Controls.Count - 1 do
begin
  If controls[ i ] is TCheckbox then
    TCheckbox( Controls[ i ] ).Checked := ....
end;

Reply by email

Mark Morgan Lloyd
to JuuS
JuuS wrote 13 lines
On 07/27/2015 09:31 AM, Mark Morgan Lloyd wrote: > P?ter G?bor wrote: >> Sorry! >> I was "misreading" your mail... you want to know the type of them. >> >> You can compare the type of them: >> >> if Sender = TButton then { do something} ; > Can this be elegantly put into a case statement? > Hi, I just tried it and no you can't. At least not directly, there may be a way of casting the Sender object to ordinal or string but I'm not sure that is efficient or elegant.

I didn't think there was, but thought it worth asking. My use case is this sort of thing:

         if MainForm.OutputComponent is TListBox then
           with MainForm.ListBox1 do begin
             Font.Name := fontName;
             Font.Size := fontSize
           end;
         if MainForm.OutputComponent is TMemo then
           with MainForm.Memo1 do begin
             Font.Name := fontName;
             Font.Size := fontSize
           end;
         ...

Now obviously I could have a separate property holding an enumeration, but it's a pity that it's not possible to do something like

         case MainForm.OutputComponent of
           TListBox: begin end;
           TMemo:    begin end;
           ...
         otherwise
           // Fatal exception since this is a serious programming error
         end;
Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Michael Van Canneyt
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 40 lines
JuuS wrote: > On 07/27/2015 09:31 AM, Mark Morgan Lloyd wrote: >> P?ter G?bor wrote: >>> Sorry! >>> I was "misreading" your mail... you want to know the type of them. >>> >>> You can compare the type of them: >>> >>> if Sender = TButton then { do something} ; >> Can this be elegantly put into a case statement? >> > Hi, I just tried it and no you can't. At least not directly, there may > be a way of casting the Sender object to ordinal or string but I'm not > sure that is efficient or elegant. I didn't think there was, but thought it worth asking. My use case is this sort of thing: if MainForm.OutputComponent is TListBox then with MainForm.ListBox1 do begin Font.Name := fontName; Font.Size := fontSize end; if MainForm.OutputComponent is TMemo then with MainForm.Memo1 do begin Font.Name := fontName; Font.Size := fontSize end; ... Now obviously I could have a separate property holding an enumeration, but it's a pity that it's not possible to do something like case MainForm.OutputComponent of TListBox: begin end; TMemo: begin end; ... otherwise // Fatal exception since this is a serious programming error end;
You can:
   Case Lowercase(MainForm.OutputComponent.Classname) of
     'tlistbox' : begin end;
     'tmemo' : begin end;

Michael.

Reply by email

Mark Morgan Lloyd
to Péter Gábor
P?ter G?bor wrote 5 lines
If you use the classname property you can: case Sender.classname of 'TCheckBox': ShowMessage('This is a TCheckBox'); end;

Good point. I wonder if that's mode-specific (language manual simply says "Free Pascal allows").

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Flávio Etrusco
to Mark Morgan Lloyd: I didn't think there was, but thought it worth asking. My use case is this sort of thing:…
Mark Morgan Lloyd wrote 46 lines
JuuS wrote: > > On 07/27/2015 09:31 AM, Mark Morgan Lloyd wrote: >> >> P?ter G?bor wrote: >>> >>> Sorry! >>> I was "misreading" your mail... you want to know the type of them. >>> >>> You can compare the type of them: >>> >>> if Sender = TButton then { do something} ; >> >> Can this be elegantly put into a case statement? >> > Hi, I just tried it and no you can't. At least not directly, there may > be a way of casting the Sender object to ordinal or string but I'm not > sure that is efficient or elegant. I didn't think there was, but thought it worth asking. My use case is this sort of thing: if MainForm.OutputComponent is TListBox then with MainForm.ListBox1 do begin Font.Name := fontName; Font.Size := fontSize end; if MainForm.OutputComponent is TMemo then with MainForm.Memo1 do begin Font.Name := fontName; Font.Size := fontSize end; ... Now obviously I could have a separate property holding an enumeration, but it's a pity that it's not possible to do something like case MainForm.OutputComponent of TListBox: begin end; TMemo: begin end; ... otherwise // Fatal exception since this is a serious programming error end;

If all you need to do is to change the Font you can also just use a cracker class or use a bogus unchecked to e.g. TMemo (accompanied by informative comments ;-) , since Font is declared in TControl.

Regards,
Fl?vio

Reply by email

Sven Barth
to Mark Morgan Lloyd: Good point. I wonder if that's mode-specific (language manual simply says "Free Pascal…

Am 27.07.2015 12:06 schrieb "Mark Morgan Lloyd" <

Show quoted text 12 lines
: P?ter G?bor wrote: > > If you use the classname property you can: > > case Sender.classname of > 'TCheckBox': ShowMessage('This is a TCheckBox'); > end; Good point. I wonder if that's mode-specific (language manual simply says

"Free Pascal allows").

I'm not aware that it is mode specific. But it will work only with FPC ;)

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150727/032880b3/attachment-0001.html>

Reply by email

Sven Barth
to Mark Morgan Lloyd: I didn't think there was, but thought it worth asking. My use case is this sort of thing:…

Am 27.07.2015 11:37 schrieb "Mark Morgan Lloyd" <

: Now obviously I could have a separate property holding an enumeration,

but it's a pity that it's not possible to do something like

Show quoted text 8 lines
case MainForm.OutputComponent of TListBox: begin end; TMemo: begin end; ... otherwise // Fatal exception since this is a serious programming error end;

I had already thought about a TClass-case myself, but the first problem is whether the case labels should be strict ("x.ClassType = y") or loose ("x is y") [for Strings and Ordinals that is no problem]. And if the latter, what if the type is a TEdit, but you have labels for example for TCustomEdit and TControl? Also ranges should probably not be allowed (e.g. TControl..TEdit).

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150727/776f9f15/attachment.html>

Reply by email