473,657 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.CanFocu s() 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 8051
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.CanFocu s() 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.ActiveCont rol property.

Me.ActiveContro l = 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/windowsuserinte rface/windowing/dialogboxes/dialogboxrefere nce/
dialogboxmessag es/wm_initdialog.a sp
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.ActiveContro l = 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
1576
by: IMRAN SAROIA | last post by:
Hi! Please advise how to set focus to any control on Web and Windows Forms. Regards Imran
1
6347
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 the FormLoad event, but the form still comes up with the focus on the tab for the first tab page. Anyone know how to do this? Here is what I tried (but simplified)... ----------------
3
8311
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 are a couple of people selling components... but that is not really an option for me... So, I took the plunge and modified the "WebUIValidation.js" file to make it happen... After tracing through file, I figure it out. It was actually pretty...
3
5618
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 then cancel button then database selection combo box then user name text box, then back to password text box) now the password, user name and database entry's are in a group box frame. the buttons (Ok and Cancel) are outside the group frame. Now...
3
6746
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 following default properties: Height = 32px, Width = 144px. I declare the Width property in my control as... \\\
9
2809
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 System.EventArgs) Handles Me.Load Me.FormView1.Row.FindControl("TextBox1").Focus() End Sub
0
1016
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 enter button when a certain textbox has the focus. These two would help me tremendously...
7
30353
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 focus to the Form using command frmName.SetFocus in Form_Paint event, however with no effect. The only successful way I think of to set focus to Form is to .Enable = False and .Enable = True all controls in Form_Paint event. This works fine, but...
4
4047
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 user looking at window A clicks the "open window B" link, I want
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.