Problems with Control.Focus Method

AndyMC

Newcomer
Joined
Feb 12, 2003
Messages
10
Hello there,

I have trouble with the Control.Focus method.

When I try to set the input focus on a textBox by using
Visual Basic:
myTextbox.Focus()
I get a "false" back and no focus is being set.

The help explains:
"A control can be selected and receive input focus if all of the following are true: its ControlStyles.Selectable style bit is set to true, it is contained in another control, and all its parent controls are both visible and enabled."

So how can I ensure all the above mentioned conditions in a smart way?

Does anybody know?

Thanks,
Andy
 
Hi Robby, thanks for your answer.

As the parent of the textbox is a user control. I tried the following code in the constructor of the user control (after initialize components):

Visual Basic:
        Me.Visible = True
        Me.Enabled = True
        Me.txtName.Visible = True
        Me.txtName.Enabled = True
        Me.txtName.Focus()

But that didn't work too.

As my textbox "txtName" is not a textbox but an extended control derived from Forms.Textbox I could imagine an overloaded "Focus"-method in my extendedTextbox where automatically everything will be done what has to be done (enable and make visible all parent controls). But how would I achieve that "automatically"?

Any ideas?
 
Unfortunately it makes no difference if constructor or load. It still doesn't work.

Has nobody the same problem???
 
You can't focus a control unless it's visible. Try, in the form's Load event, first showing the form (Me.Show()) and then setting the focus after that.
 
Back
Top