Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Form onload and foucs issues

Posted on 2003-11-03
6
Medium Priority
?
248 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
Industry Leaders: 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 500 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

Technology Partners: 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!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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