Recent

Author Topic: help using dynamic array in gui application[Solved]  (Read 2628 times)

jerryferry123

  • Newbie
  • Posts: 5
help using dynamic array in gui application[Solved]
« on: September 17, 2017, 03:21:59 pm »
Lazarus version 1.6.4, Widgetset gtk 2, fpc 3.0.2, Ubuntu 16.04

I could use some help using dynamic arrays in a gui project. I've not figured out how to declare them such that they are available from gui components on a form. All the examples I’ve found are used in terminal type applications.
« Last Edit: September 19, 2017, 01:07:22 pm by jerryferry123 »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: help using dynamic array in gui application
« Reply #1 on: September 17, 2017, 03:59:37 pm »
Save a new project in Lazarus in a suitable folder.
Double-click the form to generate an OnCreate handler, and try this:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   SysUtils, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.  
  12.   TForm1 = class(TForm)
  13.     procedure FormCreate(Sender: TObject);
  14.   private
  15.     memo: TMemo;
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.lfm}
  24.  
  25. procedure TForm1.FormCreate(Sender: TObject);
  26. var
  27.   sa: TStringArray;
  28.   s: String;
  29. begin
  30.   SetLength(sa, 4);
  31.   sa:=TStringArray.create('zero','one','two','three');
  32.   memo:=TMemo.Create(Self);
  33.   memo.Align:=alClient;
  34.   memo.Clear;
  35.   for s in sa do
  36.     memo.Lines.Add(s);
  37.   memo.Parent:=Self;
  38. end;
  39.  
  40. end.

jerryferry123

  • Newbie
  • Posts: 5
Re: help using dynamic array in gui application
« Reply #2 on: September 18, 2017, 08:47:19 pm »
Thanks for the quick reply. I tried the example then added a button which tried to reference the sa string array from code, identifier not found.

I had hoped to find a way to define a dynamic array in such a way that it can be used by the other components of a form. 

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: help using dynamic array in gui application
« Reply #3 on: September 18, 2017, 09:00:57 pm »
But that is easy, very small change, declare the dynamic array in the form.:
Code: Text  [Select][+][-]
  1. uses
  2.   SysUtils, Forms, Controls, StdCtrls;
  3.  
  4. type
  5.  
  6.   TForm1 = class(TForm)
  7.     procedure FormCreate(Sender: TObject);
  8.   private
  9.     memo: TMemo;
  10.   protected
  11.      sa: TStringArray;// declare the dynamic array here...
  12.    end;
  13.  
  14. var
  15.   Form1: TForm1;
  16.  
  17. implementation
  18.  
  19. {$R *.lfm}
  20.  
  21. procedure TForm1.FormCreate(Sender: TObject);
  22. var
  23.   s: String;
  24. begin
  25.   sa:=TStringArray.create('zero','one','two','three'); // no need to set length
  26. // or in trunk: sa := ['zero','one','two','three']; // implicit constructor
  27.   for s in sa do memo.lines.add(s);  //do not have to create dynamically, just put it on the form
  28. end;
  29.  
  30. end.

The dynamic array is now available to all components on the form. If you make it public instead of protected it is available everywhere, not only on the form.
« Last Edit: September 19, 2017, 01:10:06 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

jerryferry123

  • Newbie
  • Posts: 5
Re: help using dynamic array in gui application
« Reply #4 on: September 19, 2017, 01:06:39 pm »
Wow, thanks Thaddy for a simple explanation.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: help using dynamic array in gui application[Solved]
« Reply #5 on: September 19, 2017, 01:12:53 pm »
I couldn't have done it without howardpc's initial answer.
Glad to be of help.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018