Solved

Set Focus on Textbox Upon Form load

Posted on 2006-11-16
18
8,425 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

22 Experts available now in Live!

Get 1:1 Help Now