Recent

Author Topic: Managing a dynamic array of TButton  (Read 6537 times)

Micayah Ritchie

  • New Member
  • *
  • Posts: 18
Re: Managing a dynamic array of TButton
« Reply #15 on: February 23, 2019, 04:32:44 pm »
It will be nil but it won't ever be used. Yes it's a waste of memory but I always make errors whenever my first index is not 1.
Also I tried your suggestion and this time the buttons didn't appear at all. So I really don't know what to do now. Perhaps I'll find some way to simply not move the buttons after they are created

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Managing a dynamic array of TButton
« Reply #16 on: February 23, 2019, 04:42:42 pm »
@Micayah Ritchie: almost everything is indexed from 0 — dynamic arrays, lists, indexed properties etc (the only exception are strings). So why do you do everything differently and make your life difficult? Don't do this.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Managing a dynamic array of TButton
« Reply #17 on: February 23, 2019, 05:14:27 pm »
It will be nil but it won't ever be used. Yes it's a waste of memory but I always make errors whenever my first index is not 1.
Also I tried your suggestion and this time the buttons didn't appear at all. So I really don't know what to do now. Perhaps I'll find some way to simply not move the buttons after they are created
Maybe also add visible := true and do an application.processmessges? The latter may be enough. Note you not only have to set the top, but also left, I assume you are not that stupid? And count from zero, like everybody else except BASIC, COBOL or FOTRAN programmers. All the latter also CAPITALIZE everything. (A.K.A. shouting) which is why you can determine the age of the programmer by the wear of their capslock key... O:-)
(Note that if you do not set the left property too, all buttons get stacked on top of each other)


BTW: some of your family can - or could - count : https://en.wikipedia.org/wiki/Dennis_Ritchie

https://en.wikipedia.org/wiki/Zero-based_numbering
« Last Edit: February 23, 2019, 05:32:09 pm by Thaddy »
Specialize a type, not a var.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Managing a dynamic array of TButton
« Reply #18 on: February 23, 2019, 05:38:28 pm »
[...] I really don't know what to do now. Perhaps I'll find some way to simply not move the buttons after they are created

All you have to do is make absolutely that the very first time you call that function Someboolean is True and always that NumberOfRounds is not bigger than the number of buttons in the array.

Code: Pascal  [Select][+][-]
  1. { Adjusted everything to count from 1 ...
  2.   but it's *ugly*! :) }
  3.   If Someboolean then
  4.   begin
  5.     Setlength(RoundButtons, NumberOfRounds+1);
  6.     For x:= 1 to NumberOfRounds do
  7.     begin
  8.       RoundButtons[x]:=TButton.create(nil);
  9.       RoundButtons[x].parent:= MainForm;
  10.       RoundButtons[x].Caption:='Start Round';
  11.     end;
  12.     SomeBoolean:=false;
  13.   end;
  14.  
  15.   AProblem := (Length(RoundButtons) < 2) or
  16.               (Length(RoundButtons) < (NumberOfRounds+1));
  17.  
  18.   if AProblem then
  19.     { Raise an exception of whatever you want}
  20.   else begin
  21.     { From here on you can be sure there are as many buttons as you want }
  22.     For x:= 1 to NumberOfRounds do
  23.     begin
  24.       RoundButtons[x].Top:=50;
  25.     end;
  26.   end;
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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Managing a dynamic array of TButton
« Reply #19 on: February 23, 2019, 05:43:06 pm »
And still don't ever set the parent separately. Use the constructor, because depending on widget set it can have side effects you do not expect.
I am sure lucamar  tested just one platform...
« Last Edit: February 23, 2019, 05:44:59 pm by Thaddy »
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Managing a dynamic array of TButton
« Reply #20 on: February 23, 2019, 05:50:42 pm »
So I really don't know what to do now.

I'll try to upload the project, but It's poorly annotated and I'm not sure you'll get the point of what I'm trying to do but I will.

Show us the code, your code will be inspected by lots of experienced programmers' eyes here. Most of us here already have some suspicions in our mind.

To do it:
Create a new folder, copy and paste all the necessary files except: the binary (exe file), *.bak, lib and backup folders. Compress the folder and send the zip here.

I warn you, showing code here may attract criticism by seniors. But think positively, criticism is one of the things that can make you a better man - in this case a better programmer.

If you're to shy to show it, you can send it privately to me. I'm nice :D

And yes, you should use index start from 0. Just as Thaddy said, I started from BASIC and I was having some hard time to use 0 for index. Luckily, I forced myself. Hey, it won't too hard. A good programmer is a programmer that follows the "best practices". If you're the only programmer that write your own program, you're okay to use your own practices. But you're a lonely solo programmer, that cannot be fit into a team as a developer. That's sad.

Micayah Ritchie

  • New Member
  • *
  • Posts: 18
Re: Managing a dynamic array of TButton
« Reply #21 on: February 24, 2019, 04:03:21 am »

(Note that if you do not set the left property too, all buttons get stacked on top of each other)


Thanks, actually it was just a placeholder I have a procedure I use to set it set it's top, left, height and width as a fraction of the form's height and width.



I warn you, showing code here may attract criticism by seniors. But think positively, criticism is one of the things that can make you a better man - in this case a better programmer.


I don't mind the criticism and anything that can help me do better and learn more is welcomed because in all honesty I don't really know what I'm doing. However I'm going to upload it but I haven't started building any part of what the application is actually supposed to do, just coding the things that I'm going to need to do it as I see it in my head, since it's really something I do in spare time when I'm not at school. I have all my ideas written down in a book which I use as a reference. As a result, I must give warning that the application will look pointless where I'm at with it.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Managing a dynamic array of TButton
« Reply #22 on: February 24, 2019, 05:22:37 am »
Here is a simple project. Compare it with your project and see if it helps. It basically moves the buttons with the movement of the mouse.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Managing a dynamic array of TButton
« Reply #23 on: February 24, 2019, 03:36:34 pm »
if the coder wants a 1 base array and looking at his code here, wouldn't a fixed array of a given maximum size
work better?

Var
 RoundButtons:Array[ 1..MaximumAmountOfButtons ] of Tbutton;

from that, The "NumberOfRounds" variable can be used to index this array.

For X  :=  1 To NumberOfRounds do
 Begin
   RoundBUttons[ x ] := TButton.Create(nil);
   With RoundButtons[ x ] do
    Begin
      //set all the members of the button here
    End;
End;

Of course at some point, these buttons need to be freed before building this array again..

At program startup, this array should be nil .

at least this way, he has a 1 base index and faster execution   
The only true wisdom is knowing you know nothing

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Managing a dynamic array of TButton
« Reply #24 on: February 24, 2019, 04:05:17 pm »
Yes, of course Jamie. We still don't know if he really needs dynamic arrays.

 

TinyPortal © 2005-2018