468,457 Members | 1,782 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Setting focus on a control when the form is loaded

Hi, there!

I want to set focus on a control, of which TabIndex is not the first, when a
form is loaded. In the form's load event, I have used Control.Focus() but it
doesn't not work. The focus is not on the control I set. The focus is always
on the control of which TabIndex is 0.

Thanks.
Nov 21 '05 #1
4 1711
If you always want the same control to be given focus when the form loads,
the best (and easiest) way would be to use JavaScript. To do this, simply
add the following to you Body tag:

OnLoad="mycontrol.focus();"

--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Aung Thu" <Au*****@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
Hi, there!

I want to set focus on a control, of which TabIndex is not the first, when
a
form is loaded. In the form's load event, I have used Control.Focus() but
it
doesn't not work. The focus is not on the control I set. The focus is
always
on the control of which TabIndex is 0.

Thanks.

Nov 21 '05 #2
Thanks, Nathan.

Sorry, I forgot to mention about my application. It is Windows application,
not Web Form.

Base on the different condition, I need to set the initial focus on the
specific control when the form is loaded. So I tried the ControlName.Focus()
in the Load event of the form. But it doesn't work. How should I do to get it?

Thanks.

"Nathan Sokalski" wrote:
If you always want the same control to be given focus when the form loads,
the best (and easiest) way would be to use JavaScript. To do this, simply
add the following to you Body tag:

OnLoad="mycontrol.focus();"

--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Aung Thu" <Au*****@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
Hi, there!

I want to set focus on a control, of which TabIndex is not the first, when
a
form is loaded. In the form's load event, I have used Control.Focus() but
it
doesn't not work. The focus is not on the control I set. The focus is
always
on the control of which TabIndex is 0.

Thanks.


Nov 21 '05 #3
Aung,

Did you try it in the activated event of the form?

I hope this helps,

Cor
Nov 21 '05 #4
"Aung Thu" <Au*****@discussions.microsoft.com> schrieb:
I want to set focus on a control, of which TabIndex is not the first, when
a
form is loaded. In the form's load event, I have used Control.Focus() but
it
doesn't not work. The focus is not on the control I set. The focus is
always
on the control of which TabIndex is 0.


\\\
Private Sub Form1_Activated( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Activated
Static IsActivated As Boolean
If Not IsActivated Then
IsActivated = True
Application.DoEvents() ' ...
Me.TextBox1.Focus()
End If
End Sub
///

In .NET 2.0 Windows Forms forms have a 'Shown' event.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jason | last post: by
1 post views Thread by Abhram Jose | last post: by
12 posts views Thread by CLEAR-RCIC | last post: by
18 posts views Thread by Charles May | last post: by
3 posts views Thread by DJTN | last post: by
7 posts views Thread by Zytan | last post: by
3 posts views Thread by zacks | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.