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

Delphi 7:如何使用复选框组合,delphi,Delphi,我正在用Delphi7开发一个小应用程序,但我对如何组合使用3个复选框感到困惑 示例:如果选中check.box1和checkbox2,则使用这些pref运行系统应用程序 或者,如果checkbox1仅选中使用这些pref运行系统应用程序,或者如果checkbox2仅选中使用这些pref运行,则听起来您在开始编写所需内容时遇到了困难,这基本上是一系列基于复合布尔表达式的if..then..else流控制语句。你可能想要一个像下面这样的结构。我不会向你展示整个过程,因为这对你来说是最好的,你可以自

我正在用Delphi7开发一个小应用程序,但我对如何组合使用3个复选框感到困惑 示例:如果选中check.box1和checkbox2,则使用这些pref运行系统应用程序
或者,如果checkbox1仅选中使用这些pref运行系统应用程序,或者如果checkbox2仅选中使用这些pref运行,则听起来您在开始编写所需内容时遇到了困难,这基本上是一系列基于复合布尔表达式的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;

听起来您在开始编写所需内容时遇到了困难,基本上是一系列基于复合布尔表达式的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;

受到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

受到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


if CheckBox.Checked然后
您需要尝试以一种准确概述您想要实现的目标的方式重写您的问题。目前很难知道你想做什么。
if CheckBox.Checked然后
你需要尝试重写你的问题,用一种能准确勾勒出你想要实现的目标的方式。目前很难知道你想做什么。就我个人而言,我会做一些像你一样的事情,所以从这里开始+1。我是这样写的,因为我觉得OP很难掌握问题的基本原理。@MartynA是的,我认为你做到了,事实上,你让它尽可能容易理解,因此我的+1。我只是想为这次行动提供一个替代方案。就我个人而言,我会做一些像你一样的事情,所以从这里开始+1。我是这样写的,因为我觉得OP很难掌握问题的基本原理。@MartynA是的,我认为你做到了,事实上,你让它尽可能容易理解,因此我的+1。我只是想提出一个备选方案供大家讨论。非常感谢第一个答案解决了我的问题就像一个符咒我对delphi很陌生一直以来都在使用批处理来处理我的自动任务还有一个问题我如何在delphi中设置我的应用程序的主题7谢谢你需要开始一个新的q来询问主题-这是一个不同的主题。不我需要问一个问题。“一个网络搜索就足够了。”@DavidHeffernan:的确,但最初的问题也是如此。嘿,呵呵,“和以前一样……”(大卫·伯恩?)。不管怎样,海尼,季节的哀鸣,继续努力,等。非常感谢第一个答案解决了我的问题就像一个魔咒一样我对delphi很陌生一直以来都在使用批处理我的自动任务还有一个问题我如何在delphi中设置我的应用程序的主题7谢谢你需要开始一个新的q来询问主题-这是一个不同的主题。无需提问。“一个网络搜索就足够了。”@DavidHeffernan:的确,但最初的问题也是如此。嘿,呵呵,“和以前一样……”(大卫·伯恩?)。不管怎样,HNY,季节的哀鸣,继续努力,等等。