Mega Code Archive

 
Categories / Delphi / Hardware
 

Custom Hints For Each Tab in Delphis TTabSet or TTabControl (On Mouse Over Tab)

Title: Custom Hints For Each Tab in Delphi's TTabSet or TTabControl (On Mouse Over Tab) Delphi's TTabSet component presents horizontal tabs users can click to initiate actions. Tab set controls are commonly used to display tabbed pages within a dialog box. As the Help states, the TTabSet is provided backward compatibility. The TTabControl control can also be used to add a control with multiple tab settings to a form. Both TTabSet and TTabControl are a single object (not made up of several pages that contain different controls). When the current tab changes, the tab control must directly update its contents to reflect the change in an OnChange event handler. To create a set of tabs for the tab set control when you specify a list of strings as the value of the Tabs property. How About a Tab Specific Hint? The Hint and ShowHint properties (found in all controls that inherit from TControl) specify whether the control displays a Hint when the mouse pointer rests momentarily on the control (when ShowHint is true). For TTabSet and TTabControl there's only "one" Hint property you can set and this hint will appear for the control - not taking into account the selected tab or alike. If you use tab controls you might need to display different hints when the mouse hovers over a specific tab of the tab control. In the Delphi IDE there's a tab control with 3 tabs "Code", "Design", "History". Would it not be helpful if a hint would appear when you code and hover your mouse over the "Design" tab - for example to "hint" the name of the form you are working on. If you are using tab controls and need to change the hint of the tab control taking into account where (over what) the mouse is, here's how to do it: //handles TTabSet1 OnMouseMove procedure TTabForm.TabSet1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var iPos : integer; begin iPos := TabSet1.ItemAtPos(Point(x,y)); if iPos -1 then TabSet1.Hint := Format('%d. - %s', [iPos, TabSet1.Tabs[iPos]]) else TabSet1.Hint := ''; end; The above code presumes you have a TTabSet with several tabs. In the OnMouseMove the Hint property is changed to provide the name of the tab the mouse is over. The code uses the ItemAtPos method of the TTabControl to get the index of the tab under the mouse. Note that you can display hints in the status bar control (if you have one in your application). If you are working with TTabControl, you can use the next code: //handles TabControl1 OnMouseMove procedure TTabsForm.TabControl1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var iPos : integer; begin iPos := TabControl1.IndexOfTabAt(x,y); if iPos -1 then TabControl1.Hint := Format('%d. - %s', [iPos, TabControl1.Tabs[iPos]]) else TabControl1.Hint := ''; end; The code is much the same as used with TTabSet, only the method to be called is IndexOfTabAt. That's it. Now each tab has its own hint :)