• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8443
  • Last Modified:

Set Focus on Textbox Upon Form load

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
durrence71
Asked:
durrence71
  • 8
  • 4
  • 3
  • +1
1 Solution
 
riyazthadCommented:
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
 
durrence71Author Commented:
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
 
riyazthadCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
HillwaaaCommented:
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
 
durrence71Author Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Is this a converted VB6 app via the wizard?

Does the problem persist in a New project?
0
 
durrence71Author Commented:
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
 
durrence71Author Commented:
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
 
riyazthadCommented:
Dont put in Form Activated event, try to put in paint event. It will work.



0
 
durrence71Author Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.net are you working with?
0
 
HillwaaaCommented:
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
 
durrence71Author Commented:
Visual Basic 2005 Express Edition
0
 
durrence71Author Commented:
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
 
HillwaaaCommented:
Ah - ok - activate method will only work for 2003 projects :(
0
 
HillwaaaCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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
 
durrence71Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 8
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now