Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Get Selected Tabs of a Multiselect TTabControl in Delphi

Title: Get Selected Tabs of a Multiselect TTabControl in Delphi Delphi's TTabControl (Win32 palette) can be used to add a control with multiple tab settings to a form. By using a tab control, an application can define multiple pages for the same area of a window or dialog box. A tab control is analogous to the dividers in a notebook or the labels in a file cabinet. Unlike a page control (TPageControl), TTabControl is not made up of several pages that contain different controls. Instead, TTabControl is a single object. Selected Tabs when Multiselect := true The Multiselect property of the TTabControl specifies whether multiple tabs can be selected at the same time. The MultiSelect property can only be true if the Style is tsFlatButtons or tsButtons. To select more than one tab with Multiselect = true, click the tab caption while holding the CTRL key. Unfortunately, the TTabControl does not expose a property or a method "get selected tabs" - to provide a list of indexes or strings one can use to determine what tabs are selected when multiselect is true. In order to get what tabs are selected you need to delve into low level Windows tab control messaging system. Here's what to do: Drop a TTabControl on a form. Name it "TabControl1". Specify Multiselect = true using the Object Inspector. Style = tsButtons using the Object Inspector. Add a few tabs by assigning strings to the Tabs property. Have a button control and an edit box (Edit1). Run the program, while holding the CTRL key click on a few tabs. The following button OnClick event handler will display the names of the selected tabs (separated by the pipe "|" character) in the edit box: uses commctrl; //display TabControl1 selected tab captions in an edit (Edit1) control var cnt: Integer; tabItem: TTCItem; selTabs: string; begin selTabs := ''; for cnt := 0 to -1 + TabControl1.Tabs.Countdo begin FillChar(tabItem, SizeOf(tabItem), 0) ; tabItem.mask := TCIF_STATE; tabItem.dwStateMask := TCIS_BUTTONPRESSED; if SendMessage(TabControl1.Handle, TCM_GETITEM, cnt, Longint(@tabItem)) 0 then if (tabItem.dwState and TCIS_BUTTONPRESSED) 0 then selTabs := selTabs + TabControl1.Tabs[cnt] + '|'; end; Edit1.Text := '|' + selTabs; end; To compile the above code you need to add "commctrl" to the uses clause. The trick is in sending the TCM_GETITEM message to the tabcontrol instance (SendMessage). TCM_GETITEM retrieves information about a tab in a tab control by filling in a TTCItem record variable. This record can be used to get or set the attributes of a tab item. The dwState field retrieves the current state of the item - if the TCIS_BUTTONPRESSED flag is set the button is selected / pressed. The above function, gets the state of every tab in the tab control and retrieves the state of the TCIS_BUTTONPRESSED flag for that item.