Solved

Focus method working only sporadically

Posted on 2004-10-04
7
197 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 20

Accepted Solution

by:
TheAvenger earned 125 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

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!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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 …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

628 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