Focus question

T

toysrwill

I have a form with many controls and I want the focus to stay on a
particular textbox no matter what the user does. I don't want to have
to code an event for absolutely everything the user can do. I tried
the leave event, but the problem with that is if the next event puts
text in the textbox then the cursor is at the beginning of the text
not the end. I know ways to accomplish this, however I just trying to
see if there is a better way. Maybe there is an event that is executed
after everything else.
Thanks in advance,
Will
 
A

AlexS

Hi, toysrwill

Did you look at Validation events (Validating, Validated)? Do they solve
your problems?

HTH
Alex
 
M

Morten Wennevik

Hi Will,

You can try using the Control.Enter event and set all controls to focus the textbox instead.

foreach(Control c in this.Controls)
{
c.Enter += new EventHandler(c_Enter);
}

private void c_Enter(object sender, EventArgs e)
{
textBox1.Focus();
}

This will let you use Click events for buttons. It will preserve cursor position of the textbox, but not let you select text from other textboxes.

I wonder if it would be better to just disable everything else until decide to let the user do other stuff.


Happy coding!
Morten Wennevik [C# MVP]
 

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