Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
Am 27.07.2015 11:37 schrieb "Mark Morgan Lloyd" < markmll.laza...@telemetry.co.uk>: > > 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; 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 -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
Am 27.07.2015 12:06 schrieb "Mark Morgan Lloyd" < markmll.laza...@telemetry.co.uk>: > > 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 -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
2015-07-27 13:33 keltezéssel, Michael Schnell írta: > > I suppose you meant > > if Sender is TButton then { do something} ; > > -Michael > Yes! -- Péter Gábor p...@freemail.hu -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
On Mon, Jul 27, 2015 at 6:37 AM, Mark Morgan Lloyd wrote: > 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 -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
On 07/27/2015 07:47 AM, Péter Gábor wrote: if Sender = TButton then { do something} ; I suppose you meant if Sender is TButton then { do something} ; OP: If you want to deal with a lot of different classes that might be the sender, it might help to define an array of these classes (types) and search it. -Michael -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
Sorry about that - that message was supposed to be sent in reply to a message rhar came in right before one that was for this list. My Note 4 thought I was trying to reply a thread in the Lazarus group. -Chris On Jul 27, 2015 7:23 AM, Chris Kelling wrote: > > We have one in Room B. It is Ed Sax's old machine. It is an early model that > has the chicklett keyboard. About the only distinction tge PC Jr has is that > is was a finacial failure. Both style of keyboards are wireless, but you had > to have the keyboard aimed directly at the IR receiver on the PC. > > If it has the newer style keyboard, I say we take it. We can put both styles > on display to show the difference. Otherwise, we should pass. > > -Chris > > On Jul 27, 2015 6:06 AM, Mark Morgan Lloyd > wrote: > > > > 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"). > > > > -- > > Mark Morgan Lloyd > > markMLl .AT. telemetry.co .DOT. uk > > > > [Opinions above are the author's, not those of his employers or colleagues] > > > > -- > > ___ > > Lazarus mailing list > > Lazarus@lists.lazarus.freepascal.org > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > -- > ___ > Lazarus mailing list > Lazarus@lists.lazarus.freepascal.org > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
We have one in Room B. It is Ed Sax's old machine. It is an early model that has the chicklett keyboard. About the only distinction tge PC Jr has is that is was a finacial failure. Both style of keyboards are wireless, but you had to have the keyboard aimed directly at the IR receiver on the PC. If it has the newer style keyboard, I say we take it. We can put both styles on display to show the difference. Otherwise, we should pass. -Chris On Jul 27, 2015 6:06 AM, Mark Morgan Lloyd wrote: > > 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"). > > -- > Mark Morgan Lloyd > markMLl .AT. telemetry.co .DOT. uk > > [Opinions above are the author's, not those of his employers or colleagues] > > -- > ___ > Lazarus mailing list > Lazarus@lists.lazarus.freepascal.org > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
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"). -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
If you use the classname property you can: case Sender.classname of 'TCheckBox': ShowMessage('This is a TCheckBox'); end; 2015-07-27 09:31 keltezéssel, Mark Morgan Lloyd írta: > Péter Gábor wrote: >> >> if Sender = TButton then { do something} ; > > Can this be elegantly put into a case statement? > -- Péter Gábor p...@freemail.hu -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
On Mon, 27 Jul 2015, Mark Morgan Lloyd wrote: 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.-- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
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; -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
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. 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; -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
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? -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
Am 27.07.2015 07:48 schrieb "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} ; The correct operator is "is" not "=". Regards, Sven -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
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: > 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 p...@freemail.hu -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] how to get Type of Sender or Components[i] in order to Cast?
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 p...@freemail.hu -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
[Lazarus] how to get Type of Sender or Components[i] in order to Cast?
Thanks. Bob B. -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus