如何仅在所有组合框都进行了选择时才启用 delphi 表单中的按钮



需要检查多个组合框是否不为空。尝试了一个组合框,如何将其应用于多个组合框。

if (combobox1.ItemIndex <> -1) then
  begin
     btnOK.Enabled := true;
  end
else
btnOK.Enabled := false;

PS:我是德尔福的新手,如果有的话,请忍受错误。

最简单的结构是这样的结构

if (ComboBox1.ItemIndex = -1) or (ComboBox2.ItemIndex = -1) or (ComboBox3.ItemIndex = -1) then
    Exit;
Button1.Enabled := true;

如果任何组合框选择了 -1,则会退出该过程。如果选中所有框,则会启用该按钮。

或者,您可以将它们全部绑定在一起并直接写入 Enabled 属性

Button1.Enabled := (ComboBox1.ItemIndex <> -1) and (ComboBox2.ItemIndex <> -1) and (ComboBox3.ItemIndex <> -1);

如果你真的有很多组合框,最漂亮的方法是使用数组

procedure TForm1.SetButtonActive;
var
    boxes: array of TComboBox;
    box: TComboBox;
begin
    boxes := [ComboBox1, ComboBox2, ComboBox3];
    for box in boxes do
        if box.ItemIndex = -1 then
            Exit;
    Button1.Enabled := true;
end;

最新更新