473,327 Members | 2,069 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,327 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 3911
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.