Board index » delphi » Modal and Modeless Forms,how to SetFocus?

Modal and Modeless Forms,how to SetFocus?

Hi All
i copied a Standard Dialog from the Repository Box, Adding
only an Edit Control, to the 2 Buttons(Ok,Cancel).
Now, i want the Edit Control to be in Focus,whenever i
show the Dialog by the ShowModal, because the focus is
always on the last clicked Button,so i added this line:
   MyDialog.ShowModal;
   MyDialog.Edit1.SetFocus;
........
the Edit is in Focus,when the Dialog form shows,but i get
 an Error, each time i click one of the Buttons,like:
"can't focus a disabled or not visible window".
i don't exactly understand what is the form position *after*
the click, and how it differs from a Modeless form,like
the FindDialog, e.g, which always focuse the edit control?
can this problem be solved,still with a Modal?
-------
Thank you
jacob
 

Re:Modal and Modeless Forms,how to SetFocus?


Quote
jacob muntner wrote:
>    MyDialog.ShowModal;
>    MyDialog.Edit1.SetFocus;

Try swapping the two lines.

--Doug

Re:Modal and Modeless Forms,how to SetFocus?


Swapping Raised the error Without showing the form!
Anyway,i managed to solve the problem by changing
the second line like this:
   MyDialog.ShowModal;
   MyDialog.ActiveControl := MyDialog.Edit1;
......
it works! though i don't see the difference?!
-----------
Thank you
jacob

Douglas J. Horton <dhor...@mpinet.net> wrote in message
news:3B48B31D.8C3B5F40@mpinet.net...

Quote
> jacob muntner wrote:

> >    MyDialog.ShowModal;
> >    MyDialog.Edit1.SetFocus;

> Try swapping the two lines.

> --Doug

Re:Modal and Modeless Forms,how to SetFocus?


Quote
In article <3b489685_1@dnews>, Jacob muntner wrote:
> i copied a Standard Dialog from the Repository Box, Adding
> only an Edit Control, to the 2 Buttons(Ok,Cancel).
> Now, i want the Edit Control to be in Focus,whenever i
> show the Dialog by the ShowModal, because the focus is
> always on the last clicked Button,so i added this line:
>    MyDialog.ShowModal;
>    MyDialog.Edit1.SetFocus;

Wrong way around. Try

  myDialog.ActiveControl := myDialog.Edit1;
  MyDialog.ShowModal;

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:Modal and Modeless Forms,how to SetFocus?


Quote
"jacob muntner" <jacob...@hotmail.co.il> wrote in message

news:3b48c6f4_1@dnews...

Quote
> Swapping Raised the error Without showing the form!

You can set ActiveControl before the form is showing but cannot set the
focus.

Quote
> Anyway,i managed to solve the problem by changing
> the second line like this:
>    MyDialog.ShowModal;
>    MyDialog.ActiveControl := MyDialog.Edit1;
> ......
> it works! though i don't see the difference?!

The difference is that you are setting the ActiveControl after the dialog
closes so it is now correctly set for the next showing (I'm assuming it is
auto-created).  The second line had no influence the first time the form was
shown.  Pre-setting conditions on Form.Close is perfectly valid provided you
only do this with forms that are not freed on closing.  It is a particularly
good place to put code if you want to re-set the dialog position, if you do
that just before form show you may see it moving.

--
Regards,
Chris Luck

Other Threads