Board index » cppbuilder » Setting Focus ?

Setting Focus ?


2004-12-01 11:57:44 PM
cppbuilder16
I need to show a control ('InputField') that is always visible, but
placed onto a panel ('Panel') that can be shown or hidden. As soon as
the panel becomes visible, the control shall automatically be focused.
Therefore, I use the following code:
Panel->Show();
Update();
InputField->SetFocus();
Unfortunately, this doesn't work: I usually get an error message stating
that an invisible or disabled window cannot be focused.
Max
 
 

Re:Setting Focus ?

Hi Max
I seem to remeber running into something similar a while ago but the details
are a bit fuzzy. However, I think it had to do with the fact that the
Show() method sends a message to the TPanel's message queue that isn't
processed until after the function completes. The SetFocus() method is
being called before the TPanel has a chance to show itself.
IIRC, what I ended up doing was moving the SetFocus() into the OnActivate
event of the TPanel. I think an alternative would be to call
ProcessMessages() after calling Update().
Hopefully one of these solutions will help.
Ciao
Bill
"Max Blank" < XXXX@XXXXX.COM >wrote in message
Quote
I need to show a control ('InputField') that is always visible, but
placed onto a panel ('Panel') that can be shown or hidden. As soon as
the panel becomes visible, the control shall automatically be focused.
Therefore, I use the following code:

Panel->Show();
Update();
InputField->SetFocus();

Unfortunately, this doesn't work: I usually get an error message stating
that an invisible or disabled window cannot be focused.


Max

 

Re:Setting Focus ?

Max Blank wrote:
Quote
I need to show a control ('InputField') that is always visible, but
placed onto a panel ('Panel') that can be shown or hidden. As soon as
the panel becomes visible, the control shall automatically be focused.
Therefore, I use the following code:

Panel->Show();
Update();
InputField->SetFocus();

Unfortunately, this doesn't work: I usually get an error message stating
that an invisible or disabled window cannot be focused.


Max

In Panel's OnShow event try the following.
void __fastcall TPanel::FormShow(TObject *Sender)
{
... // Other code.
InputField->SetFocus();
}
HTH
 

{smallsort}

Re:Setting Focus ?

"William Charles Nickerson" <billATwcnickersonDOTca>wrote in message
Quote
IIRC, what I ended up doing was moving the SetFocus() into the OnActivate
Correction: OnShow event (as Danzer pointed out 8=)
Quote
event of the TPanel. I think an alternative would be to call
ProcessMessages() after calling Update().
Ciao
Bill
 

Re:Setting Focus ?

"Max Blank" < XXXX@XXXXX.COM >wrote in message
Quote
I need to show a control ('InputField') that is always visible, but
placed onto a panel ('Panel') that can be shown or hidden. As soon as
the panel becomes visible, the control shall automatically be focused.
Therefore, I use the following code:

Panel->Show();
Update();
InputField->SetFocus();

Unfortunately, this doesn't work: I usually get an error message stating
that an invisible or disabled window cannot be focused.
Panel->Show();
ActiveControl = InputField;
You can safely set the active control even when the control or form is not
visible. When conditions permit, it will get the focus.