| 1 | using System;
|
|---|
| 2 | using System.Windows.Forms;
|
|---|
| 3 | using System.Collections.Generic;
|
|---|
| 4 | using System.Linq;
|
|---|
| 5 | using System.Text;
|
|---|
| 6 | using System.Threading.Tasks;
|
|---|
| 7 |
|
|---|
| 8 | namespace Common
|
|---|
| 9 | {
|
|---|
| 10 | class FocusState
|
|---|
| 11 | {
|
|---|
| 12 | public Control control;
|
|---|
| 13 | public int scrollPosVertical;
|
|---|
| 14 | public int scrollPosHorizontal;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | class TabControlFocus
|
|---|
| 18 | {
|
|---|
| 19 | private List<FocusState> lastFocusedControls = new List<FocusState>();
|
|---|
| 20 | private int focusedTabIndex = 0;
|
|---|
| 21 | public TabControl tabControl = null;
|
|---|
| 22 |
|
|---|
| 23 | private void UpdateListSize()
|
|---|
| 24 | {
|
|---|
| 25 | if (tabControl != null)
|
|---|
| 26 | {
|
|---|
| 27 | while (lastFocusedControls.Count < tabControl.TabPages.Count)
|
|---|
| 28 | lastFocusedControls.Add(new FocusState());
|
|---|
| 29 | while (lastFocusedControls.Count > tabControl.TabPages.Count)
|
|---|
| 30 | lastFocusedControls.RemoveAt(lastFocusedControls.Count - 1);
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | public void SetupTabControl(TabControl tabControl)
|
|---|
| 35 | {
|
|---|
| 36 | this.tabControl = tabControl;
|
|---|
| 37 | tabControl.SelectedIndexChanged += delegate (object sender, EventArgs e)
|
|---|
| 38 | {
|
|---|
| 39 | TabControl parentTabControl = (sender as TabControl);
|
|---|
| 40 | Control control = null;
|
|---|
| 41 | UpdateListSize();
|
|---|
| 42 | if (parentTabControl.SelectedIndex != -1)
|
|---|
| 43 | {
|
|---|
| 44 | control = lastFocusedControls[parentTabControl.SelectedIndex].control;
|
|---|
| 45 | if (control != null)
|
|---|
| 46 | {
|
|---|
| 47 | control.Focus();
|
|---|
| 48 |
|
|---|
| 49 | // Restore strollbars position of nearest parent panel with autoscroll
|
|---|
| 50 | while (control.Parent != null)
|
|---|
| 51 | {
|
|---|
| 52 | if ((control.Parent is Panel) && (control.Parent as Panel).AutoScroll)
|
|---|
| 53 | {
|
|---|
| 54 | (control.Parent as Panel).VerticalScroll.Value = lastFocusedControls[parentTabControl.SelectedIndex].scrollPosVertical;
|
|---|
| 55 | (control.Parent as Panel).HorizontalScroll.Value = lastFocusedControls[parentTabControl.SelectedIndex].scrollPosHorizontal;
|
|---|
| 56 | (control.Parent as Panel).PerformLayout();
|
|---|
| 57 | break;
|
|---|
| 58 | }
|
|---|
| 59 | control = control.Parent;
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | focusedTabIndex = parentTabControl.SelectedIndex;
|
|---|
| 64 | };
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | private void TrackLostFocus(object sender, EventArgs e)
|
|---|
| 68 | {
|
|---|
| 69 | Control item = (Control)sender;
|
|---|
| 70 | Control control = (Control)sender;
|
|---|
| 71 |
|
|---|
| 72 | UpdateListSize();
|
|---|
| 73 | if (!(control is Panel))
|
|---|
| 74 | while (item.Parent != null)
|
|---|
| 75 | {
|
|---|
| 76 | if (item.Parent is TabPage)
|
|---|
| 77 | {
|
|---|
| 78 | int index = tabControl.TabPages.IndexOf(item.Parent as TabPage);
|
|---|
| 79 | if (index != -1)
|
|---|
| 80 | {
|
|---|
| 81 | if (focusedTabIndex == tabControl.SelectedIndex)
|
|---|
| 82 | {
|
|---|
| 83 | lastFocusedControls[index].control = control;
|
|---|
| 84 |
|
|---|
| 85 | // Store scrollbars position of nearest parent panel with autoscroll
|
|---|
| 86 | while (control.Parent != null)
|
|---|
| 87 | {
|
|---|
| 88 | if ((control.Parent is Panel) && (control.Parent as Panel).AutoScroll)
|
|---|
| 89 | {
|
|---|
| 90 | lastFocusedControls[index].scrollPosVertical = (control.Parent as Panel).VerticalScroll.Value;
|
|---|
| 91 | lastFocusedControls[index].scrollPosHorizontal = (control.Parent as Panel).HorizontalScroll.Value;
|
|---|
| 92 | break;
|
|---|
| 93 | }
|
|---|
| 94 | control = control.Parent;
|
|---|
| 95 | }
|
|---|
| 96 | break;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | item = item.Parent;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | public void TrackLostFocusOnChildControls(Control.ControlCollection controls)
|
|---|
| 105 | {
|
|---|
| 106 | foreach (Control control in controls)
|
|---|
| 107 | {
|
|---|
| 108 | control.LostFocus += new EventHandler(TrackLostFocus);
|
|---|
| 109 |
|
|---|
| 110 | Control.ControlCollection childControls = control.Controls;
|
|---|
| 111 | if (childControls != null)
|
|---|
| 112 | TrackLostFocusOnChildControls(childControls);
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|