Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
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
 
Old July 24th, 2004, 09:58 AM
Authorized User
 
Join Date: Jul 2004
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old July 24th, 2004, 12:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old July 26th, 2004, 12:05 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

You may put the focus code in the form activate event.
 
Old July 26th, 2004, 03:53 AM
Authorized User
 
Join Date: Apr 2004
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The form_load Event do'nt meant that it Visible

 
Old July 26th, 2004, 08:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

On the ON LOAD event of the form, put this

Me.TextBox1.SetFocus


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old July 26th, 2004, 12:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old July 26th, 2004, 12:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

;)
 
Old July 26th, 2004, 03:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

;)
 
Old August 28th, 2008, 10:13 AM
Registered User
 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default

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)
 
Old February 10th, 2017, 12:43 AM
Registered User
 
Join Date: Feb 2017
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default

This is working, my problem is solved, thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to load multiple Images on the Form? shah123 C# 1 May 20th, 2007 09:28 PM
The MDIChild Form does not load Dhirendra ADO.NET 1 September 23rd, 2006 12:01 PM
Setfocus on textbox rtr1900 Classic ASP Basics 2 July 5th, 2006 05:40 AM
Continuous form / SetFocus help dbartelt Access 5 February 13th, 2006 03:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.