Control Focus issues

  • Thread starter Thread starter mklapp
  • Start date Start date
M

mklapp

Hello,

A form with 3 textboxes and a button and the code
below :

Private Sub TextBox2_Enter
fred()
end sub

Private Sub fred()

TextBox3.focus

End

Private Sub TextBox3_enter

console.writeline("Entering")

End

Private Sub TextBox3_Leave

console.writeline("Leaving")
end

Private Sub Button1_Click

TextBox2.focus

End

The intent here is to start with the focus in TextBox1
(no events), click the button. The button changes focus
to TextBox2. The Enter event calls a routine that also
shifts focus, this time to TextBox3.

I would expect the output to be:

Entering

Instead I am getting

Entering
Leaving
Entering

This seems wrong and is causing some issues. In
practice, tabbing to 'TextBox2' takes the place of the
button click, but the end result is the same, a Leave
event at the "wrong" time.

mklapp
 
Hi,

Thanks for your post!

Basically you should avoid setting focus directly in other focus event, it
will cause some problem in underlying message loop.
You may use Control.BeginInvoke to work around this problem.
Here is an sample snippet:
<code>
delegate bool BoolMethodInvoker();

void fred()
{
BeginInvoke(new BoolMethodInvoker(textBox3.Focus));
}
</code>

Does it solve your problem?
If you have anything unclear on it, please be free to reply this thread.


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Hello,

Thanks. This looks good. There are a couple of
questions.

As much as I prefer C# to VB, I must do this in VB and
the way I have done it :

Delegate Function BoolMethodInvoker() as Boolean

Private Sub fred()

BeginInvoke( New BoolMethodInvoker(AddressOf
(textBox3.focus)))

end

I get a 'compile' error. BeginInvoke takes an address
and the above does not do that ("Expression does not
produce a value"

Thanks

mklapp
 
mklapp said:
Delegate Function BoolMethodInvoker() as Boolean

Private Sub fred()

BeginInvoke( New BoolMethodInvoker(AddressOf
(textBox3.focus)))

end

Try:

With New BoolMethodInvoker(AddressOf textbox3.focus)
.BeginInvoke
End With
 
Hello,

I had to change BeginInvoke to Invoke because I did not
have an argument for BeginInvoke (it insists on one). Of
course it did not work the way I wanted it too as it is
still a synchronous call.
I'm reading up on the issue to determine the BeginInvoke
parameters and will pass it on.

mklapp
 
Hi,
This way should work.
<code>
Delegate Function BoolMethodInvoker() As Boolean
Sub fred()
BeginInvoke(New BoolMethodInvoker(AddressOf TextBox3.Focus))
End Sub 'fred
</code>
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Back
Top