471,071 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

setting default focus

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.

Jan 27 '07 #1
7 7761
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.

--
Greetings,
Josip Medved
http://www.jmedved.com

Jan 27 '07 #2
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.

Jan 27 '07 #3
... 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

Jan 27 '07 #4
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
Jan 28 '07 #5
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.

Jan 28 '07 #6
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/de...y/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

Jan 29 '07 #7
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

Jan 29 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by IMRAN SAROIA | last post: by
1 post views Thread by Rachel Suddeth | last post: by
3 posts views Thread by Brian Henry | last post: by
3 posts views Thread by Marty McFly | last post: by
7 posts views Thread by Pygmalion | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.