How to hide multiple tabs in TTabcontrol

2019-02-20 19:03发布

问题:

My program has three drop-down and one ttabcontrol which has 5 Tabs. what i need to know is how can i hide all the tabs and set their visiblety back again if drop-down menu has a particular item selected. For example My drop-down has index item of. A , B , C , A+B , A+C TabControl had following tabs. A B C Now i need to hide all the tabs and unhide tab A if drop down has selected a or a & b if dropdown is Selected to A+ B.

回答1:

use enumerable type to do that. You can explore boolean operation very easy.

TYPE
TabControlTag = (A, B, C);
TabTags = set of TabControlTag;
TForm1=class(TForm)
...

Implementation

procedure TForm1.HideTabControl(Sender: TObject);
{hide all tabItem in tabControl}
var
   i: integer;
begin
   for i := 0 to TabControl1.ComponentCount - 1 do
      if TabControl1.Components[i] is TTabItem then
         with TabControl1.Components[i] do
         begin
            visible := false;
         end;
end;

if you are using TCombobox as dropDown list, use the OnChange event

procedure TForm1.ComboBox1Change(Sender: TObject);
var
   Tabs: TabTags;
begin
   case ComboBox1.ItemIndex of
      0: { A } Tabs := [A];
      1: { B } Tabs := [B];
      2: { C } Tabs := [C];
      3: { A+B } Tabs := [A,B];
      4: { A+C } Tabs := [A,C];
   end;
   if A in Tabs then  tabItem1.Visible:=true;
   if B in Tabs then  tabItem2.Visible:=true;
   if C in Tabs then  tabItem3.Visible:=true;
end;

A very flexible and expansible solution.

For example, using TCheckbox

var
   Tabs: TabTags;
begin
tabs:=[];
If checkBoxA.IsChecked then TabTags:= [A];
If checkBoxB.IsChecked then TabTags:= TabTags + [B];//OR boolean operations. Also allowed [A,B] * [A] which means AND, [A,B] - [A] which means NOR,
If checkBoxC.IsChecked then Include(TabTags,C)
if A in Tabs then  tabItem1.Visible:=true;
if B in Tabs then  tabItem2.Visible:=true;
if C in Tabs then  tabItem3.Visible:=true;
end