Link to home
Start Free TrialLog in
Avatar of gxs
gxsFlag for United States of America

asked on

Help with If Then Statement

Hello Experts,

I'm having a difficulty understanding the logic of IF Then Else statement in Delphi

I know they differs based on the case (Nested Ifs ,, etc)

I tried to do a simple app that checks if the selected answers are correct or not ( a Quiz app )

Here is my code:

if RB1.Checked and RB3.Checked and RB6.Checked and RB8.Checked and RB9.Checked then
      begin
      FFirstGB.Color := clRed;
      FSecondGB.Color := clRed;
      FThirdGB.Color := clRed;
      FFourthGB.Color := clRed;
      FFifthGB.Color := clRed;
      end;

Open in new window


RB1 < means RadioButton1
FFirstGB < Means First First Group Box

In my If statement, if the wrong answers are chosen then their group boxes will become red. Other than that, they will stay the same.

But the problem is, when I run the app and choose the wrong answers. The first group box becomes red only : (

I'm totally sure that it has to do with the logic.

I have typed If statements in several ways but the problem still the same.

Waiting for your help and appreciated in advance,

Mike.
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Take it slowly and solve for the simplest conditions first
It would even more clearer if you first wrote it on paper.
Don't bunch all the conditions together. Test for each GroupBox separately

Dont check for the wrong answers if it is a multiple choice, rather test for the correct answer.
Example if groupbox 1 has checkboxes cb1, cb2, cb3 and cb4 and you know that cb3 is the correct answer, you could do this

if cb3.checked then
  groupbox1.color := clBlue
else
  groupbox1.clor := clRed;
It looks like you are implementing an all-or-nothing answer.  Either they get all the questions right or all the questions are marked as red.

In this example, you are setting all the frames red if any wrong answer is given.
if RB1.Checked or RB3.Checked or RB6.Checked or RB8.Checked or RB9.Checked then
      begin
      FFirstGB.Color := clRed;
      FSecondGB.Color := clRed;
      FThirdGB.Color := clRed;
      FFourthGB.Color := clRed;
      FFifthGB.Color := clRed;
      end;

Open in new window


In this example, the RB# are the correct answers, then the all-or-nothing might look like this.
if RB1.Checked and RB3.Checked and RB6.Checked and RB8.Checked and RB9.Checked then
else
      begin
      FFirstGB.Color := clRed;
      FSecondGB.Color := clRed;
      FThirdGB.Color := clRed;
      FFourthGB.Color := clRed;
      FFifthGB.Color := clRed;
      end;

Open in new window

Avatar of gxs

ASKER

Thanks aikimark and ewangoya for your efforts,

Actually I have tried to write them in different conditions, but they never worked. It worked out with me in VB without hassles at all but I separated the if statements over there.

Your example aikimark is correct logically but the problem still exist. Only the first group box becomes red. I'm using Delphi XE2, i'm just confused. Even though i'm not that expert but do you think that it has to do with the protected properties ,, etc ?

Waiting for you help,

Mike.
Avatar of gxs

ASKER

Aikimark, I have tried your example but changed the components.

I have decided to use small Timage components instead of coloring the group boxes. I put an X picture to indicate if the answer is wrong or correct.

It worked fine.

if RB1.Checked or RB3.Checked or RB6.Checked or RB8.Checked or RB9.Checked then
      begin
      FQWI1.Visible := true;
      FQWI2.Visible := true;
      FQWI3.Visible := true;
      FQWI4.Visible := true;
      FQWI5.Visible := true;
      end;

Open in new window


But here comes the question, why it didn't work with the colors ?

I'm just asking you that to learn only.

Thanks for your effort in advance,

Mike.
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is it possible that your non-coloring frames are inside another container?
Avatar of gxs

ASKER

Wooow, that was amazing Geert_Gruwez,

In regard to your question Aikimark, yes. The group boxes are inside an one large group box :(
amazing ? ugh, no not really
there isn't any rolling dice in their ... :)

another sample for a quiz:
https://www.experts-exchange.com/questions/24700123/In-multiple-choice-Q-A-application-best-way-to-bring-up-answers-to-choose-a-class.html