Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Focus method working only sporadically

Posted on 2004-10-04
7
Medium Priority
?
198 Views
Last Modified: 2010-04-15
When loading a form, I call the Focus() method of a text box so the user can start typing their value in. The first time the form is shown when the program is executed, it works fine. Subsequent times, however, result in the focus being set somewhere else.

Does anyone know if you have to destroy the window in a particular way to get this to work, or how I can guarantee that the Focus() sets the focus every time?

Thanks
0
Comment
Question by:tvanceplus
[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
7 Comments
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 12220403
Put focus code in activate event


            private void Form1_Activated(object sender, System.EventArgs e)
            {
                  this.textBox1.Focus();
            }

Cheers,
S Sansanwal
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12221435
How do you have the tab order setup for the form?

Bob
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12223701
If u want the form to have the focus on textbox when activated the tab index of the textbox should be 1
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
ID: 12224361
Where do you have the Focus() call? If it is in the OnLoad event handler and you hide the form and reshow it, it will work only the first time. So you should either create a new form or move the Focus() call to another event handler. However the suggested Activated event will make also problems as it will be fired also when the application is changed (switch to another application, then come back). Maybe you can use VisibleChanged to set the focus.
0
 
LVL 1

Author Comment

by:tvanceplus
ID: 12241657
So basically, I need to recreate the form every time I display it for the focus to work every time. Can you tell me why the Load doesn't work. It runs all of the rest of the code I put in the load handler. I'll give you (TheAvenger) the points anyway if recreating works, but I'm always on the lookout for new information.

Thanks

Tony
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12241727
"It runs all of the rest of the code I put in the load handler"
and before:
"Can you tell me why the Load doesn't work"

Can you explain a little bit more what you mean? OnLoad is a standard event and it is possible that it also gets lost.

These kind of situations are very rare with VS, so I assume there is something on your forms that causes it. If you find a way to reproduce the loss, you can start removing components from the form until you no longer lose the events. Then you will know what the causes the problem.
0
 
LVL 1

Author Comment

by:tvanceplus
ID: 12251278
I'm sorry for being kind of imprecise with my language. What I meant was, I handle all of the initialization code that needs to be executed for a particular form in the OnLoad handler. All of the code in this handler executes every time ShowDialog() is called. Why is the Focus() method the only method that does not work from this handler? All of my forms behave this way when I use the focus method of a control from the OnLoad handler, and I'm just sort of curious as to why.
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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