p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB How-To (http://p2p.wrox.com/forumdisplay.php?f=78)
-   -   SetFocus on a textbox when form load (http://p2p.wrox.com/showthread.php?t=15885)

snowydust July 24th, 2004 09:58 AM

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.!

Birger July 24th, 2004 12:43 PM

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.

madhukp July 26th, 2004 12:05 AM

You may put the focus code in the form activate event.

minhtri July 26th, 2004 03:53 AM

The form_load Event do'nt meant that it Visible


SerranoG July 26th, 2004 08:56 AM

On the ON LOAD event of the form, put this

Me.TextBox1.SetFocus


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

marcostraf July 26th, 2004 12:06 PM

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

Birger July 26th, 2004 12:18 PM

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?

;)

marcostraf July 26th, 2004 03:53 PM

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?

;)

Annshul August 28th, 2008 10:13 AM

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.TabIndex = 0
    End Sub

zara February 10th, 2017 12:43 AM

This is working, my problem is solved, thanks[:)]


All times are GMT -4. The time now is 06:24 AM.

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