473,412 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 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 8024
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: IMRAN SAROIA | last post by:
Hi! Please advise how to set focus to any control on Web and Windows Forms. Regards Imran
1
by: Rachel Suddeth | last post by:
I have an form where the whole display is a tab control (well, that plus a status bar.) I want to set the focus to the first TextBox on the first TabPage when it loads. I tried to put that into...
3
by: Steve Yerkes | last post by:
There seems to be way too much confusion over how to set focus on the a field using a field validator. I looked all over the web and found people trying to do this, but not getting anywhere. There...
3
by: Brian Henry | last post by:
Hi, I have a bunch of forms in a DLL that I call from my main application. The one form is a login form, and I have the tab order set in the order I want it (password box first, then ok button...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
9
by: Dick | last post by:
How do I set the focus to a control that is a child to a FormView? I've tried the obvious (below) and lots of variations but none seam to work! Sub Page_Load(ByVal sender As Object, ByVal e As...
0
by: jonefer | last post by:
Can someone provide an example (vb.net) of how I can 1. set focus to a textbox during other events other than load for example - after clicking a certain button. 2. set a corresponding default...
7
by: Pygmalion | last post by:
Hello experts, I want to use Form_Keydown event in my program. However, it turns out that one of the form controls is always in focus, so Form_Keydown event is never invoked. I tried to set...
4
by: Jason | last post by:
Hi, Here's the scenario: I have a web application that has window A and window B. A user has both window A and B open - window A is in the foreground and window B is behind it. If the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.