Solved

Set Focus on Textbox Upon Form load

Posted on 2006-11-16
18
8,426 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
  • 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
 
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 85

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 85

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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now