Solved

Focus method working only sporadically

Posted on 2004-10-04
7
190 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
Comment Utility
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
Comment Utility
How do you have the tab order setup for the form?

Bob
0
 
LVL 10

Expert Comment

by:123654789987
Comment Utility
If u want the form to have the focus on textbox when activated the tab index of the textbox should be 1
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 20

Accepted Solution

by:
TheAvenger earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

12 Experts available now in Live!

Get 1:1 Help Now