Cause a Textbox to lose focus

C

chris

Does anybody know how I can programmatically cause a textbox to lose
focus and automatically jump to the next appropriate control.

I have a custom textbox usercontrol whereby the requirement is to
enable the user to jump around between controls by hitting the enter
key instead of tab key or mouse.


Thanks
 
T

Tom Porterfield

Does anybody know how I can programmatically cause a textbox to lose
focus and automatically jump to the next appropriate control.

I have a custom textbox usercontrol whereby the requirement is to
enable the user to jump around between controls by hitting the enter
key instead of tab key or mouse.

Assuming your control derives from System.Windows.Forms.Control. Look at
calling the SelectNextControl method on the Form object that the instance of
your control is on in an override OnKeyPress. You can call your FindForm
method to get the form that your control is on.
 
B

Bob Grommes

You can also call the Focus() method of a specific control, provided that
control's CanFocus property is true. Focus() returns a bool based on
success or failure; if CanFocus == false then Focus() will always return
false.

In my experience all "out of the box" WinForms controls will respond
correctly to Focus() even when the form hasn't been shown yet, but some
third party controls will not do this until the form is shown (specifically,
I've seen this with controls from Infragistics). So if you ever want to set
initial focus on a new form based on some condition or other, it's safest to
do that in the form's Activate event rather than in the Load event or from
somewhere outside the form before it's shown.

--Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top