Solved

Form onload and foucs issues

Posted on 2003-11-03
6
239 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

21 Experts available now in Live!

Get 1:1 Help Now