Recent

Author Topic: how to set the 'real' focus to a form  (Read 7657 times)

Martin V

  • Full Member
  • ***
  • Posts: 139
how to set the 'real' focus to a form
« on: May 10, 2010, 02:05:58 pm »
Hello,

I have got a form which has at least one control which can hold the focus (e.g. a tEdit or a tButton). If I use

MyForm.SetFocus

then the active control inside the form still holds the focus. But I need that the controls loose the focus and the form really gets the focus so it will be ready itself for keyboard input. The only way that seems to work is to set all controls to visible := false and then to set all controls to visible true again - not a pretty way to do this. Other trials failed (ActiveControl, Focused). Any ideas for a better way to do this?

Thanks for any hints,
Martin

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: how to set the 'real' focus to a form
« Reply #1 on: May 10, 2010, 03:40:29 pm »
I'm not sure why you would ever want to do that  %)
Can't you just set KeyPreview to true and test whether or not a keypress needs to be captured?

Or:
you can put a panel on the form that has the same color as your form and no bevel or caption so it's invisible.
And set width and height to 1, so if fits anywhere.
Set Tabstop to false.

Now if you set the focus to this panel, also set the form's KeyPreview to true.
This basically does what you want.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Martin V

  • Full Member
  • ***
  • Posts: 139
Re: how to set the 'real' focus to a form
« Reply #2 on: May 10, 2010, 10:30:00 pm »
eny,

yes, a tPanel would fit the purpose. But I wonder if there is no possibility to loose the focus of all controls in a form, because in this case, the focus would drop back to the form handle.

tPanel should work indeed, but is not useful for me because it is an existing project and I do not want to change the program basics because of backward compatibility.

Martin

 

TinyPortal © 2005-2018