/// <summary> /// Sets the tabControl to use the custom drawing mode, /// if the mode or the eventhandler have already been set then /// they are not set again. /// </summary> /// <param name="tabControl">The tabControl to set its custom value.</param> /// <param name="property">A custom behaviour value.</param> /// <param name="value">True if the custom drawing mode has to be set.</param> private static void SetCustomDrawingMode(TabControl tabControl, newPropertiesEnum property, bool value) { if (tabControl != null) { int key = tabControl.GetHashCode(); if (value) { if (!ControlOfCustomDrawingMode.ContainsKey(key)) { tabControl.DrawMode = TabDrawMode.OwnerDrawFixed; tabControl.DrawItem += TabControl_DrawItem; ControlOfCustomDrawingMode.Add(key, new List<newPropertiesEnum>()); } if (!ControlOfCustomDrawingMode[key].Contains(property)) ControlOfCustomDrawingMode[key].Add(property); } else { if (ControlOfCustomDrawingMode.ContainsKey(key)) { if (ControlOfCustomDrawingMode[key].Contains(property)) ControlOfCustomDrawingMode[key].Remove(property); if (ControlOfCustomDrawingMode[key].Count == 0) { tabControl.DrawMode = TabDrawMode.Normal; tabControl.DrawItem -= TabControl_DrawItem; ControlOfCustomDrawingMode.Remove(key); } } } } }
/// <summary> /// Gets the TabPages of the Tab Control. /// </summary> /// <param name="tabControl">The Tab Control to test.</param> /// <returns>The visible Tab pages in the Tab Control.</returns> private static IList<TabPage> GetTabControlPages(TabControl tabControl) { int key = tabControl.GetHashCode(); WeakReference reference = null; if (pagesCache.ContainsKey(key)) reference = pagesCache[key]; if (reference == null) pagesCache[key] = (reference = new WeakReference(null)); IList<TabPage> result = (IList<TabPage>)reference.Target; if (result == null) { reference.Target = result = new List<TabPage>(); if (VisibleAffected(tabControl)) foreach (KeyValuePair<TabPage, bool> pageEntry in TabsVisible[tabControl.GetHashCode()]) result.Add(pageEntry.Key); else foreach (TabPage page in tabControl.TabPages) result.Add(page); } return result; }
/// <summary> /// Process a UseMnemonic property for a tabControl when this was delayed. /// </summary> /// <param name="tabControl">The TabControl to process.</param> private static void ProcessDelayedUseMnemonic(TabControl tabControl) { bool value = (bool)newProperties[tabControl.GetHashCode()][newPropertiesEnum.UseMnemonic]; Form parentForm = tabControl.FindForm(); int code = parentForm.GetHashCode(); if (value) { parentForm.KeyPreview = true; parentForm.KeyDown += TabControl_ParentForm_KeyDown; if (!FormsWithTabsControlsUsingMnemonic.ContainsKey(code)) { FormsWithTabsControlsUsingMnemonic.Add(code, new List<TabControl>()); FormClosedEventHandler handler = new FormClosedEventHandler(delegate(object sender, FormClosedEventArgs e) { FormsWithTabsControlsUsingMnemonic.Remove(code); parentForm.KeyDown -= TabControl_ParentForm_KeyDown; }); parentForm.FormClosed += handler; } FormsWithTabsControlsUsingMnemonic[code].Add(tabControl); } else { parentForm.KeyPreview = false; parentForm.KeyDown -= TabControl_ParentForm_KeyDown; if (FormsWithTabsControlsUsingMnemonic.ContainsKey(code)) { if (FormsWithTabsControlsUsingMnemonic[code].Contains(tabControl)) FormsWithTabsControlsUsingMnemonic[code].Remove(tabControl); if (FormsWithTabsControlsUsingMnemonic[code].Count == 0) FormsWithTabsControlsUsingMnemonic.Remove(code); } } }
/// <summary> /// Used instead of SelectedIndex when SetTabVisible has been used previously in the TabCtrl. /// Using SetTabVisible in a TabCtrl may return incorrect values in TabCtrl.SelectedIndex. /// </summary> /// <param name="TabCtrl">The Tab Control to test.</param> public static int GetSelectedTabIndex(TabControl TabCtrl) { int index = TabCtrl.SelectedIndex; UpdateTabsVisible(TabCtrl); if (index != -1) { index = TabsVisible[TabCtrl.GetHashCode()].IndexOf(new KeyValuePair<TabPage, bool>(TabCtrl.SelectedTab, true)); if (index == -1) index = TabsVisible[TabCtrl.GetHashCode()].IndexOf(new KeyValuePair<TabPage, bool>(TabCtrl.SelectedTab, false)); } return index; }
/// <summary> /// Checks if the tabControl is controlled by the newProperties Dictionary. /// </summary> /// <param name="tabControl">The tab control to test.</param> private static void CheckNewProperties(TabControl tabControl) { if (!newProperties.ContainsKey(tabControl.GetHashCode())) { newProperties[tabControl.GetHashCode()] = new Dictionary<newPropertiesEnum, object>(); } }
/// <summary> /// Sets the value for the property ActiveTabFontStyle. /// </summary> /// <param name="tabControl">The tab control to set.</param> /// <param name="value">The value to be set.</param> private static void Static_SetActiveTabFontStyle(TabControl tabControl, ActiveTabFontStyleEnum value) { if (CheckForProperty(tabControl, newPropertiesEnum.ActiveFontStyle)) { newProperties[tabControl.GetHashCode()][newPropertiesEnum.ActiveFontStyle] = value; if (value != ActiveTabFontStyleEnum.Default) SetCustomDrawingMode(tabControl, newPropertiesEnum.ActiveFontStyle, true); } }
/// <summary> /// Internal function to update the TabsVisible for the TabCtrl. /// </summary> /// <param name="TabCtrl">The Tab Control to update.</param> private static void UpdateTabsVisible(TabControl TabCtrl) { //The tabControl is not in the list of control yet if (!TabsVisible.ContainsKey(TabCtrl.GetHashCode())) { TabsVisible.Add(TabCtrl.GetHashCode(), new List<KeyValuePair<TabPage, bool>>()); for (int i = 0; i < TabCtrl.TabPages.Count; i++) { TabsVisible[TabCtrl.GetHashCode()].Add(new KeyValuePair<TabPage, bool>(TabCtrl.TabPages[i], true)); } } }
/// <summary> /// Used instead of SelectedIndex when SetTabVisible has been used previously in the TabCtrl. /// Using SetTabVisible in a TabCtrl may return incorrect values in TabCtrl.SelectedIndex. /// </summary> /// <param name="TabCtrl">The Tab Control to test.</param> /// <param name="index">The Tab index.</param> public static void SetSelectedTabIndex(TabControl TabCtrl, int index) { UpdateTabsVisible(TabCtrl); if (!GetTabVisible(TabCtrl, index)) throw new InvalidOperationException("Invalid property value"); TabCtrl.SelectedTab = TabsVisible[TabCtrl.GetHashCode()][index].Key; }
/// <summary> /// Sets the caption of a tab page. /// </summary> /// <param name="TabCtrl">The TabControl to use.</param> /// <param name="index">The index of the tab.</param> /// <param name="caption">The caption to set.</param> public static void SetTabCaption(TabControl TabCtrl, int index, string caption) { List<KeyValuePair<TabPage, bool>> list; TabsVisible.TryGetValue(TabCtrl.GetHashCode(), out list); if (list == null) { TabCtrl.TabPages[index].Text = caption; } else { list[index].Key.Text = caption; } }
/// <summary> /// Sets the current visible status of a tab into a tabcontrol. /// </summary> /// <param name="TabCtrl">The Tab Control to test.</param> /// <param name="index">The Tab index.</param> public static bool GetTabVisible(TabControl TabCtrl, int index) { UpdateTabsVisible(TabCtrl); if ((index < 0) || (index >= TabsVisible[TabCtrl.GetHashCode()].Count)) throw new Exception("Invalid property array index"); return TabsVisible[TabCtrl.GetHashCode()][index].Value; }
/// <summary> /// Selects the specified tab in the TabControl. /// </summary> /// <param name="TabCtrl">The tab control to use.</param> /// <param name="index">The index of the tab to select.</param> public static void SetSelectedIndex(TabControl TabCtrl, int index) { List<KeyValuePair<TabPage, bool>> list; TabsVisible.TryGetValue(TabCtrl.GetHashCode(), out list); if (list == null) { TabCtrl.SelectedIndex = index; } else { // If the tab is invisible, throw an exception. if (!list[index].Value) { throw new Exception("Run-time error '380':\r\n\r\nInvalid property value"); } TabCtrl.SelectedTab = list[index].Key; } }
/// <summary> /// Gets the current status of a tab into a tabcontrol. /// </summary> /// <param name="TabCtrl">The tab control to test.</param> /// <param name="index">The tab index.</param> public static bool GetTabEnabled(TabControl TabCtrl, int index) { if (TabsDisabled.ContainsKey(TabCtrl.GetHashCode())) { return !TabsDisabled[TabCtrl.GetHashCode()].Contains(index); } return true; }
/// <summary> /// Gets the number of tab pages in a TabControl. /// </summary> /// <param name="TabCtrl">The TabControl to test.</param> /// <returns>The number of tabs in the TabControl.</returns> public static int GetTabCount(TabControl TabCtrl) { List<KeyValuePair<TabPage, bool>> list; TabsVisible.TryGetValue(TabCtrl.GetHashCode(), out list); if (list == null) { return TabCtrl.TabPages.Count; } else { return list.Count; } }
/// <summary> /// Gets the caption of a tab in a TabControl. /// </summary> /// <param name="TabCtrl">The TabControl to use.</param> /// <param name="index">The index of the tab.</param> /// <returns>The caption of the specified tab.</returns> public static string GetTabCaption(TabControl TabCtrl, int index) { List<KeyValuePair<TabPage, bool>> list; TabsVisible.TryGetValue(TabCtrl.GetHashCode(), out list); if (list == null) { return TabCtrl.TabPages[index].Text; } else { return list[index].Key.Text; } }
/// <summary> /// Gets the value for the property ActiveTabFontStyle. /// </summary> /// <param name="tabControl">The tab control to test.</param> /// <returns>The current value for ActiveTabFontStyle property.</returns> private static ActiveTabFontStyleEnum Static_GetActiveTabFontStyle(TabControl tabControl) { if (CheckForProperty(tabControl, newPropertiesEnum.ActiveFontStyle)) return (ActiveTabFontStyleEnum)newProperties[tabControl.GetHashCode()][newPropertiesEnum.ActiveFontStyle]; return ActiveTabFontStyleEnum.Default; }
////////////////////////////// //// USEMNEMONIC PROPERTY //// ////////////////////////////// /// <summary> /// Enables/Disables a Tab. /// </summary> /// <param name="TabCtrl">The TabCtrl to be enabled.</param> /// <param name="index">The TabControl index.</param> /// <param name="value">Indicates if enable/disable TabControl.</param> public static void SetTabEnabled(TabControl TabCtrl, int index, bool value) { IList<int> lstDisabled; if (TabsDisabled.ContainsKey(TabCtrl.GetHashCode())) lstDisabled = TabsDisabled[TabCtrl.GetHashCode()]; else { lstDisabled = new List<int>(); TabsDisabled.Add(TabCtrl.GetHashCode(), lstDisabled); TabCtrl.Selecting += TabControl_Selecting; SetCustomDrawingMode(TabCtrl, newPropertiesEnum.TabEnabled, true); TabCtrl.EnabledChanged += TabCtrl_EnabledChanged; } //Tab is being enabled so it must be eliminated from the list if ((value) && (lstDisabled.Contains(index))) lstDisabled.Remove(index); //Tab is being disabled so it must be added if necessary if ((!value) && (!lstDisabled.Contains(index))) { lstDisabled.Add(index); GetTabControlPages(TabCtrl)[index].ForeColor = Color.Green; } TabsDisabled[TabCtrl.GetHashCode()] = lstDisabled; TabCtrl.Refresh(); }
/// <summary> /// The static implmentation of GetUseMnemonic for internal use. /// </summary> /// <param name="tabControl">The TabControl to enable the property.</param> /// <returns>True if the TabControls have to set the UseMnemonic property set.</returns> private static bool Static_GetUseMnemonic(TabControl tabControl) { bool needsUpdateForced = ((!newProperties.ContainsKey(tabControl.GetHashCode())) || (!newProperties[tabControl.GetHashCode()].ContainsKey(newPropertiesEnum.UseMnemonic))); bool res = false; if (CheckForProperty(tabControl, newPropertiesEnum.UseMnemonic)) res = (bool)newProperties[tabControl.GetHashCode()][newPropertiesEnum.UseMnemonic]; if (needsUpdateForced) SetCustomDrawingMode(tabControl, newPropertiesEnum.UseMnemonic, res); return res; }
/// <summary> /// Sets the current visible status of a tab into a tabcontrol. /// </summary> /// <param name="TabCtrl">The Tab Control to set.</param> /// <param name="index">The Tab index.</param> /// <param name="value">The Visible value being set.</param> public static void SetTabVisible(TabControl TabCtrl, int index, bool value) { UpdateTabsVisible(TabCtrl); List<KeyValuePair<TabPage, bool>> visibleTabs = TabsVisible[TabCtrl.GetHashCode()]; if ((index < 0) || (index >= visibleTabs.Count)) throw new Exception("Invalid property array index"); KeyValuePair<TabPage, bool> tabPageToChange = visibleTabs[index]; if (tabPageToChange.Value != value) { //Set invisible by removing it from the TabControl if (!value) { visibleTabs[index] = new KeyValuePair<TabPage, bool>(tabPageToChange.Key, value); TabCtrl.TabPages.Remove(tabPageToChange.Key); } else { //Set visible by checking its position and inserting it if necessary int expectedPosition = 0; visibleTabs[index] = new KeyValuePair<TabPage, bool>(tabPageToChange.Key, value); for (int i = 0; i < index; i++) { if (visibleTabs[i].Value) { expectedPosition++; } } TabCtrl.TabPages.Insert(expectedPosition, tabPageToChange.Key); } TabCtrl.Visible = (TabCtrl.TabPages.Count > 0); } }
/// <summary> /// The static implementation of SetUseMnemonic, for internal use. /// </summary> /// <param name="tabControl">The TabControl to enable the property.</param> /// <param name="value">The value to be set.</param> /// <param name="OnInitialization"></param> private static void Static_SetUseMnemonic(TabControl tabControl, bool value, bool OnInitialization) { int key = tabControl.GetHashCode(); bool needsUpdateForced = (!newProperties.ContainsKey(key) || !newProperties[key].ContainsKey(newPropertiesEnum.UseMnemonic)); if (CheckForProperty(tabControl, newPropertiesEnum.UseMnemonic)) { if (needsUpdateForced || (((bool)newProperties[key][newPropertiesEnum.UseMnemonic]) != value)) { newProperties[key][newPropertiesEnum.UseMnemonic] = value; if (value) SetCustomDrawingMode(tabControl, newPropertiesEnum.UseMnemonic, true); else SetCustomDrawingMode(tabControl, newPropertiesEnum.UseMnemonic, false); if (OnInitialization) DelayedSetUseMnemonic.Add(tabControl); else ProcessDelayedUseMnemonic(tabControl); } } }
/// <summary> /// Check if the property 'newPropertiesEnum' is already defined for this tabcontrol. /// </summary> /// <param name="tabControl">The tab control to test.</param> /// <param name="prop">The property to check.</param> /// <returns>True if property could be checked.</returns> private static bool CheckForProperty(TabControl tabControl, newPropertiesEnum prop) { if (tabControl == null) return false; CheckNewProperties(tabControl); if (!newProperties[tabControl.GetHashCode()].ContainsKey(prop)) newProperties[tabControl.GetHashCode()][prop] = GetDefaultValueForProperty(prop); return true; }
/// <summary> /// Indicates whether the given TabControl object has been affected by a /// SetTabVisible operation. /// </summary> /// <param name="tabControl">The TabControl object to test for.</param> /// <returns><c>true</c> if at least one tab page of the control has been made invisible.</returns> private static Boolean VisibleAffected(TabControl tabControl) { return TabsVisible.ContainsKey(tabControl.GetHashCode()); }
/// <summary> /// Gets the index of the selected tab. Unlike in VB6, if /// no tabs are visible, it will return -1 instead of the /// index of the last visible tab. /// </summary> /// <param name="TabCtrl">The TabControl to use.</param> /// <returns>The index of the selected tab.</returns> public static int GetSelectedIndex(TabControl TabCtrl) { List<KeyValuePair<TabPage, bool>> list; TabsVisible.TryGetValue(TabCtrl.GetHashCode(), out list); if (list == null) { return TabCtrl.SelectedIndex; } else { return GetPageIndex(TabCtrl, TabCtrl.SelectedTab); } }