Recent

Author Topic: Variable for label/button name?  (Read 5239 times)

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Variable for label/button name?
« on: September 14, 2014, 04:32:14 pm »
Lazarus version: 1.2.4
OS: Windows 8.1
Hi, I'm trying to specify a certain button by having it's number set by a variable.
e.g. Instead of Button1, Button(i). i being a variable.

I began trying this with a label but when error emerged I presumed it was due to label being a completely different command, when considered separate from anything else.
Yet to my dismay I was confronted with the same error message when I changed to use buttons.

Is there anyway that I could do this?
Code: [Select]
  B20:=Total DIV 20;
  A20:=Total DIV 20;
  B10:=A20 DIV 10;
  A10:=A20 DIV 10;
  B5:=A10 DIV 5;
  A5:=A10 DIV 5;
  B2:=A5 DIV 2;
  A2:=A5 DIV 2;
  B1:=A2 DIV 1;
  A1:=A2 DIV 1;
  For i:=1 to 5 do
    Begin
      case i of
        1:Count:=20;
        2:Count:=10;
        3:Count:=5;
        4:Count:=2;
        5:Count:=1;
      end;
      Button(i).Caption:=('£',Count,': ',B(Count));
    end;
  i:=1

Also, I would appreciate anything to improve (shorten/tidy) my code. Thanks.
Programming is my middle name! Wait, no it is not...

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Variable for label/button name?
« Reply #1 on: September 14, 2014, 04:51:24 pm »
You can store Buttons in array and then you need to initialize the array at start of your program.
Code: [Select]
Buttons: array of TButton;
...
SetLength(Buttons, 5);
Buttons[0]:=Button1;
Buttons[1]:=Button2;
...
Buttons[i];  // or Buttons[i-1]; in your case.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Variable for label/button name?
« Reply #2 on: September 14, 2014, 04:56:06 pm »
Well, for a start...
Code: [Select]
:=('£',Count,': ',B(Count));This simply isn't Pascal code and will never compile.  That individual part of the statement can be re-written a few ways...
Code: [Select]
:='£' + IntToStr(Count)+': B(' + IntToStr(Count)+')';
or
:=Format('£ %d Count: B(%d)', [Count, Count]);

But this won't solve all your problems...

I have absolutely no idea what you're doing here, or how it is pertinent to the rest of the code example :-(
Code: [Select]
  B20:=Total DIV 20;
  A20:=Total DIV 20;
  B10:=A20 DIV 10;
  A10:=A20 DIV 10;
  B5:=A10 DIV 5;
  A5:=A10 DIV 5;
  B2:=A5 DIV 2;
  A2:=A5 DIV 2;
  B1:=A2 DIV 1;
  A1:=A2 DIV 1;

And here, you're using an array that hasn't been populated...
Code: [Select]
Button(i)
There's a possibility that you think that calling for instance Button(0) will access the first button in your form.  This isn't the case.  There are ways to set up arrays to reference designated controls, and I'll post an example if you like.  But for now I would recommend that you set the caption of each button individually.

Code: [Select]
Button1.Caption := Format('£ %d Count: B(%d)', [0, 0]);
(@Blaazen is a faster typer that I :)  He show's one of the ways you could set that array up)

Quote
Yet to my dismay I was confronted with the same error message when I changed to use buttons.

In this instance you haven't told us what the error is, so this limits our ability to help you.  I'm guessing though that you got a series of errors, the first one being the incorrect string concatenation, and the subsequent ones being button() array not defined.    In future, please let us know exactly what errors you're getting, and this will help us resolve your issue faster.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Variable for label/button name?
« Reply #3 on: September 14, 2014, 09:06:45 pm »
Thanks for the help guys, I've just gone with the simple method (avoiding doing an array at this moment in time).

Mike.Cornflake: This part of the code probably would make no sense as I wasn't concentrating when I did it, half of them should have MOD not DIV.
Code: [Select]
  B20:=Total DIV 20;
  A20:=Total DIV 20;
  B10:=A20 DIV 10;
  A10:=A20 DIV 10;
  B5:=A10 DIV 5;
  A5:=A10 DIV 5;
  B2:=A5 DIV 2;
  A2:=A5 DIV 2;
  B1:=A2 DIV 1;
  A1:=A2 DIV 1;

Blaazen: In terms of the array I presume that I could declare it in a different unit as long as that unit is in the uses section?

P.S. The only reason I'm doing this is that I've just started college and this is one of the programs in the extension task, I was bored and thought I would upgrade it from a basic application.
Programming is my middle name! Wait, no it is not...

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Variable for label/button name?
« Reply #4 on: September 14, 2014, 09:09:35 pm »
Blaazen: In terms of the array I presume that I could declare it in a different unit as long as that unit is in the uses section?

I'm not blaazen but as long as the array is declared in the interface section of the external unit you will have no problems.
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

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Variable for label/button name?
« Reply #5 on: September 14, 2014, 09:27:11 pm »
Okay, thanks! :)
Programming is my middle name! Wait, no it is not...

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Variable for label/button name?
« Reply #6 on: September 16, 2014, 01:19:05 pm »
You can use loop through all components on a form using ComponentCount and getting component name with ComponentName. If your buttons are Button1, Button2... then you can simply cutoff 'Button' and converting remaining to match a button number.

Another approach is explained here:
http://forum.lazarus.freepascal.org/index.php/topic,21587.msg126488.html#msg126488
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018