?
Solved

Set Focus on Textbox Upon Form load

Posted on 2006-11-16
18
Medium Priority
?
8,433 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 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