How to hide multiple tabs in TTabcontrol

强颜欢笑 提交于 2019-12-01 13:23:18

问题


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


来源:https://stackoverflow.com/questions/43943107/how-to-hide-multiple-tabs-in-ttabcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!