简体   繁体   中英

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. 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.

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:

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. (Since Delphi uses Short-circuit evaluation )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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