Re: Strange behavior when automatically setting the focus to a checkbox by Morten
Morten
Wed Apr 28 03:45:21 CDT 2004
On 28 Apr 2004 01:12:13 -0700, SJ <suzannejamesuk2003@yahoo.co.uk> wrote=
:
> 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 =3D 3 Then
> CheckBox1.Focus()
> End If
> End Sub
>
You should change it to if >=3D 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 te=
xt =
since the next change would be 4 characters. If you don't want to handl=
e =
key events to cancel characters above the allowed length you need to set=
=
MaxLength
TextBox2_TextChanged handles TextBox3's TextChanged event? Not that the=
re =
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 TextChang=
ed =
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 descripti=
on.
-- =
Happy coding!
Morten Wennevik [C# MVP]