Recent

Author Topic: [Solved] Waht is TLabel?? is it TWinControl?  (Read 5181 times)

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
[Solved] Waht is TLabel?? is it TWinControl?
« on: July 08, 2017, 01:40:04 pm »
Hi

i put a label and a button and a checkbox in form and write this code for all them :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. begin
  4.   if(ssRight in Shift)then begin
  5.     if Sender is TWinControl then
  6.       with Sender as TWinControl do begin
  7.         ShowMessage(Name);
  8.       end;
  9.   end;
  10. end;

when i right click on button or checkbox showmesage show  name of them

but when i right click on Label doesn't show ?
.
why?
.
waht is TLabel ?
.
please improve this code.

thank you
« Last Edit: July 08, 2017, 07:07:24 pm by majid.ebru »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Waht is TLabel?? is it TWinControl?
« Reply #1 on: July 08, 2017, 01:48:01 pm »
you know that is easy to find out right? just ctrl+click on the TLabel type in the declaration and it will take you to the class it self.

Well since I'm here there are 2 types of visual controls the wincontrols or the ones that have a hwnd handle from the underline widgetset or TGraphicControls the virtual controls which have no handle do not receive any information from the underline widgetset and they rely on the parent to feed them what ever information they need to function.
The first one is considered a 1st class citizen the second is lighter in memory and most times faster but they rely on the parrent to tell them what is going on around them.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Waht is TLabel?? is it TWinControl?
« Reply #2 on: July 08, 2017, 02:04:41 pm »
You could add this method to your form, and connect it to the OnMouseDown events of the label, button and form.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SharedMouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. begin
  4.   if (Button = mbRight) then begin
  5.     if (Sender is TLabel) then
  6.       ShowMessage(TLabel(Sender).Name)
  7.     else if (Sender is TButton) then
  8.       ShowMessage(TButton(Sender).Name)
  9.     else ShowMessage('You right-clicked but not on a label or button');
  10.   end;
  11. end;

Note that ssRight (used in the code you showed) has nothing to do with which mouse button is pressed.
« Last Edit: July 08, 2017, 02:09:00 pm by howardpc »

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
Re: Waht is TLabel?? is it TWinControl?
« Reply #3 on: July 08, 2017, 02:25:54 pm »
you know that is easy to find out right? just ctrl+click on the TLabel type in the declaration and it will take you to the class it self.

when i  ctrl+click on the TLabel(or TEdit or TCombobox or TButton or TCheckBox) ,the file StdCtrls opens
and
when i  ctrl+click on the TPanel ,the file ExtCtrls opens
 
i don't know why Tlabel and TCheckBox are in one file (they are in the file StdCtrls), but this code work on TCheckBox and doesn't work on TLabel?!?!?


You could add this method to your form, and connect it to the OnMouseDown events of the label, button and form.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SharedMouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. begin
  4.   if (Button = mbRight) then begin
  5.     if (Sender is TLabel) then
  6.       ShowMessage(TLabel(Sender).Name)
  7.     else if (Sender is TButton) then
  8.       ShowMessage(TButton(Sender).Name)
  9.     else ShowMessage('You right-clicked but not on a label or button');
  10.   end;
  11. end;

Note that ssRight (used in the code you showed) has nothing to do with which mouse button is pressed.

i know this code and i don't want to use this code(Thank you)


Note that ssRight (used in the code you showed) has nothing to do with which mouse button is pressed.

i don't know this ?!?!?!(Thank you)
« Last Edit: July 08, 2017, 02:31:22 pm by majid.ebru »

Thaddy

  • Hero Member
  • *****
  • Posts: 17448
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: Waht is TLabel?? is it TWinControl?
« Reply #4 on: July 08, 2017, 02:58:53 pm »
TLabel is a TWincontrol, but the WINAPI WndClass is STATIC_TEXT under Windows. So it is not meant to receive any messages.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Handoko

  • Hero Member
  • *****
  • Posts: 5458
  • My goal: build my own game engine using Lazarus
Re: Waht is TLabel?? is it TWinControl?
« Reply #5 on: July 08, 2017, 03:13:55 pm »
@majid.ebru

Using what taazz suggested, you will know TLabel is not inherited from TWinControl. Simply change your code form TWinControl -> TControl, it works. At least it worked on my Linux Gtk2.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. begin
  4.   if(ssRight in Shift)then begin
  5.     if Sender is TControl then
  6.       with Sender as TControl do begin
  7.         ShowMessage(Name);
  8.       end;
  9.   end;
  10. end;

Handoko

  • Hero Member
  • *****
  • Posts: 5458
  • My goal: build my own game engine using Lazarus
Re: Waht is TLabel?? is it TWinControl?
« Reply #6 on: July 08, 2017, 03:16:58 pm »
This page shows that TLabel is not inherited from TWinControl:

http://lazarus-ccr.sourceforge.net/docs/lcl/stdctrls/tlabel.html

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Waht is TLabel?? is it TWinControl?
« Reply #7 on: July 08, 2017, 04:01:26 pm »
you know that is easy to find out right? just ctrl+click on the TLabel type in the declaration and it will take you to the class it self.

when i  ctrl+click on the TLabel(or TEdit or TCombobox or TButton or TCheckBox) ,the file StdCtrls opens
and
when i  ctrl+click on the TPanel ,the file ExtCtrls opens
 
i don't know why Tlabel and TCheckBox are in one file (they are in the file StdCtrls), but this code work on TCheckBox and doesn't work on TLabel?!?!?

keep on walking down the hierarchy see where it leads. eg
Code: Pascal  [Select][+][-]
  1.                              TControl = class(TLCLComponent)
  2.   TGraphicControl = class(TControl)                   TWinControl = class(TControl)
  3.   ....
  4.   TCustomLabel = class(TGraphicControl)               TCustomEdit = class(TWinControl)
  5.   ....
  6.   TLabel = class(TCustomLabel)                        TEdit = class(TCustomEdit)
  7.   ....
  8.  
see a pattern here?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 17448
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: Waht is TLabel?? is it TWinControl?
« Reply #8 on: July 08, 2017, 04:59:24 pm »
This page shows that TLabel is not inherited from TWinControl:

http://lazarus-ccr.sourceforge.net/docs/lcl/stdctrls/tlabel.html

Yes indeed, I confused with TStaticText and KOL's TLabel.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
Re: Waht is TLabel?? is it TWinControl?
« Reply #9 on: July 08, 2017, 07:06:54 pm »
TWinControl -> TControl

Thank You @Handoko

it works.  :-* :-*

 

TinyPortal © 2005-2018