Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Focus method working only sporadically

Posted on 2004-10-04
7
Medium Priority
?
199 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
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
Industry Leaders: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Integration Management Part 2
Suggested Courses

927 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