gxs
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:
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.
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;
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.
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.
In this example, the RB# are the correct answers, then the all-or-nothing might look like this.
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;
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;
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.
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.
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.
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.
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;
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is it possible that your non-coloring frames are inside another container?
ASKER
Wooow, that was amazing Geert_Gruwez,
In regard to your question Aikimark, yes. The group boxes are inside an one large group box :(
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
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
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;