Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After loading the screen fields with the appropriate values, I focus on the first field on the screen.

However, the focus winds up on one of my buttons. (Even though, I never tell the program to do so.)

During debugging, I noticed the return value of my field.Focus() statement--is FALSE.

This means, either: 1. The program is NOT going to focus on the field that I specified (which it doesn't); 2. The program can NOT focus on the field that I specified--for some reason. Unfortunately, since the program doesn't bomb--there's really no way for me to tell.

Is there an ALTERNATIVE WAY to focus a field--and actually have the field FOCUSED?

What I have tried:

Using FOCUS() for the field.
(I also use SELECT()--and although this works, it doesn't actually FOCUS on the field, the way that I'd like.)
Posted
Updated 21-Jun-22 9:45am
Comments
0x01AA 21-Jun-22 14:13pm    
Is this Windows Forms?

Have you tried setting the Tab Order?
Set tab order of controls - Windows Forms .NET | Microsoft Docs[^]
If the tab order isn't changed, then the first input control you added to your form will get the focus set to it when the form loads.

Also check that the control you are calling Focus on is definitely the button, and that you are calling it in the Shown event rather than the Load or Form constructor.
 
Share this answer
 
Just made a test with Windows Forms...

Does _not_ set focus on the button of my choice:
private void FormMain_Load(object sender, EventArgs e)
{
    // No sucess
    buttonTestHttpClient.Focus();
}
This works and sets the focus on the button of my choice:
private void FormMain_Load(object sender, EventArgs e)
{
    // Sucess, also with e.g. a Textbox.
    this.ActiveControl = buttonTestHttpClient;
}

[Edit] Works also for me
private void FormMain_Load(object sender, EventArgs e)
{
    // Sucess, also with e.g. a Textbox.
    buttonTestHttpClient.Select();
}

[Edit 1]
See also here, especally the 'Note': Control.Focus Method (System.Windows.Forms) | Microsoft Docs[^]

Note

Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.

I hope it helps.
 
Share this answer
 
v5
You might have set a button as the default button in the designer.
See: Designate a Button as the Accept Button Using the Designer - Windows Forms .NET Framework | Microsoft Docs[^]
 
Share this answer
 
Comments
0x01AA 21-Jun-22 16:02pm    
But that works only for Buttons and what I understand from the question, it is more 'InputControl' related...?
RickZeeland 22-Jun-22 2:19am    
He mentions: "However, the focus winds up on one of my buttons"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900