Delphi 7:如何使用复选框的组合

时间:2015-10-24 09:27:02

标签: delphi

我正在使用Delphi7中的一个小应用程序,但我很困惑如何使用3个复选框的组合 示例:如果选中check.box1和checkbox2,则使用这些首选项运行系统应用程序 或者,如果checkbox1仅选中,则使用这些首选项运行系统应用程序,或者仅选中checkbox2以使用这些首选项运行

2 个答案:

答案 0 :(得分:3)

听起来你在开始编写你需要的东西时遇到了麻烦,这基本上是一系列基于复合布尔表达式的if..then..else流控制语句。您可能想要一个类似下面的结构。我不会向你展示整件事,因为你最好自己解决这个问题,但这应该会给你一个想法:

  if (CheckBox1.Checked) and (CheckBox2.Checked) and not (CheckBox3.Checked) then 
    begin
      // do one thing
    end
  else 
    begin
      if (CheckBox1.Checked) and not (CheckBox2.Checked) and not (CheckBox3.Checked) then 
        begin
          // do another thing
        end
      else
        // etc
    end;

如果您熟悉枚举类型,则最好声明一个并使用它来派生所选首选项,然后对它们进行操作。这样做的目的有两个:提高代码的清晰度(当然代价是代码更长);并将用于计算用户所选择的偏好的逻辑与作用于其上的代码分开。

这样的东西
type
  TRunPreference = (rpNone, rpOne, rpTwo, rpThree, rpFour,
    rpFive, rpSix, rpSeven, rpEight);

  // rpOne..rpEight are for the 8 permutations of three booleans
  // and the rpNone is to provide for initialising the variable
  // with something which isn't one of the desired values

var
  RunPreference : TRunPreference;

procedure TForm1.Button1Click(Sender: TObject);
begin
  RunPreference := rpNone;

  if (CheckBox1.Checked) and (CheckBox2.Checked) and not (CheckBox3.Checked) then
    begin
      RunPreference := rpOne;
    end
  else
    begin
      if (CheckBox1.Checked) and not (CheckBox2.Checked) and not (CheckBox3.Checked) then
        begin
          RunPreference := rpTwo;
        end
      else
        ; //
    end;
  case RunPreference of
    rpNone :
      ; // do nothing
    rpOne :
      begin
        // Act on rpOne
      end;
    rpTwo :
      begin
        // Act on rpTwo
      end;
     // etc
   end; { Case of RunPreference }
end;

答案 1 :(得分:2)

受到MartynA解决方案的启发,并且因为我更喜欢紧凑的代码,所以我会这样做:

// For the three (sets of?) preferences, define bit values, and
// a variable to hold the combination of the selected preferences:
const
  PrefA=1; PrefB=2; PrefC=4;
var
  prefs: byte;

procedure TForm12.Button2Click(Sender: TObject);
begin
  // whether a preference should be active or not becomes a simple `OR` function
  // based on each checkbox's state
  prefs := 0;
  if CheckBox1.Checked then prefs := prefs or PrefA;
  if CheckBox2.Checked then prefs := prefs or PrefB;
  if CheckBox3.Checked then prefs := prefs or PrefC;

  // then use a `case` statement to run according the preference combination  
  // The values like `PrefA or PrefC` can be used instead of numeric values,
  // since they can be resolved at compile time.
  // Whether they improve readability or not is ... (disputable)

  case prefs of
    0:
      ShowMessage('Running without preferences');
    PrefA:
      ShowMessage('Running with PrefA alone');
    PrefB:
      ShowMessage('Running with PrefB alone');
    PrefA or PrefB:
      ShowMessage('Running with PrefA and PrefB');
    PrefC:
      ShowMessage('Running with PrefC alone');
    PrefA or PrefC:
      ShowMessage('Running with PrefA and PrefC');
    PrefB or PrefC:
      ShowMessage('Running with PrefB and PrefC');
    PrefA or PrefB or PrefC:
      ShowMessage('Running with PrefA and PrefB and PrefC');
  end;
end;

如果您需要检查您的代码是否有偏好,您可以这样做:

  if (prefs and PrefB) then
    // do whatever requires PrefB to be selected