[SOLVED] Array of forms? lazarus / 61-Beginners ---------------------------------------------------------------------- FiftyTifty · 2017-02-04 01:58 So I finally got around to working on the gui for my .MAP file editor, and dear god, the number of TEdit forms. There's a maximum of 8 entries, each one with 14 values that will have their own TEdit form. That's 112 individual entries that I'll have to call, line by line, to enable/disable and get/set values for. Screw that, that's a ruddy nightmare. Aren't arrays designed for this sort of tedious nightmare? How do I go about chucking forms into an array, and then iterating on them? I'm willing to bite the bullet and manually add them to arrays, due to having to split them up into specific "groups" anyway, but even that's many times better than the, err, "traditional" way. ---------------------------------------------------------------------- derek.john.evans · 2017-02-04 02:04 Use TComponent ownership. ---------------------------------------------------------------------- FiftyTifty · 2017-02-04 02:27 Quote from: Geepster on February 04, 2017, 03:04:21 amUse TComponent ownership. Ooh, didn't know that existed. So essentially, the form hierarchy in the Object Inspector is a bunch of nested arrays? Here's how the project looks: [spoiler](https://snag.gy/Oj61wR.jpg)[/spoiler] Trouble is, I need to split the "arrays" up. If I do a loop on [bformWindow.formPageStats.Components[][/b], I'll be operating on all of the child forms, and I'll have to use an ungodly number of if statements. There's probably a GroupBox form I can use to get that done. So yeah, seems simple enough, now that I think about it. Another problem, is that it's a pain in the ass to keep typing out the form hierarchy. Is there a way to get to the same data with a variable instead? Sounds like what pointers were made for. ---------------------------------------------------------------------- derek.john.evans · 2017-02-04 02:43 Yep, usually TComponent ownership is enough. You can also use TControl parenting which is the visual relationship. ie: ControlCount, Controls[] I normally write methods/properties to extract components/controls of a know type within the hierarchy. That way, im not type casting all over the place. ---------------------------------------------------------------------- derek.john.evans · 2017-02-04 02:45 Quote from: MajinCry on February 04, 2017, 03:27:17 amAnother problem, is that it's a pain in the ass to keep typing out the form hierarchy. Is there a way to get to the same data with a variable instead? Sounds like what pointers were made for. Unsure what you mean, but, usually if you have a repeating control layout, you only have to write it once. ---------------------------------------------------------------------- FiftyTifty · 2017-02-04 04:37 Quote from: Geepster on February 04, 2017, 03:45:25 amUnsure what you mean, but, usually if you have a repeating control layout, you only have to write it once. Code: [Select]procedure MainProcedure(); begin   formWindow.formPageGroup.SelectNextPage(true);   formPageGroup. end; As I was typing that next line, this error popped up: Quoteprogramwindow.pas(123,3) Error: Identifier not found "formPageGroup" I'd like to avoid typing out the whole form hierarchy when I want to reference a specific form. Pretty much want to shorthand it, without giving the forms new names due to ease of reading. There any way to do that? Was thinking pointers, but they seem to be more for use with the basic variables (byte, integer, string, etc). ---------------------------------------------------------------------- FiftyTifty · 2017-02-04 06:15 Righty, tried to make a couple super basic functions using TComponent. There's no example code in the docs for it, so I assumed it would provide the same properties as a form. Guess that was wrong. Code: [Select]unit maincode; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, StdCtrls; implementation procedure EnableGroupChildForms(formParent: TComponent); var   iCounter: byte; begin   for iCounter := 0 to (formParent.ComponentCount - 1) do begin     formParent.Components[iCounter].Enabled := true;   end; end; procedure DisableGroupChildForms(formParent: TComponent); var   iCounter: byte; begin   for iCounter := 0 to (formParent.ComponentCount - 1) do begin     formParent.Components[iCounter].Enabled := false;   end; end; end. QuoteCompile Project, Target: DWMAPFileEditor.exe: Exit code 1, Errors: 2 maincode.pas(17,37) Error: identifier idents no member "Enabled" maincode.pas(26,37) Error: identifier idents no member "Enabled" So, ehm, where are the proper docs for this thing? ---------------------------------------------------------------------- ASerge · 2017-02-04 08:10 Quote from: MajinCry on February 04, 2017, 07:15:51 amCompile Project, Target: DWMAPFileEditor.exe: Exit code 1, Errors: 2 maincode.pas(17,37) Error: identifier idents no member "Enabled" maincode.pas(26,37) Error: identifier idents no member "Enabled" Enabled is TControl property, not TComponent. Use this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...(formParent: TWinControl);var  C: TControl;begin  for C in formParent.GetEnumeratorControls do    C.Enabled := False;end; ---------------------------------------------------------------------- minesadorada · 2017-02-04 08:20 Looking at your screenshot it looks like you have one window (TForm) with lots of Edit boxes (Tedit) on Tabsheets. Are you looking to make and control only the TEdits via an array? ---------------------------------------------------------------------- balazsszekely · 2017-02-04 09:02 It's not clear to me what are you trying to achieve, however the following function will recursively search for any TWinControl and disable/enable it according to its tag. Lets say you have a form with a TPageControl, one of the TTabSheet contains a TPanel and finally the panel contains an TEditt and a TButton(screenshot). Now if you wish to disable the TEdit,  just set the Tag to 10 and run the following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure DisableControl(AControl: TControl; ATag: Integer; AEnable: Boolean);var  I: Integer;begin  if AControl = nil then    Exit;  if AControl is TWinControl then    if TWinControl(AControl).Tag = ATag then      TWinControl(AControl).Enabled := AEnable;   for I := 0 to TWinControl(AControl).ControlCount - 1 do    DisableControl(TWinControl(AControl).Controls[I], ATag, AEnable);end; procedure TForm1.Button1Click(Sender: TObject);begin  DisableControl(Form1, 10, False)end; No matter how deeply nested a control is, the recursive function will find it. Basically you have to give unique tags to your controls belonging to a group, and you're good to go. ---------------------------------------------------------------------- FiftyTifty · 2017-02-04 18:13 Uploaded the current state of the project 'ere: https://github.com/MajinCry/Digimon-World-MAP-File-Editor Mostly just a GUI at this point. Quote from: minesadorada on February 04, 2017, 09:20:39 amLooking at your screenshot it looks like you have one window (TForm) with lots of Edit boxes (Tedit) on Tabsheets. Are you looking to make and control only the TEdits via an array? Pretty much. The TEdit forms are split into groups of eight, and I want to modify them through arrays. I only need to be able to get and edit the Enabled and Text properties. To do it manually one by one, for each TEdit, is just a complete nightmare. The way I see it, I'd create a few loops, each with their own function, and I'd just call the functions with a form array as one of the parameters. Quote from: ASerge on February 04, 2017, 09:10:26 amEnabled is TControl property, not TComponent. Use this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...(formParent: TWinControl);var  C: TControl;begin  for C in formParent.GetEnumeratorControls do    C.Enabled := False;end; Aye, this seems the way to go. I'll go and see if Lazarus throws any errors. Quote from: GetMem on February 04, 2017, 10:02:22 amIt's not clear to me what are you trying to achieve, however the following function will recursively search for any TWinControl and disable/enable it according to its tag. Lets say you have a form with a TPageControl, one of the TTabSheet contains a TPanel and finally the panel contains an TEditt and a TButton(screenshot). Now if you wish to disable the TEdit,  just set the Tag to 10 and run the following code: -snip- No matter how deeply nested a control is, the recursive function will find it. Basically you have to give unique tags to your controls belonging to a group, and you're good to go. I'd have to set tags for each individual control, and then have a bazillion lines checking for each tag, no? Would be fine if I only had a couple TEdit fields that needed changed, but I've got to change 96 of them. ---------------------------------------------------------------------- balazsszekely · 2017-02-04 18:45 QuoteI'd have to set tags for each individual control, and then have a bazillion lines checking for each tag, no? No! Not necessarily. The following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  C: TControl;begin  for C in formParent.GetEnumeratorControls do    C.Enabled := False;end; Is equivalent to my function, if you call it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DisableControl(formParent, 0, False)but you have more possibility. For example to enable/disable controls by hierarchy level or set a tag to a group of Edit(not individually). ---------------------------------------------------------------------- FiftyTifty · 2017-02-04 19:05 Quote from: GetMem on February 04, 2017, 07:45:58 pmNo! Not necessarily. The following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  C: TControl;begin  for C in formParent.GetEnumeratorControls do    C.Enabled := False;end; Is equivalent to my function, if you call it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DisableControl(formParent, 0, False)but you have more possibility. For example to enable/disable controls by hierarchy level or set a tag to a group of Edit(not individually). Huh, whaddya know, two ways to do the same thing. Your way would be better for more convoluted GUIs. Luckily, the files I'm making the editor for are rather simple. No nested structures and no pointers, so the program itself is also simple. Now I've got to work on the code. Tah for the help lads. ---------------------------------------------------------------------- minesadorada · 2017-02-05 07:34 In this case as they are all similar, it's easier to use an array of TEdits and make/set them up dynamically IMO.  That way you can assign a common handler for validation for example. ---------------------------------------------------------------------- FiftyTifty · 2017-02-05 16:58 Quote from: minesadorada on February 05, 2017, 08:34:15 amIn this case as they are all similar, it's easier to use an array of TEdits and make/set them up dynamically IMO.  That way you can assign a common handler for validation for example. No hable German mein senor. That would be a major pain in the arse though, no? Creating groups of eight forms, placing them into different tab forms, manually typing out the properties for them, putting code in amongst the auto- generated UI code...Ew. Sounds easier to just have a couple of nested loops return the TEdit forms and call a convoluted text validation function. ---------------------------------------------------------------------- howardpc · 2017-02-06 18:07 The attached project shows one way to manage multiple pages of columns of edits. It uses simple descendants of TPageControl, TTabsheet and TGroupBox customised for the sort of GUI you are developing. By including a custom enumerator, and using array properties indexed by strings it is possible to 'loop' over columns of edits and change their properties (e. g. Enabled) with just two or three lines of code which identify each pagecontrol page and each tabsheet 'column' using the same strings displayed in the GUI. ---------------------------------------------------------------------- FiftyTifty · 2017-02-06 19:02 Quote from: howardpc on February 06, 2017, 07:07:41 pmThe attached project shows one way to manage multiple pages of columns of edits. It uses simple descendants of TPageControl, TTabsheet and TGroupBox customised for the sort of GUI you are developing. By including a custom enumerator, and using array properties indexed by strings it is possible to 'loop' over columns of edits and change their properties (e. g. Enabled) with just two or three lines of code which identify each pagecontrol page and each tabsheet 'column' using the same strings displayed in the GUI. The problem with coding the GUI by hand, rather than using the WYSIWYG designer, is that you have to enter everything by hand. It's more than just a couple lines of code; there's quite a few there. Whereas with the gui, just make one moderately sized function that takes the number of TEdit fields, the parent form, and bam. Ctrl+C Ctrl+V, change the properties changed, and it will also only take a few lines to change it up. I assume, anyway. I could be completely off-base, as I've not yet figured out how to get the data, so no GUI action right now. Thought an AnsiString would be the way to go, but apparently it doesn't like buffers. ---------------------------------------------------------------------- howardpc · 2017-02-06 20:29 Quote from: MajinCry on February 06, 2017, 08:02:59 pmThe problem with coding the GUI by hand, rather than using the WYSIWYG designer, is that you have to enter everything by hand. It's more than just a couple lines of code; there's quite a few there. It's a matter of personal preference, of course, which route you take. However, for a project that involves placing 88 edits and 11 groupboxes, naming each one intelligently and aligning them all correctly I would say that the problem with using the WYSIWYG designer is that you have to do all that work by hand. How long did it take you to produce the 1008-line programwindow.lfm by using the Designer? It may well have been comparable to the time I took to write customised components that offer the flexibility to be extended and adapted easily. Anyway, I find writing code more satisfying than dropping numerous controls on forms and repeatedly setting the same properties time and time and again. I find RAD excellent to knock up small GUIs for programs that I'll only use once or twice myself. But as soon as the level of GUI complexity increases, and the program will be used by others who'll want changes (10 edits per column, not 8, and an alternate colour different from the hard-lfm-coded value of clInfoBk, etc.) you find a static .lfm quite restrictive, and difficult to adapt. If 44 of the 88 edits need their background color changed... well for me the effort of locating the right ones, and the tedium of altering their properties via the Object Inspector is too much. Whereas well-designed custom components can often be updated to adapt to the needs of version 2 or version 7 of the program with alteration of just one or two parameters. ---------------------------------------------------------------------- minesadorada · 2017-02-07 11:44 Quote from: MajinCry on February 05, 2017, 05:58:17 pmQuote from: minesadorada on February 05, 2017, 08:34:15 amIn this case as they are all similar, it's easier to use an array of TEdits and make/set them up dynamically IMO.  That way you can assign a common handler for validation for example. No hable German mein senor. WTF?  Oh, never mind. ---------------------------------------------------------------------- Thaddy · 2017-02-07 12:14 Quote from: minesadorada on February 07, 2017, 12:44:40 pmQuote from: MajinCry on February 05, 2017, 05:58:17 pmQuote from: minesadorada on February 05, 2017, 08:34:15 amIn this case as they are all similar, it's easier to use an array of TEdits and make/set them up dynamically IMO.  That way you can assign a common handler for validation for example. No hable German mein senor. WTF?  Oh, never mind. But I do mind... Didn't you know German is just over-complicated Dutch? And English is just the same as Old Dutch with a funny accent and less words so far less expressive and precise? (TRUE! etymologically...)