Tuesday, June 22, 2021

Delphi: components array vs controls array

Yesterday, whilst programming the 'saved query' code, I made much use of the 'components' array in order to traverse the various fields on the screen and examine their values, such as

for i:= 0 to componentcount - 1 do if components[i] is TLabeledEdit then begin ord:= TLabeledEdit (components[i]).TabOrder; flags[ord]:= TLabeledEdit (components[i]).text <> '' end else if components[i] is TComboBox then begin ord:= TComboBox (components[i]).TabOrder; flags[ord]:= TComboBox (components[i]).text <> '-' end else if components[i] is TRadioGroup then begin ord:= TRadioGroup (components[i]).TabOrder; flags[ord]:= TRadioGroup (components[i]).itemindex <> 2; end;

The generic code that I wrote yesterday was similar but I knew that 'componentcount' and the components array were properties of the form itself. That might be so, but the taborder values that I was obtaining didn't seem correct.

After researching the topic slightly on Stack Overflow, I discovered that the components array contains every object that is owned by the form (I found a good answer yesterday but can't find it today). Despite the fact that the edit boxes, combo box and radio group are placed on a tab sheet (which in turn is a son of a page control which is a son of the form), their owner is the form. If I want to iterate over the objects that I placed on the tab sheet, I need to use the 'controls' array and pass the tab sheet as the 'father'. I discovered this backwards by trying to iterate over the tab sheet's components array and finding it empty ... and not understanding why.

Apart from anything else, there are far fewer objects in the tab sheet's control array than there in the form's components array, so traversing the first array will be faster than the latter. I hope that this time the taborder values that the code will read are 'correct', i.e. correspond to the values that I set in the object inspector. The taborder that I see refers to the order within the tab sheet.

Today I wrote a test program with a form similar to the parameters form from the Manager program; I wrote a simple debug routine to output the controls' name and where appropriate, the tag and tab order. This worked as expected, and when I updated the Manager program using the controls array, everything worked properly. I don't know what I messed up yesterday evening.....

No comments: