Solved

Form onload and foucs issues

Posted on 2003-11-03
6
243 Views
Last Modified: 2010-04-16
hi.

i have a form that i create when my app starts. then when a user right clicks i show it. the first time it pops up the first out of three buttons has focus like it should. each button has a tab index of 1,2,3 repsectively. also, in the form on load i have button.focus();. however, whenever i popup the form for the second+ time the last button that was clicked on is focused, not button one. i dont understand why the focus method isnt setting foucs to button one. how can i achieve this?

thank you.
0
Comment
Question by:jeremiahscott
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9671281
Handle VisibleChanged Event and set focus there:

private void Form_TextChanged(object sender, EventArgs e)
{
    if ( Visible == True )
        button.focus();
}
0
 
LVL 6

Expert Comment

by:purpleblob
ID: 9671288
Are you using ShowDialog or Show ?

Basically it sounds like you are setting the focused item and then calling ShowDialog, this would appear to be setting the focus prior to display but when the Form displays it is restoring the form's state to the state when it was last closed.

What you could do is place the button.Focus() call into the form's (which has the buttons in) Activated event.

Hope this helps
0
 

Author Comment

by:jeremiahscott
ID: 9672020
im still a little confused.
i will have to check tonite if i am using show or show dialog.

what i do know is that from my main form i call show/showdialog. then i set a breakpoint in the onload of the form it is showing. it goes thru there and calls the button1.focus(). i thought that onload was called each time the form is shown, which is seems to be since the break point is hit. i still dont understand why calling button1.focus() doent work there. sorry =(
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 6

Accepted Solution

by:
purpleblob earned 125 total points
ID: 9672368
Okay, just played with this a little more and I think I understand what's actually happening underneath.

As you say the Load event gets called each time the form loads, but the form is not made visible until the Load event completes (i.e. the Load event is called - you do your initialization within it then the method that fired the event makes the form visible). Now it would appear that until the form is made visible setting focus within the Load event will have no effect.

This can be seen by placing the following in the Load event

bool b = textBox1.Focused;
textBox1.Focus();
b = textBox1.Focused;

Even if textBox1 is the first control in the tab order it will not have focus during the Load event at any point. However if you set the form to Visible before setting focus you'll get the desired effect.

i.e.

this.Visible = true;
bool b = textBox1.Focused;
textBox1.Focus();
b = textBox1.Focused;

The following link http://support.microsoft.com/default.aspx?scid=kb;en-us;822493 shows some alternate methods for setting the focus during the load event (it's in VB.NET but pretty self explanatory)

Hope this helps
0
 

Author Comment

by:jeremiahscott
ID: 9672613
thanks for the detailed explination. i understand what is going on now.
0
 
LVL 6

Expert Comment

by:purpleblob
ID: 9672781
Glad to have helped :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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 article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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