Mega Search
23.2 Million


Sign Up

Make a donation  
Too many defaults  
News Group: borland.public.delphi.ide.general

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!


Vote for best question.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 5:21 PM EST
From: Allen Nugent
 
Re: Too many defaults  
News Group: borland.public.delphi.ide.general
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


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 4:33 PM EST
From: Allen Nugent
 
Re: Too many defaults  
News Group: borland.public.delphi.ide.general
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

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 7:02 PM EST
From: Rudy Velthuis [TeamB]
 
Re: Too many defaults  
News Group: borland.public.delphi.ide.general
"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 



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 9:33 AM EST
From: Remy Lebeau \(TeamB\)
 
Re: Too many defaults  
News Group: borland.public.delphi.ide.general
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

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 9:57 AM EST
From: Marc Rohloff [TeamB]
 
Re: Too many defaults  
News Group: borland.public.delphi.ide.general
- 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

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 1:11 PM EST
From: Rudy Velthuis [TeamB]
 
Re: Too many defaults  
News Group: borland.public.delphi.ide.general
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

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 1:08 PM EST
From: Rudy Velthuis [TeamB]
 
Re: Too many defaults  
News Group: borland.public.delphi.ide.general
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 
>
>


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 6:14 PM EST
From: Allen Nugent
 
Re: Too many defaults  
News Group: borland.public.delphi.ide.general
"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 



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 5:35 PM EST
From: Remy Lebeau \(TeamB\)
 
Re: Too many defaults  
News Group: borland.public.delphi.ide.general
> 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



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 12:44 AM EST
From: -