Recent

Author Topic: [SOLVED] TCButton Array "Down" Property Not Working  (Read 2141 times)

pixelink

  • Hero Member
  • *****
  • Posts: 1260
[SOLVED] TCButton Array "Down" Property Not Working
« on: May 02, 2019, 08:19:06 pm »
Okay... I have and example project.

I was originally using TSpeedButton to dynamically create a button array.However, there was no Hover Font Color.
LAINZ suggested using the TCButton, I love it! So customize-able!

However, the SpeedButton had the "Active" state that just automatically worked.
BUT, TCButton doesn't have that. The only property close to that is the "DOWN" state

And I can trigger that when the button is click. That works
However, if you click on multiple buttons they "go down" but previous ones that I click don't go back up.

So, the only way I know in theory is to loop through all the dynamically created buttons and set DOWN:=FALSE to clear everything.
And then only one button appears down.

Got it so far?

THE PROBLEM....

When i loop through the array to try an set all the buttons DOWN property to FALSE
I get errors saying the identifier is not known (or similar)

And what I don't understand is that

Item.Caption:='Button' + IntToStr(i); - works fine - "Caption" is exposed to the array

BUT

Item.Down:=false;  DOESN'T WORK. - Error: identifier idents no member "Down"

Why?

CODE JUST FOR TRYING TO RESET DOWN PROPERTY

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.Button2Click(Sender: TObject);
  3.  
  4. // LOOP THROUGH BUTTONS - CHANGE DOWN TO FALSE
  5.  
  6.   var
  7.   i: Integer;
  8.   Item: TControl;
  9. begin
  10.   for i := (Panel1.ControlCount - 1) downto 0 do
  11.   begin
  12.     Item := Panel1.controls[i];
  13.     Item.Caption:='Button' + IntToStr(i);
  14.     Item.Down:=false;  - //DON'T WORK
  15.     //TBCButton.Down:=false; - DON'T WORK
  16.     //TBCButton(i).Down:=false; - DON'T WORK
  17.  
  18.   end;
  19.  
  20. end;
  21.  
  22.  

All other code works.
BUT if you want a project to see and try for yourself

Download from here:
https://www.pixelink.media/downloads/BtnArrays.zip


Thanks in advance
« Last Edit: May 02, 2019, 09:46:22 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TCButton Array "Down" Property Not Working
« Reply #1 on: May 02, 2019, 08:28:27 pm »
Here is a screen shot of what i don't want to happen.

ONLY 1 button should be pressed down.
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TCButton Array "Down" Property Not Working
« Reply #2 on: May 02, 2019, 09:17:30 pm »
What I don't get is that "Caption" is exposed as a Property, while "Down" is not in the code intellisense drop-down.

YET... "DOWN" is a property in the object inspector.

So, how do I get around something that is missing, or I am I even going to able to.

see screenshot

« Last Edit: May 02, 2019, 09:21:07 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TCButton Array "Down" Property Not Working
« Reply #3 on: May 02, 2019, 09:29:59 pm »
Just so everyone knows.

Using TCButton1.Down:=False - directly works perfectly fine if you drag the control unto a form.
The property "DOWN" is there.

But, since I created the buttons dynamically, the "Item" (in my array loop) is not including the the "DOWN" property an an identifier.

I know the differences going on here.




« Last Edit: May 02, 2019, 09:32:11 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Josh

  • Hero Member
  • *****
  • Posts: 1363
Re: TCButton Array "Down" Property Not Working
« Reply #4 on: May 02, 2019, 09:31:28 pm »
Hi

The Down property is for the TBCButton.
TControl does not have this property.

You can access the property like

(item as TBCButton).Down:=

so a quick change of your code may help; I have not tested though so may contain bug(s)
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2.  
  3. // LOOP THROUGH BUTTONS - CHANGE DOWN TO FALSE
  4.  
  5.   var
  6.   i: Integer;
  7.   Item: TControl;
  8. begin
  9.   for i := (Panel1.ControlCount - 1) downto 0 do
  10.   begin
  11.     Item := Panel1.controls[i];
  12.     If Item is TBCButton then  // Added check in case panel has other controls
  13.     Begin
  14.        (item as TBCButton).Caption:='Button' + IntToStr(i);
  15.        (item as TBCButton).Down:=false;  - //DON'T WORK
  16.        //TBCButton.Down:=false; - DON'T WORK
  17.        //TBCButton(i).Down:=false; - DON'T WORK
  18.     end;
  19.   end;
  20. end;
  21.  

Not you could use a with cause instead, my personal preference is casting as above that way I know exactly what is happening
ie
with (item as tbcbutton) do
begin
  down:=false;
  caption:='blah
.....
end;
« Last Edit: May 02, 2019, 09:34:14 pm by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TCButton Array "Down" Property Not Working
« Reply #5 on: May 02, 2019, 09:33:36 pm »
Hi

The Down property is for the TBCButton.
TControl does not have this property.

You can access the property like

(item as TBCButton).Down:=

so a quick change of your code may help; I have not tested though so may contain bug(s)
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2.  
  3. // LOOP THROUGH BUTTONS - CHANGE DOWN TO FALSE
  4.  
  5.   var
  6.   i: Integer;
  7.   Item: TControl;
  8. begin
  9.   for i := (Panel1.ControlCount - 1) downto 0 do
  10.   begin
  11.     Item := Panel1.controls[i];
  12.     If Item is TBCButton then  // Added check in case panel has other controls
  13.     Begin
  14.        (item as TBCButton).Caption:='Button' + IntToStr(i);
  15.        (item as TBCButton).Down:=false;  - //DON'T WORK
  16.        //TBCButton.Down:=false; - DON'T WORK
  17.        //TBCButton(i).Down:=false; - DON'T WORK
  18.     end;
  19.   end;
  20. end;
  21.  

Well, that is weird, Drag a TCButton unto a form. It most certainly has a "DOWN" property... it works for me.
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Josh

  • Hero Member
  • *****
  • Posts: 1363
Re: TCButton Array "Down" Property Not Working
« Reply #6 on: May 02, 2019, 09:36:10 pm »
TControl is the base control,
TBCButton is a customized control that has additional properties like down, Hover, Gradients, etc etc etc, these are not part of the base TCOntrol.
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TCButton Array "Down" Property Not Working
« Reply #7 on: May 02, 2019, 09:36:21 pm »
Hi

The Down property is for the TBCButton.
TControl does not have this property.

You can access the property like

(item as TBCButton).Down:=

so a quick change of your code may help; I have not tested though so may contain bug(s)
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2.  
  3. // LOOP THROUGH BUTTONS - CHANGE DOWN TO FALSE
  4.  
  5.   var
  6.   i: Integer;
  7.   Item: TControl;
  8. begin
  9.   for i := (Panel1.ControlCount - 1) downto 0 do
  10.   begin
  11.     Item := Panel1.controls[i];
  12.     If Item is TBCButton then  // Added check in case panel has other controls
  13.     Begin
  14.        (item as TBCButton).Caption:='Button' + IntToStr(i);
  15.        (item as TBCButton).Down:=false;  - //DON'T WORK
  16.        //TBCButton.Down:=false; - DON'T WORK
  17.        //TBCButton(i).Down:=false; - DON'T WORK
  18.     end;
  19.   end;
  20. end;
  21.  

Not you could use a with cause instead, my personal preference is casting as above that way I know exactly what is happening
ie
with (item as tbcbutton) do
begin
  down:=false;
  caption:='blah
.....
end;

PERFECT - IT WORKS!

Code: Pascal  [Select][+][-]
  1.  
  2. (item as TBCButton).Down:=false;
  3.  
  4.  

Hmmm, I tried a lot of ways, but never would of thought that!


THANK YOU!
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: TCButton Array "Down" Property Not Working
« Reply #8 on: May 02, 2019, 09:36:49 pm »
TControl is the base control,
TBCButton is a customized control that has additional properties like down, Hover, Gradients, etc etc etc, these are not part of the base TCOntrol.

Oh, make sense.
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] TCButton Array "Down" Property Not Working
« Reply #9 on: May 02, 2019, 09:57:43 pm »
In fact, you only need the check "if item is TBCButton" once.
You don't need the "as" checks as well.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);var
  2.   i: Integer;
  3.   item: TControl;
  4. begin
  5.   for i := (Panel1.ControlCount - 1) downto 0 do
  6.   begin
  7.     item := Panel1.controls[i];
  8.     if item is TBCButton then
  9.       begin
  10.         item.Caption:='Button' + IntToStr(i);
  11.         TBCButton(item).Down := False;
  12.       end;
  13.   end;
  14. end;

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [SOLVED] TCButton Array "Down" Property Not Working
« Reply #10 on: May 02, 2019, 10:28:15 pm »
In fact, you only need the check "if item is TBCButton" once.
You don't need the "as" checks as well.

The "Something as TSomeClas" is not a check, it is the elegant  (and safer) way of casting Someting as a TSomeClass instead of using the not-always-safe hard-cast TSomeClass(Something).

There are not many differences but the main one is that using as allows the compiler (and runtime?) to check whether it's safe to cast, while the hardcast forces it to cast whether it's safe or not.

ETA Forgot to say that if you really know what you're doing (as in this case, after the check with "is") the hard-cast may be be slightly quicker. Avoiding all those pesky checks and all that.

So you're mostly right :)
« Last Edit: May 02, 2019, 10:33:11 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] TCButton Array "Down" Property Not Working
« Reply #11 on: May 03, 2019, 09:05:57 am »
Perhaps I was foolish to term the "as" operator a "check".
It is only a check in the sense that if the potential cast is invalid it will raise an exception, which is rarely what the user wants to encounter.

If a cast is unsafe it means the programmer is at fault, not the user, and so some uses of "as" I see are simply lazy programming by people who perhaps aren't really sure what they are doing, and throw in an "as" to "protect" flawed code.

IMHO it is far better to make an explicit check using "is" which returns a clear boolean result. Provided the result is True, an appropriate hard cast is perfectly valid, and all risk of an exception (at least an exception on account of an invalid cast) is removed.

 

TinyPortal © 2005-2018