0

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.

Buddy
  • 13
  • 2
  • What are the other 2 *drop-down*s doing? – Sertac Akyuz May 12 '17 at 18:14
  • There doing same thing D E F and G H I same as A B C – Buddy May 12 '17 at 18:21
  • My suggestion is to first getting it to work by using conditional statements. For instance "if". After that you might consider thinking on how it could be done more efficiently. – Sertac Akyuz May 12 '17 at 18:33
  • If ComboBo.ItemIndex = 1 then begin Tabitem1.enabled :=True ; end else begin Tabitem1.enabled :=flase ; it is working fine with 1 tab hiding and dishidng but when i have to get visible two tab a same time like a+b case it wont work what to do in that case ? – Buddy May 12 '17 at 18:40
  • You just have to have consecutive statements, each one for a tab. – Sertac Akyuz May 12 '17 at 18:42
  • 2
    Are you using VCL or FMX? In FMX, `TTabItem` has a `Visible` property. But in VCL, `TTabControl` does not allow you to hide/show tabs, you have to physically remove them from the `Tabs` property and add them back again, so I would suggest using `TPageControl` instead of `TTabControl` so you can set the `TTabSheet.TabVisible` property of the various pages as needed. – Remy Lebeau May 12 '17 at 19:13
  • I Am using FMX. – Buddy May 13 '17 at 09:49

1 Answers1

-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
  • to know more about enumerable logic read https://www.thoughtco.com/understanding-delphi-set-type-1057656 – Ricardo da Rocha Vitor May 12 '17 at 21:40
  • And what if tab A+B or B+C is selected then how to get visible both tabs at once ? – Buddy May 13 '17 at 05:26
  • This will work fine! It is already implemented in procedure above! Just put new Items on combobox with combination you want and add reference to this Index in 'Case of' structure, But that is the reason I suggest you use 'TCheckBox' or 'TCheckBoxList', then you take all possible combination – Ricardo da Rocha Vitor May 13 '17 at 13:38