Click here to Skip to main content
15,663,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m doing a windows application.In that i want focus the control by pressing enter key. For example, Suppose my application having 5 text boxes. Also i put 5 check boxes near that text boxes. I checked 2nd,4th and 5th check boxes and i saved it. Next time of running, the focus should be in 2nd check box and after pressing enter key the focus should go to 4th text box.
how to do this?
pls help me.
thanks....
Posted
Comments
Braj_12 25-Oct-13 4:52am    
Why Should on 4th TextBox....Please explain little bit more your problem.
vineeth raju 25-Oct-13 4:56am    
i checked 4th check box so focus should be their in 4th text box
Braj_12 25-Oct-13 4:58am    
So,u mean to say, focus should go on to the next selected checkbox's textbox..
vineeth raju 25-Oct-13 5:02am    
yes but the condition is , the check box must checked other wise it should not go.
vineeth raju 25-Oct-13 5:00am    
focus should go, which all check box i have checked, if i uncheck one check box then the focus will not go their. got it?

1 solution

This is a bad idea.
It's possible - and not even that difficult - but that doesn't mean you should do it.

ENTER in windows normally means "I've finished entering by data - save it", and TAB means "I've finished entering this bit - move on". If you subvert that by co-opting the ENTER key to move from field to field, then your application doesn't work like 90% of other apps, and it can confuse your users, or they can even refuse to use it.

You would be a lot better advised to use TAB instead, and just set the tab-order appropriately, checking the checkboxes when you get the TextBox.Enter event to see it you should skip that field.
It's easier to implement, and doesn't confuse users so much.
 
Share this answer
 
Comments
vineeth raju 25-Oct-13 5:32am    
ok. Then how can i use here TAB instead of Enter? pls give me the code also
OriginalGriff 25-Oct-13 5:52am    
If you set the tab order (use the VS button, it's on the Layout toolbar - mouse over tooltips will show you which one it is) then TAB will logically move the input from control to control in a neat flow: left to right, then down is normal and expected.
If you then handle the Enter event for each of your TextBoxes, you will get that method called when that control becomes the focussed event - I.e. the user has but the input there via TAB or another means. You can check the appropriate CheckBox and see if it is allowed, and if not use Control.Focus to move to the next TextBox in sequence.

But an easier (and more user friendly way) is to handle the CheckBox.CheckChanged event, and set the appropriate TextBox.Enabled to false if it's box is not checked. That way, the system will handle not letting him in, and he gets a visual cue as to one of the effects of unchecking the box. Combined with the Tab order, this is often the best method!
vineeth raju 25-Oct-13 6:03am    
if (e.KeyData == Keys.Return && checkBox2.Checked == true)
{
textBox2.Focus();
}
now i am using this code. This code is working but, if i have large data it is not possible to give all text boxes. so i need an easy way to do this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900