Method Focus()

  • Thread starter Thread starter Christian Pické
  • Start date Start date
C

Christian Pické

Hi,

I want to set the focus to a specific textbox. When using somthing like
textbox1.Focus() it doesn't do anything. Am I missing something here?

Regards,

Christian
 
Hi,
It should work. Are you by any chance traping the 'Enter' focus event?
which in turn is doing something to leave focus?

Cheers
Benny
 
Christian,

Can you give an example where the Focus method of the TextBox class does
not work?
 
I've also many times encountered this problem!

I've set Focus on some control, and breakpoint in the next line said
otherwise!
Couldn't explain it though!
 
Goran,

I still don't understand. What do you mean you breakpoint in the next
line? If you are setting breakpoints after a control has the focus set, and
then you switch to a debugger, then the focus is most definitely lost. This
is behavior I would expect.
 
Christian,

Is it possible that you validatate the controls and the validation fails? In
this case it is possible that the control that fails the validation retains
the focus.
 
Nicholas Paldino said:
Goran,

I still don't understand. What do you mean you breakpoint in the next
line? If you are setting breakpoints after a control has the focus set, and
then you switch to a debugger, then the focus is most definitely lost. This
is behavior I would expect.

OK! You're right with this one! My bad!
But I've looked into debugger only because that control wasn't focused after
I called focus method!
And I came across that problem in few projects!
 
Sorry, none of the above is happening. I am just opening a Windows Form
FormDiscount and have this code:

...
public FormDiscount()
{
InitializeComponent();
textboxReason.Focus();
}
...

When running the program, it's not textboxReason that gets the focus but
the control that has taborder number 1.

I did some simulation here because I encountered the problem on an other
form where some key-trapping is done, so I tried it on FormDiscount
which is a very simple form with 2 labels, 2 textboxes en 2 buttons, but
it still doesn't work!

Christian
 
Hi Christian,

Insted this code use the following
public FormDiscount()
{
InitializeComponent();
this.ActiveControl = textboxReason;
}
 
OK, this one works. Thanks a lot!

But it still doesn't take away my frustration about a method that simply
doesn't work the way it's described (gets AND sets the focus).

Christian
 
Hi Christian,

The problem is that in the constructor CanFocus property returns *false*. It
returns false because the control is not visible yet. Normaly frameworks (at
least some of them ) don't allow focusing invisible windows. So, Focus
method checks CanFocus property and because it is *false* it doesn't do
anything. If it was true the Focus method would call SetFocus API and if
everything goes ok, would set ActiveControl.
However windows allows focusing invisible windows if you try the following
it works.

[DllImport("user32.dll")]
private extern static IntPtr SetFocus(IntPtr hwnd);
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
SetFocus(textBox1.Handle);
}
 

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

Back
Top