How to use a checkbox in Delphi?
Asked Answered
J

3

5

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?

Jean answered 19/3, 2009 at 5:59 Comment(1)
I want it where, if the user checks the checkbox, it will continue with that procedure, but if the checkbox is NOT checked, it will continue a different procedure.Jean
D
12

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.

Dipody answered 19/3, 2009 at 6:14 Comment(3)
For that second one, how would I implement my check if Print Screen is pressed? Or, if possible, could you put that someone into that second code?Jean
It's the same. You have an event handler for "KeyPressed" or similar event. Inside you check if it was really the key of interest pressed. If it is, invoke the code that reads checkbox state and acts accordingly.Dipody
Alright, I have this: begin if CheckBox1.Checked then begin If odd(GetAsyncKeyState(VK_snapshot)) Then begin //code here end else begin //different code here end; Which seems to work.Jean
I
0
if DT.FieldByName('name_of_checkbox').AsBoolean=True then begin ..... end;
// In this case dt is TADOquery that you had used in your program.
Intratelluric answered 30/11, 2009 at 15:54 Comment(0)
D
-1

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)

Divvy answered 19/3, 2009 at 11:25 Comment(1)
Please edit this - Delphi does not necessarily use short-circuit evaluation, the behaviour depends on the {$B} compiler directive or the respective command line switch!Gazelle

© 2022 - 2024 — McMap. All rights reserved.