Solved

Form onload and foucs issues

Posted on 2003-11-03
6
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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