 |
VB How-To Ask your "How do I do this with VB?" questions in this forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

July 24th, 2004, 09:58 AM
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
SetFocus on a textbox when form load
Hi,
can anyone tell me how should i setfocus on a textbox immediately after the form loads?
i have put textbox1.focus() ( Vb.net)
but it wont work. I put this code under form_load but the textbox is visible.!
__________________
Snowydust
|

July 24th, 2004, 12:43 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Either change the tabindex to 0 for the textbox or use this:
Show()
TextBox2.Focus()
You cannot use Focus if the object is disabled or invisible.
|

July 26th, 2004, 12:05 AM
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You may put the focus code in the form activate event.
|

July 26th, 2004, 03:53 AM
|
Authorized User
|
|
Join Date: Apr 2004
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The form_load Event do'nt meant that it Visible
|

July 26th, 2004, 08:56 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
On the ON LOAD event of the form, put this
Me.TextBox1.SetFocus
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|

July 26th, 2004, 12:06 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You can use SetFocus only if the component is visible, but because the form is still invisible during the Load event that code will not work. Put it in any of these evnets: Activate, Paint, Resize. Use a static boolean to do it only once:
public Form_XXX
static bSkip as boolean
if not bSkip then
txt.SetFocus
bSkip=true
endif
|

July 26th, 2004, 12:18 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Please enlighten me! What's the problem with the two suggestions I made. I might miss something important.
Serrano, I can't make the 'Me'and 'SetFocus' work in VB.NET. Should it?
;)
|

July 26th, 2004, 03:53 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Your suggestions are correct.
The focus is by default on the component with the lower tabIndex, thus some code must be added in cases where the default is not the right choice.
I did not like your second suggestion (although it works) because I prefer not to call Me.Show in a Load event, in case the form must be loaded but not shown (or shown later)
Marco
Quote:
quote:Originally posted by Birger
Please enlighten me! What's the problem with the two suggestions I made. I might miss something important.
Serrano, I can't make the 'Me'and 'SetFocus' work in VB.NET. Should it?
;)
|
|

August 28th, 2008, 10:13 AM
|
Registered User
|
|
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.TabIndex = 0
End Sub
|
The Following User Says Thank You to Annshul For This Useful Post:
|
zara (February 10th, 2017)
|

February 10th, 2017, 12:43 AM
|
Registered User
|
|
Join Date: Feb 2017
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
This is working, my problem is solved, thanks 
|
|
 |