Giving a TextBox the focus

G

Guest

I need to be able to select a specific TextBox on a WindowsForms DialogBox to
receive the focus from within the Load method. I thought that it would be as
easy as calling the TextBox's Focus method. However, I found that the TextBox
couldn't accept the focus. The CanFocus method returns FALSE and the
CanSelect method returns TRUE.

This is a simple dialog box running under VS2005 RC. The dialog box does
have a default button handler for the OK button, but I don't think this has
anything to do with it. Why can't I set a specific TextBox to have the focus
from within Load?
 
H

Herfried K. Wagner [MVP]

michael said:
I need to be able to select a specific TextBox on a WindowsForms DialogBox
to
receive the focus from within the Load method. I thought that it would be
as
easy as calling the TextBox's Focus method. However, I found that the
TextBox
couldn't accept the focus. The CanFocus method returns FALSE and the
CanSelect method returns TRUE.

Call the control's 'Select' method. Controls can only be focused if they
are visible and enabled.
 
G

Guest

Thanks. Select() worked. Would have thought that Select implied selecting
text in the control, but Select without parameters simply activates the
control. Wonder why Focus() doesn't have the same action.

Michael
 
J

Jeffrey Tan[MSFT]

Hi Michael,

Yes, Focus() and Select() method should can be used to active certain
control. Actually, both these methods internally use SetFocus Win32 API to
set the focus to the control.

However, Focus() method first checked CanFocus property to determine if
this property returns true, if not, it just does nothing.

Then you may be wonder why Control.CanFocus property returns false in
Form.Load event. If we use Reflector to view source code, we will see this:
public bool get_CanFocus()
{
if ((this.window.Handle != IntPtr.Zero) &&
SafeNativeMethods.IsWindowVisible(new HandleRef(this.window,
this.window.Handle)))
{
return SafeNativeMethods.IsWindowEnabled(new
HandleRef(this.window, this.window.Handle));
}
return false;
}

Yes, this property checks 3 conditions:
1. Handle created?
2. Is visible?
3. Is enabled?

Because we created the control ourselves, its handle should have been
created. Also, it should be enabled by default. So the false condition
should be #2. Actually, we can do the manual check ourselves, like this:
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool IsWindowEnabled(IntPtr hWnd);

private void FormTestUI_Load(object sender, System.EventArgs e)
{
MessageBox.Show(IsWindowVisible(this.textBox1.Handle).ToString());
MessageBox.Show(IsWindowEnabled(this.textBox1.Handle).ToString());
}
It will first display false, then with a true, just as we expected.

So the answer for your question is that the TextBox control is not visible
in Load event, so it just returns false for CanFocus property.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Now THAT's an answer! Thanks so much for taking the time. Now I better
understand.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top