Solved

Set Focus on Textbox Upon Form load

Posted on 2006-11-16
18
8,429 Views
Last Modified: 2008-03-09
I'm trying to do something that was so simple in vb6.  I have a form (frmMain) that when a button is clicked it does frmScan.Show().  On this second form, I want the first textbox to have focus and be awaiting input from the user.  Here are the things I've tried:

1) From first form (frmMain) I did  frmScan.Show() and then frmScan.Focus()

2) Tried adding txtTextbox.Focus() to the form_activated and form_load subs

3)  Made sure that the textbox had Tabstop set to True and TabIndex set to 0 so it should get focus first.  I also went to view->tab stop order to be sure everything looked good.

Any suggestions would be appreciated.

0
Comment
Question by:durrence71
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
  • +1
18 Comments
 
LVL 6

Expert Comment

by:riyazthad
ID: 17959076
How many controls are there? Is there any other 0 tabindex control exist? I am sure View->Tab Order will help u to make focus.

Dont do anything like focus as in vb.6

0
 

Author Comment

by:durrence71
ID: 17959125
There are 11 controls and 6 labels.  There are no duplicate tab indexes.  I've tried not putting the txtTextbox.Focus() and just relying on tax indexes, but it still isn't working.  When the form comes up as far as the eye can tell nothing has focus.  If I hit tab once, it then is in the txtTextbox field that I want to have focus.
0
 
LVL 6

Expert Comment

by:riyazthad
ID: 17960161
Is this windows or web application?

Can you jusy verify  by creating a new project and single textbox?

Or are you getting for any project what ever the matter?

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17960429
Hi durrence71,

Assuming you are talking about a windows app,

Use the frmScan.Activated event:

    Private Sub frmScan_activate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        Me.txtTextbox.Focus()
    End Sub

I knew this worked, but didn't know why until I found caball88's post here: http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21325813.html

"set the focus in the Form_Activated event. The load event is fired first but the controls are not initialized and therefore cannot get the focus."

Cheers!
0
 

Author Comment

by:durrence71
ID: 17967925
I am talking about a windows app.  Hillwaaa that suggestion didn't work.  I've gone over tab order and triple checked everything and just can't figure this out.  Any more suggestions?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17967958
Is this a converted VB6 app via the wizard?

Does the problem persist in a New project?
0
 

Author Comment

by:durrence71
ID: 17968078
It is a converted VB6 app via the wizard?  Could that possibly have something to do with it?  I took a very simple vb6 demo program, converted it and then wrote the rest of the code for what I was trying to do.
0
 

Author Comment

by:durrence71
ID: 17968148
I tried creating a new project.  I created a form, placed 3 text boxes on it, then a button to end the program.  If I create the proper tab order, it works.  If I try to use the form_activated sub and force focus on a text box other than what the tab order thinks is 0, the tab order wins out and the form_activated set focus is for nothing.

I also did find out that the tab order focus works until I place a 3rd party TWAIN/Imaging control on the page.  Then the tab order focus will no longer work.

I am going to contact the vendor for the twain/imaging control to see if they've seen this.
0
 
LVL 6

Expert Comment

by:riyazthad
ID: 17968394
Dont put in Form Activated event, try to put in paint event. It will work.



0
 

Author Comment

by:durrence71
ID: 17968899
Adding it to the paint event did work, but then none of the buttons on the form work anymore.  The only way to close the form is to click on the X in the upper left.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17968908
What version VB.net are you working with?
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17968934
Hi durrence71 - so does your earlier post mean that you could focus to a textbox with tab order not 0 from the form activated event until the 3rd party control was added?  
0
 

Author Comment

by:durrence71
ID: 17968936
Visual Basic 2005 Express Edition
0
 

Author Comment

by:durrence71
ID: 17968948
Hillwaaa - I couldn't set focus to the text box using txtbox.Focus, but having the text box set as tab order of 0 it would have focus when the form loaded.  Upon adding the 3rd party control, it would no longer have focus when the form loaded.
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17968964
Ah - ok - activate method will only work for 2003 projects :(
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17968983
Ah - a simple solution... just tried this in vb 2005 professional, so hopefully it'll work for you:

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TextBox1.Select()
    End Sub
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 17969045
"Upon adding the 3rd party control, it would no longer have focus when the form loaded."

I've seen many a post here on EE where 3rd party controls have produced odd behaviour in applications.  See if the vendor can fix it...or get rid of the control.

Another thing you can try is using a Timer with a short interval like 500 milliseconds that fires only once at form load.  From the tick event set the focus on the control you want.  This way hopefully the form can fully load and whatever "interference" the 3rd party control was making will be already past...
0
 

Author Comment

by:durrence71
ID: 17980407
The 3rd party control company has gotten back with me.  They can't explain it, but are able to recreate the same problems I have listed on here.  They recommend using a Timer like Idle_Mind suggested, hence I will give the points to Idle_Mind.  Thanks to everyone's help.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question