Set focus on a specified control

  • Thread starter Thread starter Gilbert Tordeur
  • Start date Start date
G

Gilbert Tordeur

How can I set the focus on a specified control in a Windows form ?
Thank you.
Gilbert
 
[yourcontrol].Focus( )
eg. TextBox1.Focus( ), etc

you can check the return value to determine whether the focus was set
successfully or not.
true if successful; false otherwise.

hope this helps..
Imran.
 
* "Gilbert Tordeur said:
How can I set the focus on a specified control in a Windows form ?

In addition to the other replies: Notice that the focus can only be set
to a visible and enabled control.
 
Herfried K. Wagner said:
In addition to the other replies: Notice that the focus can only be set
to a visible and enabled control.

Ugh! This is SO important. I wasted a whole day not being able to figure out
why button1.PerformClick() wasn't working. Turns out it will only work IF
the button is enabled. BTW, is there a way to execute the sub if the button
IS NOT enabled? It's no big deal to check it's status, enable it if off, and
disable it again later (well, it is kind of a pain) but it would be nice to
be able to override it without worrying about it's status and making sure
it's returned to it's previous state.
 
Ricky,

Until now I did not know this problem, however a lot of people are just
calling the sub.

The performclick does something as
button1_click(nothing,nothing)

What is found by some active in this newsgroup awful, however the only
reason I can think about that it looks very awful (what I find as well)

:-)

Cor
 
Cor Ligthert said:
Ricky,

Until now I did not know this problem, however a lot of people are just
calling the sub.

The performclick does something as
button1_click(nothing,nothing)

Ah. I knew it was just a subroutine but I couldn't figure out how to call it
with the correct signature. So I can pass it Nothing as the arguments and it
won't choke (as long as I don't try to do anything with what is normally in
those arguments)? Sweet.
 
* "Ricky W. Hunt said:
Ugh! This is SO important. I wasted a whole day not being able to figure out
why button1.PerformClick() wasn't working. Turns out it will only work IF
the button is enabled. BTW, is there a way to execute the sub if the button
IS NOT enabled? It's no big deal to check it's status, enable it if off, and
disable it again later (well, it is kind of a pain) but it would be nice to
be able to override it without worrying about it's status and making sure
it's returned to it's previous state.

I would take the code from the button's 'Click' event handler and place
it in a separate method. Then you can call this method from within the
'Click' event handler and from the place where you want the method to be
executed.
 

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

Back
Top