Coloring your tabs with the ITEMSTYLE property
Published 19 JUL 2019 at 02:07:47AM
A new facility introduced in version 10 is the ability to set the styling information for the tabs in the TabControl using the new ITEMSTYLE and ITEMSTYLES properties, and in this post we'll explain how to use them.
TabStates and ItemStyles
Each tab in a TabControl can be in one of the following states at runtime:
- Normal (Unselected tab)
- Hot (Mouse is over a Normal tab)
- Disabled
- Selected
- Hot Selected (Mouse is over the selected tab)
And for each one of these states you can specify the following styling information for the tabs:
- ForeColor
- BackColorFrom
- BackColorTo
- Bold
- Italic
- Underline
- Translucency
- CloseButton ForeColor
- CloseButton BackColor
To do this at run-time you can use one of the following properties:
- The ITEMSTYLE property
- The ITEMSTYLES property
and we'll take a look at each of these in turn.
The ITEMSTYLE property
prevStyle = Get_Property( ctrlEntID, "ITEMSTYLE", itemState ) currStyle = Set_Property( ctrlEntID, "ITEMSTYLE", newStyle, itemState )
This property allows you to get or set the ITEMSTYLE for a single state. The property itself is an @fm-delimited array of styling information like so:
<1> ForeColor (COLORREF) <2> BackColor From (COLORREF) <3> BackColor To (COLORREF) <4> Bold (1/0) <5> Italic (1/0) <6> Underline (1/0) <7> Translucency (0-100) <8> CloseButton ForeColor (COLORREF) <9> CloseButton BackColor (COLORREF)
Equates for these array positions can be found in the PS_TABCONTROL_EQUATES insert record:
equ TCIS_POS_FORECOLOR$ to 1 ; * // COLORREF equ TCIS_POS_BACKCOLOR_FROM$ to 2 ; * // COLORREF equ TCIS_POS_BACKCOLOR_TO$ to 3 ; * // COLORREF equ TCIS_POS_BOLD$ to 4 ; * // Boolean equ TCIS_POS_ITALIC$ to 5 ; * // Boolean equ TCIS_POS_UNDERLINE$ to 6 ; * // Boolean equ TCIS_POS_TRANSLUCENCY$ to 7 ; * // UInt (0-100) equ TCIS_POS_CLOSEBTNFORECOLOR$ to 8 ; * // COLORREF equ TCIS_POS_CLOSEBTNBACKCOLOR$ to 9 ; * // COLORREF
You must also use the index parameter with the Get_Property and Set_Property to specify the tab state that you are setting, which is an integer between 1 and 5:
equ TCIS_NORMAL$ to 1 equ TCIS_HOT$ to 2 equ TCIS_DISABLED$ to 3 equ TCIS_SELECTED$ to 4 equ TCIS_HOTSELECTED$ to 5
Example: Setting the Hot and Hot Selected styles
$insert colors $insert logical // Set the mouseover text to change to red itemStyle = "" itemStyle<TCIS_POS_FORECOLOR$> = RED$ Call Set_Property_Only( ctrlEntID, "ITEMSTYLE", | itemStyle, | TCIS_HOT$ ) // Set the mouseover text for a selected item to change to red // and bold itemStyle = "" itemStyle<TCIS_POS_FORECOLOR$> = RED$ itemStyle = TRUE$ Call Set_Property_Only( ctrlEntID, "ITEMSTYLE", | itemStyle, | TCIS_HOTSELECTED$ )
The ITEMSTYLES property
prevStyles = Get_Property( ctrlEntID, "ITEMSTYLES" ) currStyles = Set_Property( ctrlEntID, "ITEMSTYLES", newStyles )
This property is very similar to the ITEMSTYLE property except that it allows you to get or set the styles for all states at once. The property itself is an @fm/@vm delimited array: each state is delimited by @fm, and the styling information for each state is delimited by @vm.
<1> ItemStyle for the Normal State <2> ItemStyle for the Hot State <3> ItemStyle for the Disabled State <4> ItemStyle for the Selected State <5> ItemStyle for the Hot Selected State
(You will note these map onto the "TCIS_" state equates shown above) For each one of these states the ItemStyle information is an @vm delimited array using the same structure as for the ITEMSTYLE property, i.e:
<0,1> ForeColor (COLORREF) <0,2> BackColor From (COLORREF) <0,3> BackColor To (COLORREF) <0,4> Bold (1/0) <0,5> Italic (1/0) <0,6> Underline (1/0) <0,7> Translucency (0-100) <0,8> CloseButton ForeColor (COLORREF) <0,9> CloseButton BackColor (COLORREF)
So you can use the "TCIS_POS_" equates shown above as well. Example: Setting the Hot and Hot Selected styles using ITEMSTYLES
$insert colors $insert logical itemStyles = Get_Property( ctrlEntID, "ITEMSTYLES" ) // Set the mouseover text to change to red itemStyles<TCIS_HOT$, TCIS_POS_FORECOLOR$> = RED$ // Set the mouseover text for a selected item to change to red // and bold itemStyles<TCIS_HOTSELECTED$,TCIS_POS_FORECOLOR$> = RED$ itemStyles<TCIS_HOTSELECTED$,TCIS_POS_BOLD$> = TRUE$ Call Set_Property_Only( ctrlEntID, "ITEMSTYLES", itemStyles )
Setting ItemStyles in the Form Designer
Item styling for the tab control can also be specified at design time by using the "ItemStyles" property that is available when you select the tab control in the form designer. When you click the button for this property you will see an ItemStyles editor dialog that lets you specify the styling information: (Note that in the current release (10.0.6) you will not see this applied to the design control - this will be fixed in an upcoming release)