简体   繁体   English

如何在 Delphi 中使用复选框?

[英]How to use a checkbox in Delphi?

Right now, I have the code:现在,我有代码:

begin
If odd(GetAsyncKeyState(VK_snapshot)) then
If CheckBox1.Checked then
begin

And then it continues on with the rest of the code.然后继续使用代码的 rest。 Is that the correct way of doing that, or am I doing it wrong?这是正确的做法,还是我做错了?

What you suggest is a perfectly legal way to determine if a checkbox is checked.您建议的是确定是否选中复选框的完全合法的方法。 The code doing so might look like这样做的代码可能看起来像

if checkBox.Checked then begin
    //do whatever needed for checked checkbox
end

or like this或者像这样

if checkBox.Checked then begin
    //do whatever needed for checked checkbox
end else begin
    //do whatever needed for unchecked checkbox
end

Just remember that the value you obtained from Checked property corresponds to the checkbox's state at the moment when you obtained the value.请记住,您从 Checked 属性中获取的值对应于您获取该值时复选框的 state。

if DT.FieldByName('name_of_checkbox').AsBoolean=True then begin ..... end;
// In this case dt is TADOquery that you had used in your program.

since you are using 2 if-statements, you might also combine them into one:由于您使用的是 2 个 if 语句,因此您也可以将它们合并为一个:

if odd(GetAsyncKeyState(VK_snapshot)) and CheckBox1.Checked then
begin
  ...
  ...
end;

The second part of the if-statement (checkbox1.Checked) will only be evaluated if the first one evaluates to True. if 语句的第二部分 (checkbox1.Checked) 仅在第一个评估为 True 时才被评估。 (Since Delphi uses Short-circuit evaluation ) (由于 Delphi 使用短路评估

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM