Strange behavior when automatically setting the focus to a checkbox

S

SJ

Hi all,

I have come across a weird problem when attempting to automatically
set the focus in a vb.net form to a checkbox control...

In my form I have (on a tab page in a tab control) several textboxes
and a checkbox. The behaviour I want from my app is as follows:-

When the textbox (which is prior in tab order to the tab control) has
been filled with a certain length of text by the user, the focus is
then automatically set to the checkbox.

The (basic) code I'm using in the textbox's key press event is :-

If int_NumCharsinTextBox > MaxAllowedLength Then
checkbox1.focus
End If

The bizarre thing is that this just does not work... UNLESS I have
pressed the tab key on the form first! Why is this? If I use above
code to set the focus to another text box instead of a checkbox, then
the focus goes there no problem with out having to press the tabkey
somewhere on the form.

Any points on how to overcome this problem will be gratefully
received. I'm going to be using this code in a few places and I would
like all the controls to behave in a consistent way.

Thanks in advance
Suzanne
 
M

Morten Wennevik

Hi Suzanne,

Pardon me for saying so, but I think that isn't such a good idea.
If the user enters some value, reaches the limit, and then sees he
mistyped a character... it is bound to cause confusion. Then again, if
you are going to use it yourself, or the behaviour is well explaind...

In any case, in C# you could accomplish this by using the TextChanged event

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if(textBox1.Text.Length >= 4)
checkBox1.Focus();
}


Happy coding!
Morten Wennevik [C# MVP]
 
S

SJ

Hi Morten

thank you for replying. The reason I am trying to accomplish the above
is for both ease of data entry for the user and also for data
validation - i.e. to ensure that the user can not physically type in
more characters than are required for say a national insurance number
but anyway.... I think I have not explained my problem clearly
enough....

I don't think it matters what event of the textbox that you put
checkBox1.Focus() in - the problem is that the focus will not go to
the checkbox. **Well actually after further investigation the focus
DOES go to the checkbox but the "focus rectangle" does NOT appear
round the checkbox indicating that the checkbox has the focus**

Try this; put 2 text boxes and a checkbox on a windows form. In the
code for the 2nd textbox put this code

Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox3.TextChanged
If TextBox2.Text.Length = 3 Then
CheckBox1.Focus()
End If
End Sub

Load the form & try entering text in the textbox2 you will see that
you can't enter more than 3 chars but the focus does not appear to go
to the checkbox (no focus rectangle).

Now tab from the 1st textbox to the 2nd. Enter text in textbox 2, now
you will see that once you have more than 3 chars, the focus will now
appear to go to the checkbox i.e. the "focus rectangle" will be
visible round the checkbox. This is my problem - I want the focus
rectangle to appear round the checkbox when the checkbox has the focus
weather the user has used the tab key on the form or not.

Again any advice would be most welcome.

Thank you
Suzanne
 
M

Morten Wennevik

Hi Morten

thank you for replying. The reason I am trying to accomplish the above
is for both ease of data entry for the user and also for data
validation - i.e. to ensure that the user can not physically type in
more characters than are required for say a national insurance number
but anyway.... I think I have not explained my problem clearly
enough....

You could set the textbox' MaxLength property to 3 :)
I don't think it matters what event of the textbox that you put
checkBox1.Focus() in - the problem is that the focus will not go to
the checkbox. **Well actually after further investigation the focus
DOES go to the checkbox but the "focus rectangle" does NOT appear
round the checkbox indicating that the checkbox has the focus**

Try this; put 2 text boxes and a checkbox on a windows form. In the
code for the 2nd textbox put this code

Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox3.TextChanged
If TextBox2.Text.Length = 3 Then
CheckBox1.Focus()
End If
End Sub

You should change it to if >= 3, because people might paste to the
textbox, and if they paste 4 characters they can continue entering more
numbers. Also, if they tab back and forth they can continue entering text
since the next change would be 4 characters. If you don't want to handle
key events to cancel characters above the allowed length you need to set
MaxLength

TextBox2_TextChanged handles TextBox3's TextChanged event? Not that there
is anything wrong with it, as long as you are aware of it :)
Load the form & try entering text in the textbox2 you will see that
you can't enter more than 3 chars but the focus does not appear to go
to the checkbox (no focus rectangle).

Sorry, but for me the checkbox gets a focus rectangle
Now tab from the 1st textbox to the 2nd. Enter text in textbox 2, now
you will see that once you have more than 3 chars, the focus will now
appear to go to the checkbox i.e. the "focus rectangle" will be
visible round the checkbox. This is my problem - I want the focus
rectangle to appear round the checkbox when the checkbox has the focus
weather the user has used the tab key on the form or not.

Um, I was already in the 2nd textbox, the first textbox has no TextChanged
event code.
Again any advice would be most welcome.

Thank you
Suzanne

I'm sorry, but I can't replicate the problem, be it C# or VB. If you
create a fresh project do you still get the same problem? I might have
done a step different or missed something in your step by step description.
 
S

SJ

Hi Morton,

thank you for your persisting help! I've done a bit more searching on
the newsgroups and come across other people who have complained that
they also do not get the focus rectangle on checkboxes. The response
that they got was that the focus rectangle is an accessibility feature
designed for keyboard users & you only get the focus rectangle if you
select the checkbox with a tab key - which explains what I was
experiencing! Apparently changing the keyboard accessibility options
has an affect, so I'm going to try that.

Actually... after this problem I only just noticed that when you
select the checkbox with a mouse that you do not get the focus
rectangle on the checkbox anyway! I guess this is the control's
default behaviour? If so I think that this inconsistency in the
checkboxes "visual response" between selecting it with the mouse or
with the tab key is very confusing to people (esp. as most people use
a combination of the mouse and the keyboard anyway).

Anyway thank you for your help and advice

Suzanne
 

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

Similar Threads


Top