Setting Focus to A Control on a Form

  • Thread starter Thread starter Charles May
  • Start date Start date
C

Charles May

I know I've done this somewhere but I cannot find any example of it in any
of my test code.

I have a form with a Listbox that is filled by a dataset it fills in various
textboxes.

When the form first opens, I have to tab to the listbox so that I can scroll
down the list.

How do I setfocus to the listbox (I know I can change the tab order but I
thought I could move to it with a setfocus method)

Thanks

Charlie
 
Thanks BB
I had tried that but it didn't work. Then I went back in to see if I had
done something wrong
and found that I had put the code in the wrong routine.

Thanks
Charlie
 
Thanks BB
I had tried that but it didn't work. Then I went back in to see if I had
done something wrong
and found that I had put the code in the wrong routine.
You should put it in the Load event of the form, so that focus is
gained as soon as the form is loaded. Make sure the control has the
tabstop property enabled
 
Pepi Tonas said:
You should put it in the Load event of the form, so that focus is
gained as soon as the form is loaded. Make sure the control has
the tabstop property enabled

Setting the focus in the Load event does not work because the control must
be visible to gain the focus.
 
* "Armin Zingler said:
Setting the focus in the Load event does not work because the control must
be visible to gain the focus.

It will work if you show the form in the 'Load' event handler.

;-)
 
Herfried K. Wagner said:
It will work if you show the form in the 'Load' event handler.

;-)

Yep. But bad design in IMHO (load fires the first time before the form is
shown, so it is not necessary to show it because it's just about to be
shown).
 
* "Armin Zingler said:
Yep. But bad design in IMHO (load fires the first time before the form is
shown, so it is not necessary to show it because it's just about to be
shown).

But it may help to avoid ugly 'Activated' hacks to execute code after
showing the form.
 
Herfried K. Wagner said:
But it may help to avoid ugly 'Activated' hacks to execute code
after showing the form.

theform.show
'theform.refresh
theform.thecontrol.focus
 
Setting the focus in the Load event does not work because the control must
be visible to gain the focus.

That explains why it sometimes works for me and sometimes not.
Appreciate your correction. Thanks
 
Back
Top