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

RE: Control Focus issues by v-yiy

v-yiy
Wed Jan 07 03:42:47 CST 2004

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.


RE: Control Focus issues by mklapp

mklapp
Wed Jan 07 07:45:05 CST 2004

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

>-----Original Message-----
>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.
>
>.
>

Re: Control Focus issues by Kevin

Kevin
Wed Jan 07 07:57:33 CST 2004

mklapp wrote:

> 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


Re: Control Focus issues by mklapp

mklapp
Wed Jan 07 16:01:18 CST 2004

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

>-----Original Message-----
>mklapp wrote:
>
>> 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
>
>.
>

RE: Control Focus issues by v-yiy

v-yiy
Wed Jan 07 21:40:10 CST 2004

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.