Skip to content

Instantly share code, notes, and snippets.

@CallumCarmicheal
Created October 28, 2022 15:46
Show Gist options
  • Save CallumCarmicheal/9bbddcd896d97882539ba45655c2179a to your computer and use it in GitHub Desktop.
Save CallumCarmicheal/9bbddcd896d97882539ba45655c2179a to your computer and use it in GitHub Desktop.
Add tooltips to all mattersphere control's, helps with DPI issues on laptops where text is cut off.
#region ToolTip Generation
ToolTip _GlobalToolTip = new ToolTip();
protected void setupToolTipLabels() {
// Loop tabs.
TabPage tp = (TabPage)EnquiryForm.Parent; //Grabs the Tab Page of the Enquiry Form
TabControl tc = (TabControl)tp.Parent;
foreach ( TabPage tbp in tc.TabPages )
_GlobalToolTip_RecursivelyLoopControls( tbp.Text, tbp );
tc.SelectedIndexChanged -= processTabControlIndexChanged;
tc.SelectedIndexChanged += processTabControlIndexChanged;
}
private void processTabControlIndexChanged(object sender, EventArgs args) {
var tc = sender as TabControl;
if (tc == null) return;
var currentPage = tc.SelectedTab;
if (currentPage.Controls.Count == 0) {
currentPage.ControlAdded -= processTabChange_Event;
currentPage.ControlAdded += processTabChange_Event;
} else {
processTabChange(currentPage);
}
}
private void processTabChange_Event(object sender, EventArgs args) {
var tabPage = (TabPage)sender;
if (tabPage == null) return;
var tc = (TabControl)tabPage.Parent;
processTabChange(tc.SelectedTab);
}
private void processTabChange(TabPage currentPage) {
EnquiryForm enqPage = currentPage.Controls[0] as EnquiryForm;
// Failed to cast.
if (enqPage == null) {
return;
}
enqPage.Rendered -= _GlobalToolTip_GeneralEventHandler_REN;
enqPage.Rendered += _GlobalToolTip_GeneralEventHandler_REN;
enqPage.RefreshedControls -= _GlobalToolTip_GeneralEventHandler_RC;
enqPage.RefreshedControls += _GlobalToolTip_GeneralEventHandler_RC;
// Set the tooltip
TabPage tp = (TabPage)EnquiryForm.Parent;
_GlobalToolTip_RecursivelyLoopControls(tp.Text, enqPage);
}
protected void _GlobalToolTip_GeneralEventHandler_REN(object sender, EventArgs args) {
// Rendered, Prefer to use Refreshed Controls
}
protected void _GlobalToolTip_GeneralEventHandler_RC(object sender, EventArgs args) {
EnquiryForm enqPage = sender as EnquiryForm;
if (enqPage == null) return;
TabPage tp = (TabPage)EnquiryForm.Parent;
// Refreshed Controls
_GlobalToolTip_RecursivelyLoopControls(tp.Text, enqPage);
}
protected void _GlobalToolTip_RecursivelyLoopControls(string TabName, System.Windows.Forms.Control parent) {
// Types that can have a label on them.
List<Type> targetTypes = new List<Type>() {
typeof(FWBS.Common.UI.Windows.eBase2)
, typeof(FWBS.Common.UI.Windows.eLabel2)
, typeof(FWBS.Common.UI.Windows.eTextBox2)
, typeof(FWBS.Common.UI.Windows.eCheckBox2)
, typeof(FWBS.Common.UI.Windows.eComboBox2)
, typeof(FWBS.Common.UI.Windows.eComboBox2)
, typeof(FWBS.Common.UI.Windows.eXPComboBox)
, typeof(FWBS.Common.UI.Windows.eMultiTextBox2)
, typeof(FWBS.OMS.UI.Windows.eXPComboBoxCodeLookup)
, typeof(FWBS.OMS.UI.Windows.eCurrency)
};
foreach (Control control in parent.Controls) {
// Add the label
if (targetTypes.Contains(control.GetType()))
_GlobalToolTip.SetToolTip(control, control.Text);
// Check if tab control
else if (control.GetType() == typeof(TabControl)) {
var tc = (TabControl)control;
tc.SelectedIndexChanged -= processTabControlIndexChanged;
tc.SelectedIndexChanged += processTabControlIndexChanged;
if (tc.SelectedTab != null) {
var currentPage = tc.SelectedTab;
if (currentPage.Controls.Count == 0) {
currentPage.ControlAdded -= processTabChange_Event;
currentPage.ControlAdded += processTabChange_Event;
} else {
processTabChange(currentPage);
}
}
}
// Check if Enquiry Tab Control
else if (control.GetType() == typeof(FWBS.OMS.UI.Windows.ucOMSTypeTabs)) {
var omsTC = (FWBS.OMS.UI.Windows.ucOMSTypeTabs)control;
var tc = (TabControl)omsTC.TabControl;
tc.SelectedIndexChanged -= processTabControlIndexChanged;
tc.SelectedIndexChanged += processTabControlIndexChanged;
if (tc.SelectedTab != null) {
var currentPage = tc.SelectedTab;
if (currentPage.Controls.Count == 0) {
currentPage.ControlAdded -= processTabChange_Event;
currentPage.ControlAdded += processTabChange_Event;
} else {
processTabChange(currentPage);
}
}
}
// Attempt to process any children.
else {
this._GlobalToolTip_RecursivelyLoopControls(TabName, control);
}
}
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment