2017-05-12 68 views
0

我的程序有三個下拉菜單和一個ttabcontrol,其中有5個選項卡。我需要知道的是如何隱藏所有選項卡並將其可見性設置回來,如果下拉菜單選擇了特定項目。 例如 我的下拉列表有索引項。 A,B,C,A + B,A + C TabControl有以下選項卡。 A B C 現在我需要隱藏所有標籤和取消隱藏標籤甲如果下拉已經選擇了或& B如果下拉被選擇爲A + B.如何隱藏TTabcontrol中的多個選項卡

+0

什麼其他2 *下拉* S在做什麼? –

+0

在做同樣的事情D E F和G H我相同的A B C – Buddy

+0

我的建議是首先使用條件語句來工作。例如「如果」。之後,你可能會考慮考慮如何更有效地完成它。 –

回答

-1

使用枚舉類型來做到這一點。您可以非常輕鬆地探索布爾操作。

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

實施

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; 

如果你正在使用TCombobox的下拉列表中,使用OnChange事件

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; 

一個非常靈活和可擴展的解決方案。

例如,使用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 
+0

瞭解更多關於可枚舉邏輯的信息請閱讀https://www.thoughtco.com/understanding-delphi-set-type-1057656 –

+0

如果選擇標籤A + B或B + C,那麼如何選擇兩個標籤? – Buddy

+0

這將工作正常!它已經在上面的程序中實現了!只需將組合框中的新項目與您想要的組合相結合,並在'Case of'結構中添加對此索引的引用,但這就是我建議您使用'TCheckBox'或'TCheckBoxList'的原因,然後將所有可能的組合 –

相關問題