By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

Set focus on a specified control

P: n/a
How can I set the focus on a specified control in a Windows form ?
Thank you.
Gilbert
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Simply:

[Control Name].Select

e.g. TextBox1.Select

Hope this helps.
"Gilbert Tordeur" <gi*************@fr.york.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
How can I set the focus on a specified control in a Windows form ?
Thank you.
Gilbert

Nov 21 '05 #2

P: n/a
[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" <gi*************@fr.york.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
How can I set the focus on a specified control in a Windows form ?
Thank you.
Gilbert

Nov 21 '05 #3

P: n/a
M
What about textbox1.Focus()

"Matt S" <ma**@toomuchspamalready.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Simply:

[Control Name].Select

e.g. TextBox1.Select

Hope this helps.
"Gilbert Tordeur" <gi*************@fr.york.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
How can I set the focus on a specified control in a Windows form ?
Thank you.
Gilbert


Nov 21 '05 #4

P: n/a
* "Gilbert Tordeur" <gi*************@fr.york.com> scripsit:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O6*************@TK2MSFTNGP11.phx.gbl...
* "Gilbert Tordeur" <gi*************@fr.york.com> scripsit:
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.


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.
Nov 21 '05 #6

P: n/a
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
Nov 21 '05 #7

P: n/a
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.
Nov 21 '05 #8

P: n/a
* "Ricky W. Hunt" <rh*****@hotmail.com> scripsit:
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.


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.