Solved

Focus method working only sporadically

Posted on 2004-10-04
7
191 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

16 Experts available now in Live!

Get 1:1 Help Now