Recent

Author Topic: Anyone know how to change the properties of 32 SlideBars from one proceedure  (Read 2534 times)

andyf97

  • New Member
  • *
  • Posts: 21
How to change the properties of 32 ECSliders or other same components from one procedure

Actually all the parameters apart from Top and Left will be the same.

I was trying this way but it fails to set them obviously because I am a bit dumb, the ECSLiders are all on form1.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3. with ECSlider1.Knob do
  4.      BevelWidth:=2;
  5.      Color:=clHighlight;
  6.      Cursor:=crHandpoint;
  7.      Height:=32;
  8.      Style := eosButton;
  9.      TickMarkCount:=1;
  10.      TickMarkDesign:=etdSimple;
  11.      TickmarkSpacing:=2;
  12.      TickmarkStyle:=etsSolid;
  13.      Width:=40;
  14. end;  
  15.  

         

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Use the sender parameter.
Code: Pascal  [Select][+][-]
  1. if sender is TSomeControl then with sender as TSomeControl do.....
  2. // or with hardcast instead of softcast:
  3. if sender is TSomeControl then with TSomeControl(sender) do....
Such code makes it type specific, not instance specific, so you can handle any or all of the sliders from the same handler and you can even distinguish between all instances by accessing the control names, since they must be unique.

(Btw: this is also how it should be done. )
« Last Edit: April 01, 2023, 09:32:34 am by Thaddy »
Specialize a type, not a var.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
How to change the properties of 32 ECSliders or other same components from one procedure

Actually all the parameters apart from Top and Left will be the same.

I was trying this way but it fails to set them obviously because I am a bit dumb, the ECSLiders are all on form1.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3. with ECSlider1.Knob do
  4.      BevelWidth:=2;
  5.      Color:=clHighlight;
  6.      Cursor:=crHandpoint;
  7.      Height:=32;
  8.      Style := eosButton;
  9.      TickMarkCount:=1;
  10.      TickMarkDesign:=etdSimple;
  11.      TickmarkSpacing:=2;
  12.      TickmarkStyle:=etsSolid;
  13.      Width:=40;
  14. end;  
  15.  

         
I guess you are missing a begin/end, just a guess, I do not have that component.
Code: Pascal  [Select][+][-]
  1. with ECSlider1.Knob do
  2.   begin
  3.      BevelWidth:=2;
  4.      Color:=clHighlight;
  5.      Cursor:=crHandpoint;
  6.      Height:=32;
  7.      Style := eosButton;
  8.      TickMarkCount:=1;
  9.      TickMarkDesign:=etdSimple;
  10.      TickmarkSpacing:=2;
  11.      TickmarkStyle:=etsSolid;
  12.      Width:=40;
  13.   end;
  14. end;  
  15.  
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3. with ECSlider1.Knob do  // Seriously???? That is all wrong in this context! Use the sender. As I already explained
  4.  
BTW you are fired.  >:D O:-)
We are not here to promote the screendrawers guild.
We are here to promote proper language use.
(You are free to insult me of the use of with, btw. Not clean enough)
« Last Edit: April 01, 2023, 12:48:23 pm by Thaddy »
Specialize a type, not a var.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3. with ECSlider1.Knob do  // Seriously???? That is all wrong in this context! Use the sender. As I already explained
  4.  
BTW you are fired.  >:D O:-)
Sender inside a ButtonClick, what will it be? Let me think... maybe a button?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Sender inside a ButtonClick, what will it be? Let me think... maybe a button?
Wrong again, even more wrong:
You can connect eventhandlers with the same signature to all and everything with that signature.
That's why there is is and as....
If you do not understand it, ask again.

(Dead giveaway: give your event handlers and controls meaningful names)
« Last Edit: April 01, 2023, 12:56:18 pm by Thaddy »
Specialize a type, not a var.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
I am neither here to insult nor to challenge you.
I am fired and out of this topic, it is yours, enjoy.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

wp

  • Hero Member
  • *****
  • Posts: 11857
How to change the properties of 32 ECSliders or other same components from one procedure
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3. with ECSlider1.Knob do
  4.      BevelWidth:=2;
  5.      Color:=clHighlight;
  6.      Cursor:=crHandpoint;
  7.      Height:=32;
  8.      Style := eosButton;
  9.      TickMarkCount:=1;
  10.      TickMarkDesign:=etdSimple;
  11.      TickmarkSpacing:=2;
  12.      TickmarkStyle:=etsSolid;
  13.      Width:=40;
  14. end;  
  15.  
Do I understand correctly? There are 32 ECSliders on a form, and there is a button with which you want to change all properties (except for Left and Top) of the ECSliders?

In this case I'd define an array for all sliders and store the sliders in it:
Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     procedure FormCreate(Sender: TObject);
  4.   private
  5.     Sliders: array[0..31] of TECSlider;
  6.   end;
  7.  
  8. procedure TForm1.FormCreate(Sender: TObject);
  9. begin
  10.   Sliders[0] := ECSlider1;
  11.   Sliders[1] := ECSlider2;
  12.   ...
  13.   Sliders[31] := ECSlider32;
  14. end;

Alternatively, and a bit more clever, you could create the sliders at runtime (not in the Object Inspector) and assign them into the array immediately:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   slider : TECSlider;
  5.   x, y: Integer;
  6. begin
  7.   x := 12;
  8.   y := 12;
  9.   for i := Low(Sliders) do High(Sliders) do
  10.   begin
  11.     Sliders[i] := TECSlider.Create(self);
  12.     Sliders[i].Left := x;
  13.     Sliders[i].Top := y;
  14.     Sliders[i].Parent := self;
  15.     // ... set other properties here
  16.     y := y + Sliders[i].Height + 8;
  17.   end;
  18. end;
 
In the OnClick handler of the button I'd write a loop which iterates over all sliders in the array and sets their properties as requested.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4. begin
  5.   for i := Low(Sliders) to High(Sliders) do
  6.     with Sliders[i].Knob do
  7.     begin
  8.       BevelWidth:=2;
  9.       Color:=clHighlight;
  10.       Cursor:=crHandpoint;
  11.       Height:=32;
  12.       Style := eosButton;
  13.       TickMarkCount:=1;
  14.       TickMarkDesign:=etdSimple;
  15.       TickmarkSpacing:=2;
  16.       TickmarkStyle:=etsSolid;
  17.       Width:=40;
  18.     end;
  19.  

Thaddy, please stop playing the boss here. A boss insulting his employees is not a good boss anyway.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Thaddy, please stop playing the boss here. A boss insulting his employees is not a good boss anyway.
Well, if even you do not write proper code (using the sender parameter), what else can I do?
Although I agree with your comment, there is a limit. Someone has to take responsability. Otherwise you are not operating a solid team. This is like raising children, not about who's boss...
Use the sender and don't blame me. Pretty much how the language is designed.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     Button3: TButton;
  18.     Button4: TButton;
  19.     procedure AnyButtonClick(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.AnyButtonClick(Sender: TObject);
  36. begin
  37.   if sender is TButton then with TButton(sender) do
  38.      Caption := 'I am '+ Tbutton(sender).Name // and you have full control over the instance
  39.   else
  40.     Showmessage('I am a control but not really relevant, it seems');
  41. end;
  42.  
  43. end.
Pardon me for not giving the controls meaningful names. You know I would have done so.

If you can write the code in a one-liner and already explained this, then why complain?

Point is that it does not matter how many sliders,or in this case buttons, are there... See?
« Last Edit: April 01, 2023, 03:01:28 pm by Thaddy »
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11857
You are making assumptions about the OPs questions which are not clearly expressed. Read the first post. Are you sure that there are multiple buttons on the form? The way I understand the question there is only a single button which is supposed to modify on several control, and there is absolute no need to check the sender - a typical case for the {%H-} directive. OK - I am fired, too...

dseligo

  • Hero Member
  • *****
  • Posts: 1196
I would do it like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var i: Integer;
  3. begin
  4.   For i := 0 to Pred(ControlCount) do
  5.     If Controls[i] is TECSlider then
  6.       With (Controls[i] as TECSlider).Knob do begin
  7.         BevelWidth:=2;
  8.         Color:=clHighlight;
  9.         Cursor:=crHandpoint;
  10.         Height:=32;
  11.         Style := eosButton;
  12.         TickMarkCount:=1;
  13.         TickMarkDesign:=etdSimple;
  14.         TickmarkSpacing:=2;
  15.         TickmarkStyle:=etsSolid;
  16.         Width:=40;
  17.       end;
  18. end;

wp

  • Hero Member
  • *****
  • Posts: 11857
Yes, but only if there are no other TECSliders on the form (which is highly probable, though)

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Use the sender parameter.

He can't use Sender, because Sender is TButton and he wants to change TECSlider with clicking on the button. Sender will never be TECSlider.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Use the sender parameter.

He can't use Sender, because Sender is TButton and he wants to change TECSlider with clicking on the button. Sender will never be TECSlider.
Any control can be identified. I could also use TECSlider.... You are missing the point.
I think I retire .. 8-)
« Last Edit: April 01, 2023, 03:06:10 pm by Thaddy »
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6090
put all the controls inside of a TFlowPanel.

From there, you have a list of controls that you can step through via the ControlList property.

etc.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018