473,372 Members | 1,238 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Strange behavior when automatically setting the focus to a checkbox

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
Nov 20 '05 #1
4 3915
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]
Nov 20 '05 #2
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
Nov 20 '05 #3
On 28 Apr 2004 01:12:13 -0700, SJ <su****************@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 = 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.

--

Happy coding!
Morten Wennevik [C# MVP]
Nov 20 '05 #4
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
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: swp | last post by:
I have a site created using ASP, HTML, and JavaScript. I use frames to manage a few things, and requires SSL to connect. The site is on a secured network, not meant to be cross-browser...
5
by: MGB | last post by:
I have a strange performance question hopefully someone can clarify for me. I take a production database and make a copy of it, called test, on the same instance on the same server both running at...
1
by: David Wake | last post by:
I have two radio buttons and two checkboxes in a form. I'm trying to write some code so that when a radio button is selected, its corresponding checkbox is disabled. My code looks like this: ...
1
by: Adi | last post by:
I'm trying to do default focus on checkbox,its work fine but the problem is that the user cannot know where is the focus (there isn't visual sign and on the other hand if you press on the tab...
0
by: Richard Hollenbeck | last post by:
I've asked this question before but over a month later I still don't have an answer. A few people did try to help and I am thankful, but I really didn't get the problem solved. I'll try to ask...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
0
by: Andrew.Raymond | last post by:
I know this one is WAY out pushing the envelope, but I'd like to know if anyone has any ideas about it. We have a behemoth of a legacy MFC application which is now being embedded in a Windows...
8
by: Brad Isaacs | last post by:
Good morning friends, I am working with Visual Studio 2005, ASP.NET 2.0 I am working with the Login controls provided my .NET 2.0, trying to make the Login1 control UserName textbox obtain...
2
by: angi35 | last post by:
I hope this is an easy question for someone out there. In Access 2000…I have a MainForm with a tab control (MAIN TABS) with 7 tabs. Within each tab is a SubForm. Within each SubForm is a tab...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.