setting default focus

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

I want to set a control in a form to have the default focus.

This is not the accept button -- that's set to a button when is
'clicked' when you press ENTER. I want a textbox to have the focus
when the form is loaded. The textbox cannot be the first in the Tab
order, since it's not at the top of the window, and it seems silly to
start the tab order at it just to fix this issue. I cannot call
control.Focus() from Form1_Load() since doesn't work, even though the
form claims to be visible at this time. I also cannot give it focus
from Form1_Activated(), either. In both cases control.CanFocus() is
False.

Why is this so difficult? It seems the form should have a
DefaultControl method along with its AcceptButton and CancelButton
methods.

Any help / suggestions are appreciated.

Zytan.
 
I want to set a control in a form to have the default focus.
when the form is loaded. The textbox cannot be the first in the Tab
order, since it's not at the top of the window, and it seems silly to
start the tab order at it just to fix this issue. I cannot call
Why is this so difficult? It seems the form should have a
DefaultControl method along with its AcceptButton and CancelButton

Idea of TabIndex=0 is just that.


If you want another approach, use form's Shown event and set focus
there.
 
Josip,

It just seems improper to assume TabIndex=0 is the default focus.
Because you set the tab order based on the layout of the form's
controls. And it's easiest to do this when starting in the top-left
corner. Many forms will have a control not in the top-left corner as
the default focus, so there should be a way to set (easily) without
need to make it tabindex = 0.

In any case, the form's Shown event works great! Thanks!

Zytan.
 
... I cannot call
control.Focus() from Form1_Load() since doesn't work, even though the
form claims to be visible at this time. I also cannot give it focus
from Form1_Activated(), either. In both cases control.CanFocus() is
False.

The help for the Focus() method says:
"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."

So, I tried to use the Select() method in Form1_Load, and it WORKS!

Zytan
 
Zytan,

Based on your text, should you not ask yourself if you have designed your
form well.
It just seems improper to assume TabIndex=0 is the default focus.
Because you set the tab order based on the layout of the form's
controls. And it's easiest to do this when starting in the top-left
corner. Many forms will have a control not in the top-left corner as
the default focus, so there should be a way to set (easily) without
need to make it tabindex = 0.
The first controlbox to enter is mostly in the top-left corner and therefore
has TabIndex=0 as Josib so well advised you. (Me fool was searching in a
complete other direction).

Just my thought,

Cor
 
For within Form.Load, I think you can make a particular Control have
input focus (regardless of the tab order) by setting the
Form.ActiveControl property.

Me.ActiveControl = TextBox2

Maybe this will do what you want.

====================
Clay Burch
Syncfusion, Inc.
 
Cor,
Based on your text, should you not ask yourself if you have designed your
form well.

No. The question I should ask is, "How do I make this form behave
intuitively?" I believe I answered it correctly.
I am creating a messaging client. Every messaging client has the
input text field at the bottom with the default focus. I am 100%
certain that my form is designed EXACTLY as it should be.

The question is: Should tab index = 0 dictate the default focus OR
should default focus be set it on load, while starting the tabindex at
the top-leftmost control?

It is very bad idea to start the tab index at a control that is NOT
the top-leftmost control. It is much easier to design the tab index
logically based from the top-left control. It is that simple. In
Form1_Load, call Control.Select() to set the focus desired.

I do not understand where this notion of tabindex=0 to mean default
focus came from. Please read the Win32 WM_INITDIALOG notification
docs:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/
winui/windowsuserinterface/windowing/dialogboxes/dialogboxreference/
dialogboxmessages/wm_initdialog.asp
Return Value: "The dialog box procedure should return TRUE to direct
the system to set the keyboard focus to the control specified by
wParam. Otherwise, it should return FALSE to prevent the system from
setting the default keyboard focus."

The point... it is completely normal to have a dialog that sets
something OTHER than tabindex=0 to be the default focus.

The first controlbox to enter is mostly in the top-left corner...

The key word in that sentence is 'mostly'. Not 'always'.

Zytan
 
Clay,

Ok, I sent this an hour ago, and it still doesn't appear. I got an
error on sending, and I asked to resend, but apparently, it didn't
work. So, I'll try again:
Me.ActiveControl = TextBox2

Maybe this will do what you want.

Yes, this is exactly what I want. But, I already noted this solution
in an above post.

Thanks for your reply, nonetheless!

Zytan
 
Back
Top