I've just made the surprising discovery that more than one control on the same form can have its Default property set to True. This produces unpredictable results when [Enter] is keyed.
Is there a switch that prevents this beahaviour (i.e. forces all other controls to have .Default := False)? Or do I have to set them all in code?
Thanks!
Thanks Rudy (and everybody else)!
You've been a big help.
"Rudy Velthuis [TeamB]" wrote:
>Allen Nugent wrote:
>
>> Now I'm wondering if Delphi will let me loop over all controls in a
>> form.
>
>This should work:
>
> var
> I: Integer;
> Comp: TComponent;
>
> ...
>
> for I := 0 to ComponentCount - 1 do
> begin
> Comp := Components[I];
> if (Comp is TButton) and (Comp <> Button1) then
> TButton(Comp).Default := False;
> end;
>
>I'm not sure if this works:
>
> for Comp in Components do
> if (Comp is TButton) and (Comp <> Button1) then
> TButton(Comp).Default := False;
>
>
>--
>Rudy Velthuis [TeamB] http://www.teamb.com
>
>"If you try and take a cat apart to see how it works, the first
> thing you have in your hands is a non-working cat."
> -- Douglas Adams
Marc Rohloff [TeamB] wrote:
> > I'd rather go for the Components property. You can also recursively
> > check all Controls properties of the controls found, but that is
> > not as easy as using Components.
>
> Components also needs to be used recursively to handle situations like
> frames correctly.
Hmmm... that's true.
--
Rudy Velthuis [TeamB] http://www.teamb.com
"I do not have a body, I am a body." -- Unknown
"Allen Nugent" wrote in message
news:47857f9d$1@newsgroups.borland.com...
> Now I'm wondering if Delphi will let me loop over all controls in a form.
TWinControl (which TForm derives from) has ControlCount and Controls[]
properties for that, ie:
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
Ctrl: TControl;
begin
for I := 0 to Self.ControlCount-1 do
begin
Ctrl := Self.Controls[I];
// use Ctrl as needed ...
end;
end;
Gambit
On Thu, 10 Jan 2008 13:11:49 +0100, Rudy Velthuis [TeamB] wrote:
> Actually, that only contains the controls for which the form is parent.
> If the control is on a panel, then that panel is the parent, and the
> component won't appear in the form's Control property.
>
> I'd rather go for the Components property. You can also recursively
> check all Controls properties of the controls found, but that is not as
> easy as using Components.
Components also needs to be used recursively to handle situations like
frames correctly.
--
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com
- wrote:
>
> > Now I'm wondering if Delphi will let me loop over all controls in a
> > form. In VB, one can use something like "For Each ctrl In MyForm".
> > Does the Delphi "For...In" structure work like this?
>
> Look at the "controls" property.
Actually, that only contains the controls for which the form is parent.
If the control is on a panel, then that panel is the parent, and the
component won't appear in the form's Control property.
I'd rather go for the Components property. You can also recursively
check all Controls properties of the controls found, but that is not as
easy as using Components.
--
Rudy Velthuis [TeamB] http://www.teamb.com
"I have come to believe that the whole world is an enigma, a
harmless enigma that is made terrible by our own mad attempt to
interpret it as though it had an underlying truth."
-- Umberto Eco
Allen Nugent wrote:
> Now I'm wondering if Delphi will let me loop over all controls in a
> form.
This should work:
var
I: Integer;
Comp: TComponent;
...
for I := 0 to ComponentCount - 1 do
begin
Comp := Components[I];
if (Comp is TButton) and (Comp <> Button1) then
TButton(Comp).Default := False;
end;
I'm not sure if this works:
for Comp in Components do
if (Comp is TButton) and (Comp <> Button1) then
TButton(Comp).Default := False;
--
Rudy Velthuis [TeamB] http://www.teamb.com
"If you try and take a cat apart to see how it works, the first
thing you have in your hands is a non-working cat."
-- Douglas Adams
Thanks, Gambit.
Now I'm wondering if Delphi will let me loop over all controls in a form. In VB, one can use something like "For Each ctrl In MyForm". Does the Delphi "For...In" structure work like this?
(BTW: I find it very difficult to answer this kind of question using the RAD Studio Help. It seems to be meant for someone who is already proficient in Object Pascal.)
"Remy Lebeau \(TeamB\)" wrote:
>
>"Allen Nugent" wrote in message
>news:4785730c$1@newsgroups.borland.com...
>
>> I've just made the surprising discovery that more than one control on
>> the same form can have its Default property set to True. This produces
>> unpredictable results when [Enter] is keyed.
>
>This is documented behavior. Look at the documentation for the
>TButton.Default property:
>
> "Although an application can have more than one Default button, the form
>calls the OnClick event handler only for the first visible button in the tab
>order. Moreover, any button that has focus becomes the Default button
>temporarily; hence, if the user selects another button before pressing
>Enter, the selected button's OnClick event handler executes instead."
>
>> Is there a switch that prevents this beahaviour (i.e. forces all other
>> controls
>> to have .Default := False)?
>
>No.
>
>> Or do I have to set them all in code?
>
>In code, or at design-time, but either way you have to do it manually
>(unless you find a third-party IDE plugin that will do it for you).
>
>
>Gambit
>
>
"Allen Nugent" wrote in message
news:4785730c$1@newsgroups.borland.com...
> I've just made the surprising discovery that more than one control on
> the same form can have its Default property set to True. This produces
> unpredictable results when [Enter] is keyed.
This is documented behavior. Look at the documentation for the
TButton.Default property:
"Although an application can have more than one Default button, the form
calls the OnClick event handler only for the first visible button in the tab
order. Moreover, any button that has focus becomes the Default button
temporarily; hence, if the user selects another button before pressing
Enter, the selected button's OnClick event handler executes instead."
> Is there a switch that prevents this beahaviour (i.e. forces all other
> controls
> to have .Default := False)?
No.
> Or do I have to set them all in code?
In code, or at design-time, but either way you have to do it manually
(unless you find a third-party IDE plugin that will do it for you).
Gambit
> Now I'm wondering if Delphi will let me loop over all controls in a form.
> In VB, one can use something like "For Each ctrl In MyForm". Does the Delphi "For...In" structure work like this?
Look at the "controls" property.
From the help file:
Controls is an array of all the child controls. These are all controls
that list this control as their Parent property. The Controls property
is convenient for referring to the children of a control by number
rather than name. For example, Controls may be used to iterate over all
the child controls.
for i:= 0 to controlCount - 1 do
if controls[i] is then