Solved

Form onload and foucs issues

Posted on 2003-11-03
6
242 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 12 27
performance tune IIS 10 on win 2016 that only runs one website 4 118
Code works but breaks when I add one section 4 20
How to repeat the data 4 17
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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