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

I have a tabcontrol on which I am hosting forms under tab pages. There are controls on the form such as button and textboxes.

Lets assume there are 2 tab pages opened in tab control.
Also lets say there are 4 controls on a form in tab-page-1 and 4 controls on form in tab-page-2.

User is at tab-page-1 and the focus is on Control-3. Now user switches to tab-page-2 and enters some inputs in control 1 and focuses on control 2.
Now, user switches to tab-page-1. Remember, on tab-page-1, focus was on Control-3. But, as soon as switching is done, focus re-shifts to Control-1 on tab-page-1.
Also, if now you switch back to tab-page-2 where focus was at control-2, the focus is now at control-1.

I want to remain the focus at control where user left while switching between tabs.

Any helps and suggestion is deeply accepted.

Thank you!

What I have tried:

I tried handling various events of tab-control as well as tab-pages and form contained within such as LostFocus, Deselecting, Deselected, Selecting, Selected, Leave, Enter and more but none works.
All events fire post focus lost.

EDIT frim Chill60. I've given the OP the solution but they're not convinced. Bumping this to see if anyone has a better idea
Posted
Updated 16-May-18 15:33pm
v2
Comments
CHill60 14-May-18 5:04am    
Try capturing the tabindex as you enter each control on the tabcontrol. Then on SelectedIndexChanged method for the tab control manually set the focus to the appropriate control
Member 11040029 15-May-18 4:06am    
hello chill...that's the question only..how to capture the tabindex/control. I am finding no event that can do this.
CHill60 15-May-18 4:13am    
Each control has an Enter event - You have to do the rest (identify the "current" control for this tab, potentially store a reference per tab, give the control focus if the tab is reselected)
Member 11040029 15-May-18 10:43am    
yes I do know that but what happens is as soon as you try to change tab, tabcontrol shifts focus to very first control on the page internally, and then navigates to next tabpage. Thus, each time reference changes.
CHill60 15-May-18 15:16pm    
How about the leave event then

1 solution

A "window" can only "focus" on one control at a time.

Tabs gets "activated" and "deactivated"; and in the process, controls gain and lose focus.

If you want to "track" focus, you need to maintain a "variable" for each tab that contains a reference to the last control (on that tab) to have focus.

You can set this variable by having a "got focus" event for each eligible control (on a given tab) and setting that variable when a control gains focus.

(You "may" also have access on the current "active control" (for the tab / container) and determine current focus that way).

When the Tab is (re)activated / selected, test the variable and set focus if it has a reference / control name.
 
Share this answer
 
Comments
CHill60 17-May-18 3:37am    
:laugh: Essentially what I've already explained to the OP but they are struggling to get it to work apparently

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