Click here to Skip to main content
15,942,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have several textboxes in my app. How to restrict the focus of mouse to other textboxes when one textbox has focus? For example, I have 5 textboxes named txt1, txt2, txt3, txt4 and txt5. When I am editing in txt1 and click the mouse in any of the other textboxes the focus should remain in txt1 itself (Mouse click should be limited to the textbox which has focus). How to accomplish this task? It would be very helpful please if somebody could help me with a code sample.

What I have tried:

Since I am a newbie. I am not sure on how to proceed with this concept.
Updated 17-Nov-23 9:45am

Why not disable all but the TextBox that you want to have focus?

You haven't stated what framework you're using but this can be accomplished in them all!
Share this answer
You're going to find this approach problematic at best. Do you have any menus in your app or other controls? Try clicking a menu while you're "locking" the focus to a textbox. You're going to have to write a bunch of code to see which box has the focus and what the user clicked on to see if that's a valid move. Now go modify your app down the road and you have to update that pile of garbage.

Just disable the textboxes that do not meet the validation requirements.
Share this answer

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