Questions and answers ...

I have many Forms (about 20)with questions on each form
(Done with GroupBox and radiobuttons,type :
procedure TForm1.Button1Click(Sender: TObject);
begin
if RadioButton1.Checked = true then
showmessage('Right answer !') else
 if RadioButton2.Checked = true then
showmessage('Wrong answer !') else
if RadioButton3.Checked = true then
showmessage('Wrong answer !')
end;
The show or hide of the forms is done by 2 BinBtns.
procedure TForm1.BitBtn1Click(Sender: TObject);
{This button is used to display previous Form.Only in the case
of Form1 the dialog below appears }
 begin
   If MessageDlg('This is the last frame.Next question ?',mtInformation,
  [mbYes, mbNo],0) = mrYes then
  Begin
    Form1.Hide;
    Form2.Show;
  end;
To see the next Form :
procedure TForm1.BitBtn2Click(Sender: TObject);

begin

Form2.Show;
Form1.Hide;

end;
The questions are displayed by means of labels.
Simple so far...
To avoid radiobuttons being checked between switching form,
each form is equiped with :
procedure TForm1.FormShow(Sender: TObject);
Var i:Integer;
begin
 for i := 1 to 3 do
  TRadioButton(FindComponent('RadioButton'+inttostr(i))).
  Checked := false;
end;
So now what???What I want to do is this (or what I have already done
but dont know how to make it function right since I am a beginner):
I have placed 5 Panels at the bottom of each form with 1 editbox each.
To describe what each editbox is about I have used static text (alignLeft)
which I have placed inside the panels.
What are they?
StaticText1 (Caption 'Number of questions'(EditBox1)),
StaticText2 (Caption 'Questions answered'(EditBox2)),
StaticText3 (Caption 'Right answers'(EditBox3)),
StaticText4 (Caption 'Wrong answers'(EditBox4)),
StaticText5 (Caption 'Questions remaining(EditBox5)).
This is basically also what the contents of the edit boxes are.
Each form is identical!
Now what I need to know is how to make the editboxes function.
First EditBox (1) is a Constant as the number of questions are
the same.So how is the rest supposed to function?
When the user selects a radiobutton and clicks OK
(procedure TForm1.Button1Click(Sender: TObject);)
Edit2 counts 1 question has been answered.
Edit5 counts how many questions are remaining.
If the answer is right then Edit3 adds.If the answer is wrong
then Edit4 adds.
To reset everything to zero I have placed a button on the
form (every form has it)to reset counters (Editboxes 2 - 5 to zero).
So every next form I display, must remember the counters value.
I hope I have explained right...
Please do post COMMENTS and not straightout answers as
I want to check every suggestion and do not wish to have
question locked.Have in mind I am a beginner and a little "slow"
in understanding.
Thank you!


 


EnejAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rene100Commented:
don't know if i understand you right...you have 5 editboxes and you want to display there various numbers?
and why do you use editboxes, because their text can be changed by the user..??

regards
rene
0
geobulCommented:
 Maybe You can create a unit where to declare 5 integer variables.
  Place the name of the unit in uses clause of each form.
  In the first form's OnCreate event (for example) initialize the variables.
  In the OK btn OnClick event decide what variable to change and then place the new value to the EditBox.
  When show new form place these variables again in the EditBoxes(OnShow event).

good night and regards,

Geo
0
karooCommented:
Enej,

firstly, initialize the values of the editboxes on the first form:

procedure TForm1.FormShow(Sender: TObject);
begin
  EditBox1.Text:= your total # of q's
  EditBox2.Text:= '0';
  EditBox3.Text:= '0';
  EditBox4.Text:= '0';
  EditBox5.Text:= your total # of q's
end;


second increment the counters by changing the code of your OK button click to:

procedure TForm1.Button1Click(Sender: TObject);
var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True else
  else if RadioButton2.Checked = true then
    Correct:= False else
  else if RadioButton3.Checked = true then
    Correct:= False;
  { get values from editboxes }
  Answ := StrToInt(EditBox2.Text);
  Corr := StrToInt(EditBox3.Text);
  Wng  := StrToInt(EditBox4.Text);
  Rem  := StrToInt(EditBox5.Text);
  { calc }
  Inc(Answ);
  if Correct then Inc(Corr)
  else Inc(Wng);
  Dec(Rem);
  { set new editboxes values }
  EditBox2.Text := IntToStr(Answ);
  EditBox3.Text := IntToStr(Corr);
  EditBox4.Text := IntToStr(Wng);
  EditBox5.Text := IntToStr(Rem);
  { Show message }
  if Correct then
    ShowMessage('Right answer !')
  else ShowMessage('Wrong answer !')
end;


lastly we have to pass the counters to the next form:

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
  Form2.EditBox1.Text := EditBox1.Text;
  Form2.EditBox2.Text := EditBox2.Text;
  Form2.EditBox3.Text := EditBox3.Text;
  Form2.EditBox4.Text := EditBox4.Text;
  Form2.EditBox5.Text := EditBox5.Text;
  Form2.Show;
  Form1.Hide;
end;

try this see if it helps you.
btw, i see a couple of logic errors in your code, didn't want to post to long a comment if you are interested tell me.

Regards Ben:)
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

interCommented:
Hi Enej,

I have several suggestions as well as simple solution to your problem. First of all, instead of checking each radiobox on each form drop a TRadioGroup component on form if the visual appereance is not a problem (i.e. you are not coloring each radio button etc..). For example, in your form1, say you have a question with 5 choices, then you have a radiogroup1 with 5 radio buttons. There are some nice features of radiogroups for example you can set any of them by using ItemIndex property. In each of your forms set a constant TrueAnswer to the right answer. Then your answer checking turns out to be as follows:

if RadioGroup1.ItemIndex = TrueAnswer  then
  showmessage('Right answer !')
else
  showmessage('Wrong answer !')

by this way you can detect the true answer without checking all radio items and regardless of the number of answers for a question.

Notice that you have doing approximatelly the same thing for every form you have. So, by remembering that the golden rule of object oriented programing, we conclude that we may DERIVE our forms from a basic formtype called TQuestionForm instead of TForm that have several advantages as you will see below.

Now, create an empty unit from File->New... copy the following code to it: (save as QBaseFrm.pas)

unit QBaseFrm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TQuestionForm = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    AnsValue: Integer;
    function NofQForms: Integer;
    function NofRem: Integer;
    procedure CalcAns(Answered,Right, Wrong : TEdit);
  end;

implementation

function TQuestionForm.NofQForms: Integer;
var
  I: Integer;
begin
  Result := 0;
  for I := 0 to Screen.FormCount - 1 do
    if Pos('QForm', Screen.Forms[i].Name) <> 0 then Inc(Result);
end;

function TQuestionForm.NofRem: Integer;
begin
  //extract the current form number form name
  Result := NofQForms -
    StrToInt(Copy(Screen.ActiveForm.Name, 6, 10));
end;

procedure TQuestionForm.CalcAns(Answered,Right, Wrong: TEdit);
var
  I: Integer;
  R, W: Integer;
begin
  R := 0; W := 0;
  for I := 0 to Screen.FormCount - 1 do
    if Pos('QForm', Screen.Forms[i].Name) <> 0 then
    begin
      with TQuestionForm(Screen.Forms[i]) do
        if AnsValue > 0 then Inc(R)
        else if AnsValue < 0 then Inc(W)
    end;
  Answered.Text := IntToStr(R+W);
  Right.Text := IntToStr(R);
  Wrong.Text := IntToStr(W);
end;

end.

 
What we are going to do is to make your form development easier and when ever you add a new question form, the values are automatically updated. So name all your forms (and all the forms that will be added in future) in a specified format. For example name them as QForm1, QForm2, ... QFormN, etc...Do not start non question forms with QForm. And CHANGE the form decleration as
  TQFormXXX = class(TQuestionForm) ...etc.
So the question forms should be derived from the questionform base. We need just one more thing to do the magic. On each form OnActivate paste the following: (here for QForm1)

procedure TQForm1.FormActivate(Sender: TObject);
begin
 Edit1.Text := IntToStr(NofQForms);
 CalcAns(Edit2,Edit3,Edit4);
 Edit5.Text := IntToStr(NofRem);
end;

The same idea can be extended to make your life even easier. But lets discuss the basics first...
c.u.
igor
0
EnejAuthor Commented:
rene100 - The text inside editboxes
will not be changed by the user.
karoo - What logic errors?
I will check the rest out soon...
0
EnejAuthor Commented:
inter -
Indeed, I have done the project with
TRadioGroup (Inside I have placed 3
radiobuttons)...(checked - not-checked thing...)
I do not follow your reasoning on your Const.
How do you define it between various radiobuttons?
I could not compile the rest...

0
interCommented:
Sorry but I can't catch what you mean. What is Const?
0
EnejAuthor Commented:
Constant ...
TrueAnswer=RightAnswer;
Only one answer is right,so how do you
write the code for that?
0
interCommented:
Sorry friend, you are right. In each question form there are two integers as :

public
  TrueAnswer : Integer;
  AnsValue : Integer;

you set the true answer in your form create to the actual answer index. AnsValue is the variable that stores the current state of that question, if AnsValue > 0 then the user selects the right answer;  < 0 if user select wrong answer, =0 if user left question empty. So, check out CalcAnswer above how it counts all the forms that descends from the TQuestionForm and according to the AnsValue fields of the form, Right, Wrong and Empty questions can be determined.

Again sorry, I test the program above but I don't post my main form since it is not like your question forms.
Igor
0
EnejAuthor Commented:
May I e-mail you the thing so you can correct it?It would be much easier...
0
EnejAuthor Commented:
Karoo - I have just compiled your try and it works (apart from minor typos),so the points should go to you.Sorry Inter...
I would have some few more questions indeed...like why my app. Forms
flicker when going backwards 3-2-1...?

0
EnejAuthor Commented:
Karoo -
I have a little problem with your version.
When I click on the OK button the App.
correctly counts everything up in the edit boxes.
But,if the user clicks once more OK on the same form, once more
the same thing happens and that must not.
I would like the user to know the right answer when he clicks
on the OK button but the editboxes must not register further clicking.
Once he answers the question (good or bad),the editbox counters
register his move.No more registration per Form...Do you understand?
0
karooCommented:
Enej,

just got a chance to look at your postings, will read through it and get back to you asap
0
karooCommented:
Enej,

>>flicker when going backwards
when you go forward i.e. 1..2 etc the code:
Form2.Show;
Form1.Hide;
it has to be the other way around when you go backwards:
Form1.Show;
Form2.Hide;
does this help with the flickering?
if not post the code for the back button click.

To limit the counting:
Firstly, can a user change an answer?
if not then:
declare a private variable for the form i.e.
  private
    { Private declarations }
    UpdateCalcs: Boolean;

second on the FormShow set
UpdateCalcs:= False;

now on a RadioButtonClick event set UpdateCalcs:= True;

and the OKButtonClick event change to:
procedure TForm1.Button1Click(Sender: TObject);
var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True else
  else if RadioButton2.Checked = true then
    Correct:= False else
  else if RadioButton3.Checked = true then
    Correct:= False;
  if UpdateCalcs then
  begin
    UpdateCalcs:= False;
    { get values from editboxes }
    Answ := StrToInt(EditBox2.Text);
    Corr := StrToInt(EditBox3.Text);
    Wng  := StrToInt(EditBox4.Text);
    Rem  := StrToInt(EditBox5.Text);
    { calc }
    Inc(Answ);
    if Correct then Inc(Corr)
    else Inc(Wng);
    Dec(Rem);
    { set new editboxes values }
    EditBox2.Text := IntToStr(Answ);
    EditBox3.Text := IntToStr(Corr);
    EditBox4.Text := IntToStr(Wng);
    EditBox5.Text := IntToStr(Rem);
  end;
  { Show message }
  if Correct then
    ShowMessage('Right answer !')
  else ShowMessage('Wrong answer !')
end;

regards Ben:)
0
EnejAuthor Commented:
Resolved the flickering but cant make the above code work.It basically does the same as the previous one...No difference!
I hope you do understand what I am trying to do?
Example:
I am on Form1.Three questions only one is true.I click on a radioButton and get the reply 'Wrong answer!'.
(Now the App. has registered my attempt in the editboxes).
So how do I know which is the right answer?I click on another r-button and
my guess is right.Once again the app. has registered the event.And that must
not happen!The user can not answer
the same question more than once (at least when the editboxes are concerned).
????
0
karooCommented:
Enej,

ok, lets go one step back. This is how I understand the flow of your app, correct where im wrong.

1.) Form1 opens and the Counters initialize.
2.) the user selects an option for the first question (ie radiobutton1)
3.) the user clicks on the OK button
4.) A message displays right or wrong.
5.) Increment counters.
6.) The user answers the second question (r-button2)
7.) the user clicks on the OK button
8.) A message displays right or wrong.
9.) Increment counters.
etc.
etc.
n.) user clicks on next button.

one more q, do you disable/enable the state of your radiobuttons?

regards Ben:)
0
interCommented:
Sorry I should be out,
my email is
guclu.ongun@isbank.net.tr if you still need it.
igor ;-)
0
EnejAuthor Commented:
That is round about how it goes...
The only problem is that OK button must
function normally (on click displaying
answers (right-wrong),but counters must
stop functioning after the first guess.
Basically,it is one question with three possible answers (A,B,C).User must choose between these options...
I can only allow one registered guess
per question.
Perhaps a timer ?
0
EnejAuthor Commented:
Timer... like On Button1.Click
Timer1 enabled...
And have some procedure hanged on it
like edit2.enabled :=false or
readonly...????On showing next Form
(Form2)Timer1.Enabled:=False,Timer1.Free
etc...???
0
interCommented:
hi,
just disable the radiogroup when user chooses and answer...
igor
0
interCommented:
my friend we have a timezone problem with you ;-)
0
EnejAuthor Commented:
Hi Inter ...
What do you mean timezone problem?
Anyway,since you follow the Q I will
give you 50 points if yoy answer me
this...
I have a button on the Form1 (Button3)
Caption 'reset counters' with code :
Edit2.Text:='0';
Edit3.Text:='0';
Edit4.Text:='0';
Edit5.Text:='100';
Now I would like the button to reset counters on Form2 and 3 as well.
(I have similar button on other forms but I would like to do it from one place
for all,type Form1.Button3.Clickself
....something like that (From other Forms
and vice versa)..
Disabling RadioGroup?Then if the user
clicks wrong how is he to find the right answer?

0
EnejAuthor Commented:
Inter - 50 more points if you tell me how to log the session to a file?
Meaning,having App. create file on
buttonclick 'start logging'
and writing the final result of the editboxes on buttonclick 'stop logging'
to that file?
0
karooCommented:
hi Enej,

a timer for this would be an overkill,
use the boolean value UpdateCalcs as described in my previous comment but change the following:

TForm1.RadioGroup1Click(Sender: TObject);
begin
  //Remove this line
  UpdateCalcs:= True;
end;

Add a new event on the FormCreate

procedure TForm1.FormCreate(Sender: TObject);
begin
  UpdateCalcs:= True;
end;

Keep the OKButtonClick with the Check for UpdateCalcs the same. This means that the value for UpdateCalcs will only be set to true once on the form create and false after the Calculations has been done so the user can still check the value of the any answer but the counters will remain the same.

good luck and let me know how it goes
Ben:)
0
interCommented:
Hi,
by time zone problem, i am just kidding (when I am on computer your are not, and vice versa, anyway we meet).
The answer is, if every question form of yours are identical you may clear them by either iterating though all the components of forms and find the editboxes and clear them, or move edits to the base form(TQuestionForm). My way to the overal problem could be different, as I said before if you like here is my email...
0
interCommented:
What...? you have about 100 questions? I think we must change the logic of creating a form for each question. This is a very heavy burden for windows system. We should change the architecture for such a program, do you want it? (clue : for all your questcions there could be only one form, and the questions can be read from a file dynamically. By this way for adding new questions (or deleting some you need not to modify the exe)
0
karooCommented:
inter,

i agree that the logic for the app could be improved, i also have a solution with one form that dynamically creates the q's, the reason why i folowed enej design is because of this line
>>Have in mind I am a beginner...
my idea was to get this to work first and help Enej a bit and then show an alternative sollution for the problem showing the power of OO.

Regards Ben:)
0
EnejAuthor Commented:
I can not make the thing function!
Strangely,now when I click OK even when no radiobutton is checked I get 'answer'
message (???).

This is the code for Form1 (First Version ):
***********************************************************************
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Buttons;
type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    Button1: TButton;
    Button2: TButton;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Button3: TButton;
    Panel5: TPanel;
    Image1: TImage;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    StaticText4: TStaticText;
    StaticText5: TStaticText;
    StaticText6: TStaticText;
    Panel6: TPanel;
    StaticText7: TStaticText;
    Panel7: TPanel;
    StaticText8: TStaticText;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
 
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
   CHECKED :BOOLEAN;
implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True else
   if RadioButton2.Checked = true then
    Correct:= False else
   if RadioButton3.Checked = true then
    Correct:= False;
  Answ := StrToInt(Edit2.Text);
  Corr := StrToInt(Edit3.Text);
  Wng  := StrToInt(Edit4.Text);
  Rem  := StrToInt(Edit5.Text);
  Inc(Answ);
  if Correct then Inc(Corr)
  else Inc(Wng);
  Dec(Rem);
  Edit2.Text := IntToStr(Answ);
  Edit3.Text := IntToStr(Corr);
  Edit4.Text := IntToStr(Wng);
  Edit5.Text := IntToStr(Rem);
    if Correct then
    ShowMessage('Right answer !')
  else ShowMessage('Wrong answer !')
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Application.Terminate
end;

procedure TForm1.BitBtn2Click(Sender: TObject);

begin

Form2.Edit1.Text := Edit1.Text;
Form2.Edit2.Text := Edit2.Text;
Form2.Edit3.Text := Edit3.Text;
Form2.Edit4.Text := Edit4.Text;
Form2.Edit5.Text := Edit5.Text;
Form2.Show;
Form1.Hide;

end;

procedure TForm1.BitBtn1Click(Sender: TObject);

 begin
   If MessageDlg('This is the last frame.Next question ?',mtInformation,
  [mbYes, mbNo],0) = mrYes then
  Begin
  Form2.Edit1.Text := Edit1.Text;
  Form2.Edit2.Text := Edit2.Text;
  Form2.Edit3.Text := Edit3.Text;
  Form2.Edit4.Text := Edit4.Text;
  Form2.Edit5.Text := Edit5.Text;
  Form2.Show;
  Form1.Hide;
  end;

end;

procedure TForm1.FormShow(Sender: TObject);
Var i:Integer;
begin
 for i := 1 to 3 do
  TRadioButton(FindComponent('RadioButton'+inttostr(i))).
  Checked := false;
 

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Edit2.Text:='0';
Edit3.Text:='0';
Edit4.Text:='0';
Edit5.Text:='100';
end;
end.
*********************************************************************
This is the second version (The one last suggested):
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Buttons;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    Button1: TButton;
    Button2: TButton;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Button3: TButton;
    Panel5: TPanel;
    Image1: TImage;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    StaticText4: TStaticText;
    StaticText5: TStaticText;
    StaticText6: TStaticText;
    Panel6: TPanel;
    StaticText7: TStaticText;
    Panel7: TPanel;
    StaticText8: TStaticText;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;

    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
     UpdateCalcs: Boolean;


    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
   CHECKED :BOOLEAN;
implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
 var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True
  else if RadioButton2.Checked = true then
    Correct:= False
  else if RadioButton3.Checked = true then
    Correct:= False;
  if UpdateCalcs then
  begin
    UpdateCalcs:= False;

    Answ := StrToInt(Edit2.Text);
    Corr := StrToInt(Edit3.Text);
    Wng  := StrToInt(Edit4.Text);
    Rem  := StrToInt(Edit5.Text);

    Inc(Answ);
    if Correct then Inc(Corr)
    else Inc(Wng);
    Dec(Rem);

    Edit2.Text := IntToStr(Answ);
    Edit3.Text := IntToStr(Corr);
    Edit4.Text := IntToStr(Wng);
    Edit5.Text := IntToStr(Rem);
  end;
 
  if Correct then
    ShowMessage('Right answer !')
  else ShowMessage('Wrong answer !')
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Application.Terminate
end;

procedure TForm1.BitBtn2Click(Sender: TObject);

begin

Form2.Edit1.Text := Edit1.Text;
Form2.Edit2.Text := Edit2.Text;
Form2.Edit3.Text := Edit3.Text;
Form2.Edit4.Text := Edit4.Text;
Form2.Edit5.Text := Edit5.Text;
Form2.Show;
Form1.Hide;


end;

procedure TForm1.BitBtn1Click(Sender: TObject);

 begin
   If MessageDlg('This is the last frame.Next question ?',mtInformation,
  [mbYes, mbNo],0) = mrYes then
  Begin
  Form2.Edit1.Text := Edit1.Text;
  Form2.Edit2.Text := Edit2.Text;
  Form2.Edit3.Text := Edit3.Text;
  Form2.Edit4.Text := Edit4.Text;
  Form2.Edit5.Text := Edit5.Text;
  Form2.Show;
  Form1.Hide;
  end;

end;

procedure TForm1.FormShow(Sender: TObject);
Var i:Integer;
begin
 for i := 1 to 3 do
  TRadioButton(FindComponent('RadioButton'+inttostr(i))).
  Checked := false;
 

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Edit2.Text:='0';
Edit3.Text:='0';
Edit4.Text:='0';
Edit5.Text:='100';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
UpdateCalcs:= True;

end;

end.
**********************************************************************
When I compile this second suggestion,one try (answer) and the counters are dead for the rest of the session...
Values of EditBoxes are set by their Text property.
Try and compile an see (Leave Panels/Image/Static text out).
0
karooCommented:
Enej,

I created two forms with the code above and it works but you have to set the boolean value UpdateCalcs to true on the Create of Form2 also. This will allow Form2 to accept one answer from the user and so on.

procedure TForm2.FormCreate(Sender: TObject);
begin
  UpdateCalcs:= True;
end;


we also have to tell the user that he must make a selection if no radiobutton was checked, change the OKButtonClick to:

procedure TForm1.Button1Click(Sender: TObject);
 var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True
  else if RadioButton2.Checked = true then
    Correct:= False
  else if RadioButton3.Checked = true then
    Correct:= False
  else begin
    ShowMessage('No selection has been made');
    Exit;
  end;
  if UpdateCalcs then
  begin
  ...
  ..
  .
end;

let me know if this helps you.
Ben.
0
EnejAuthor Commented:
Sorry for the inconvenience but I think
that Delphi 5 ships with bugs (incorporated).I can find no other reasonable explanation why a good written code gives unexpected results.
I created another project in another directory and it works OK except for
a minor issue.
The App. runs OK first time that it is run (and the way it should)till the very end of the questions.But I can not restart the questionaire (the App.)once
more while it is running ???!!!
I have to terminate the App. and restart.Is there a cure for this?
(Maybe another button?)
Also,there is the issue of the
'reset counters' button.
I would like to reset counters on all forms.How?

0
EnejAuthor Commented:
Arghhhh....not another button.The thing should function when hitting the
'Reset counters' button.
Meaning,...start all over again...
That is all!
And if you tell me how to log the session to a file I will give you another 100 Pts.Fair enough?
Inter talked about strain on the Windows system...I do not think that a
Form in RAM uses much space...?
0
EnejAuthor Commented:
Well...I can hang UpdateCalcs:=True;
on 'reset counters' but what about other forms?
0
karooCommented:
Enej,

inter is correct when stating
>>This is a very heavy burden for windows system

windows probably wont have a problem to handle this but the app can be done with only one form - in view of this the burden is heavy.
i have a small app that loads the q's from a text file, creates the radios dynamically and then saves the result to a result file, if your'e interested give me your email address and i will mail it to you. You can have a look at it and see what I mean.

but lets handle a couple of your probs first,
in Form1 declare the following procedures

  public
    { Public declarations }
    procedure ResetCounters;
    procedure SaveSession(Total, Answ, Corr, Wng, Rem: string);


to reset all the counters on all the forms:

procedure TForm1.ResetCounters;
var
 I, J: integer;
begin
  with Screen do
    for I:= 0 to FormCount -1 do
      for J:= 0 to Forms[I].ComponentCount -1 do
        if Forms[I].Components[J] is TEdit then
          if TEdit(Forms[I].Components[J]).Name = 'Edit5' then
            TEdit(Forms[I].Components[J]).Text := '100'
          else TEdit(Forms[I].Components[J]).Text := '0';
end;

i do not know if this is the best sollution for reseting all the counters, it's all i could come up with now.
to when you want to clear all the counters just call ResetCounters i.e.
procedure TForm1.Button3Click(Sender: TObject);
begin
  ResetCounters;
end;


to log the session to a file:

procedure TForm1.SaveSession(Total, Answ, Corr, Wng, Rem: string);
var
  F: TextFile;
begin
  AssignFile(F, 'c:\My Documents\Temp\QRes.txt');
  Rewrite(F);
  Writeln(F, 'This is the results file for: ....');
  Writeln(F, '');
  Writeln(F, 'Total questions     : ' +Total);
  Writeln(F, 'Questions answered  : ' +Answ);
  Writeln(F, 'Correct             : ' +Corr);
  Writeln(F, 'Incorrect           : ' +Wng);
  Writeln(F, 'Questions remaining : ' +Rem);
  CloseFile(F);
end;

??please elaborate on your last comment.

Regards
Ben:)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EnejAuthor Commented:
Adjusted points to 400
0
EnejAuthor Commented:
How do I tell the App. to automatically
create a *.txt file (Log.txt)on start up (In the folder where the App.

itself resides)?That way I wont have to create it myself and include it

in setup...
As for reseting counters,I think the best solution is to have a button

on every form that points to Form1.So when you click 'Reset counters'

on Form1 :
procedure TForm1.Button2Click(Sender: TObject);
 
UpdateCalcs:=True;
Edit2.Text:='0';
Edit3.Text:='0';
Edit4.Text:='0';
Edit5.Text:='100';
You get the same effect..(Since everything is inherited from that form

onwards).
I will try your version too...
Why am I using Forms?
Well,there are too many questions&pictures
and I am a lousy programmer so I tend to
have it as simple as possible.But I am learning....Step by step...
(By the time I learn everythingI will probably be in pension,ha,ha,ha.)
If I follow right your suggestion for ResetCounters procedure,I have to

name every edit box,then assign it a value?
Type:
with Screen do
    for I:= 0 to FormCount -1 do
      for J:= 0 to Forms[I].ComponentCount -1 do
        if Forms[I].Components[J] is TEdit then
          if TEdit(Forms[I].Components[J]).Name = 'Edit5' then
            TEdit(Forms[I].Components[J]).Text := '100'
          else TEdit(Forms[I].Components[J]).Text := '0';
end;
 begin
if Forms[I].Components[J] is TEdit then
          if TEdit(Forms[I].Components[J]).Name = 'Edit4' then
            TEdit(Forms[I].Components[J]).Text := '0'
          else TEdit(Forms[I].Components[J]).Text := '0';
and so on....(???? OK ?????)Is this right?
So for the logging I assign it to a button,just like Reset Counters?
How do I start to log and how to finish?
Well,I think I have gained sufficient information to help me with my

problems.I surely would like to see the App. you mentioned and it
may be of help...
My e-mail is : skynet@email.si
Thank you for your time and patience...
Thank you!





 
0
karooCommented:
Enej,

thanks for the extra points:) and you are very welcome.

With regards to the app, i'll finish up on a couple of thigs i.e. comments and mail it to you tmw.

>>lousy programmer
not at all!, remember that Delphi has a pretty steep learning curve so it will take time, i'm still learning every day and also still get frustrated from time to time. just enjoy the ride:)

>>I tend to have it as simple as possible
hey, one of my moto's is KISS: Keep It Simple Stupid (hehe), that is an exelent way to approach a problem and you will find simpler sollutions as you get better.

Resetting Counters:
If you are comfortable with your sollution stick to it.
Mine:
>>name every edit box,then assign it a value?
no, this line
>>if TEdit(Forms[I].Components[J]).Name = 'Edit5' then
doesn't assign a new name to the Edit box, it checks if the Name of the EditBox is 'Edit5'

The logic behind ResetCounters is:
1.) Itterate through Screen.FormCount (loop through all the forms you have)
2.) Itterate through all the components on Screen.Forms[I]
3.) Check if the component is a EditBox
4.) If the component is a EditBox check if the name of the EditBox is 'Edit5' or whatever name you assigned to the editBox that contains the remaining q's value (this is because remaining is set to a 100 and not to 0)
5.) If the component is not a EditBox then ignore it (i.e. something like a panel etc.)
5.) If 4 is true then Text := 100
6.) If 4 is false (i.e. any other editbox) then Text := 0


i now have a better idea what you want from the log file try this:

in Form1:

  private
    { private declarations }
    F: TextFile;
  public
    { public declarations }
    // these are public so we can call them from any other unit
    OpenLog;
    WriteLog(Q, Answer, var3...: string);
    CloseLog;

procedure TForm1.OpenLog;
begin
  AssignFile(F, ExtractFilePath(Application.ExeName) + 'Log.txt');
  //rewrite will create a new file if it does not exist or clear an existing file
  Rewrite(F);
  Writeln(F, 'This is the results file for: ....');
  Writeln(F, '');
end;

//change the variables to what you need
procedure TForm1.WriteLog(Q, Answer, var3...: string);
begin
  Writeln(F, 'Question Number     : ' +Q);
  Writeln(F, 'Answer                                      : ' +Answer);
  Writeln(F, 'whatever            : ' +var3);
  ..
  ..
end;

procedure CloseLog;
begin
  CloseFile(F);
end;


to create the log file
procedure TForm1.FormCreate(Sender: TObject);
begin
  //only in TForm1.FormCreate
  OpenLog;
  UpdateCalcs := True;
end;

to write to the log file
lets say for example after a answer has been made on the button1 click on every form

if UpdateCals then
begin
  UpdateCalcs := False;
  WriteLog(var1, var2, var3...);
  ..
  ..
end;

then just before you close the application i.e. close button click etc. call
CloseLog;

glad to be of some help
Ben:)
0
EnejAuthor Commented:
Your version of 'Reset counters'
works,but...It resets my Edit1 also and that must display the total number of questions i.e 100.(?)
Regarding the log I am confused...
I want to do three things:
I want my application to do 3 things:
First:
Create a text file (Log.txt) on startup
in the folder where the application resides.
Second:
I want to activate logging by means of a
button.(I do not want automatic logging)
A simple button (Caption 'Start logging').When clicked it does 2 things:
It writes the contents of current editboxes to file:
>>Start log<<
Questions attempted:
Right answers:
Wrong answers:
and second thing changes the button caption to 'Stop logging'.
When I click on the button that has
'Stop logging' as a caption,logging stops and the contents of editboxes are
written to file:
>>End log<<
Questions attempted:
Right answers:
Wrong answers:
Every time I click 'Start logging' the
contents of the file miust be overwritten.
That is all...Please tell me how to do that...

0
karooCommented:
Enej,

Reset Counters:
this will do the trick for you:

with Screen do
  for I:= 0 to FormCount -1 do
    for J:= 0 to Forms[I].ComponentCount -1 do
      if Forms[I].Components[J] is TEdit then
        if (TEdit(Forms[I].Components[J]).Name = 'Edit1') or
           (TEdit(Forms[I].Components[J]).Name = 'Edit5') then
          TEdit(Forms[I].Components[J]).Text := '100'
        else
          TEdit(Forms[I].Components[J]).Text := '0';


Log:
  public
  { public declarations }
  F: TextFile;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AssignFile(F, ExtractFilePath(Application.ExeName) + 'Log.txt');
  UpdateCalcs := True;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  CloseFile(F);
  //have to close the file at the end of the session
end;

//btLogClick => your log button click
//remember to set the caption of the button to 'Start Logging' at design time

procedure TForm1.btLogClick(Sender: TObject);
const
  STARTLOG = 'Start Logging';
  STOPLOG = 'Stop Logging';
begin
  if btLog.Caption = STARTLOG then
  begin
    Rewrite(F);
    btLog.Caption := STOPLOG;
  end;
  else btLog.Caption := STARTLOG;
  WriteLn(F, '');
  WriteLn(F, 'Questions Attempted : ' + EditBox2.Text);
  WriteLn(F, 'Right Answers : ' + EditBox3.Text);
  WriteLn(F, 'Wrong Answers : ' +editBox4.Text);
end;

i think this will solve your logging:)

Regards
Ben:)
0
EnejAuthor Commented:
Your way does not work (I/O error 103).
I changed to:
procedure TForm1.btLogClick(Sender: TObject);
begin
  if btLog.Caption = 'Start logging' then
  begin
    Rewrite(F);
    btLog.Caption := 'Stop logging';
   end
  else btLog.Caption := 'Start logging';
  WriteLn(F, '');
  WriteLn(F, 'Questions Attempted : ' + Edit2.Text);
  WriteLn(F, 'Right Answers : ' + Edit3.Text);
  WriteLn(F, 'Wrong Answers : ' + edit4.Text);
end;
And it works!
These are the contents of my  Log.txt file:
Questions Attempted : 3
Right Answers : 3
Wrong Answers : 0

Questions Attempted : 6
Right Answers : 3
Wrong Answers : 3

I did a little trick i.e tried to cheat.
I answered some questions before logging
and they displayed in the first 3 lines.
After I clicked 'Stop logging' I had the
total count at the 3 bottom lines.Cute!

ERROR !!!
When I close my application without doing anything I raise an exception error.Delphi Points to on Form Destroy
event CloseFile(F);
Well,we are trying to close a file that has not even been opened,am I right?
0
EnejAuthor Commented:
Anyway,there is something that I do not understand in this application...
When I click answer on form 1 and then go to form 2 and click an answer there,everything works ok.Now when I go back from Form2 to form1 and
try and click an answer it does not add to editboxes(Right!).But then I go to form2 and click answer it WILL add to editboxes.(And is not supposed to)???
Here is the code for both Units:
********************************************************************
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls;

type
  TForm1 = class(TForm)
    RadioGroup1: TRadioGroup;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Button2: TButton;
    Button3: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    procedure Button1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    UpdateCalcs: Boolean;
   
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Checked : Boolean;
implementation

uses Unit2;

 {$R Slike.res}

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True
  else if RadioButton2.Checked = true then
    Correct:= False
  else if RadioButton3.Checked = true then
    Correct:= False
  else begin
  ShowMessage('No selection has been made');
  Exit;
  end;
  if UpdateCalcs then
  begin
    UpdateCalcs:= False;

    Answ := StrToInt(Edit2.Text);
    Corr := StrToInt(Edit3.Text);
    Wng  := StrToInt(Edit4.Text);
    Rem  := StrToInt(Edit5.Text);

    Inc(Answ);
    if Correct then Inc(Corr)
    else Inc(Wng);
    Dec(Rem);

    Edit2.Text := IntToStr(Answ);
    Edit3.Text := IntToStr(Corr);
    Edit4.Text := IntToStr(Wng);
    Edit5.Text := IntToStr(Rem);
  end;

  if Correct then
    ShowMessage('Right answer !')
  else ShowMessage('Wrong answer !')
end;



Procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Form2.Edit1.Text := Edit1.Text;
Form2.Edit2.Text := Edit2.Text;
Form2.Edit3.Text := Edit3.Text;
Form2.Edit4.Text := Edit4.Text;
Form2.Edit5.Text := Edit5.Text;
Form2.Show;
Form1.Hide;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
If MessageDlg('Do you really want to exit?',mtInformation,
  [mbYes, mbNo],0) = mrYes then
   Application.Terminate
 
end;

procedure TForm1.FormShow(Sender: TObject);
Var i:Integer;
begin
 for i := 1 to 3 do
  TRadioButton(FindComponent('RadioButton'+inttostr(i))).
  Checked := false;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
UpdateCalcs:= True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UpdateCalcs:=True;
Edit2.Text:='0';
Edit3.Text:='0';
Edit4.Text:='0';
Edit5.Text:='100';
end;

procedure TForm1.BitBtn1Click(Sender: TObject);

begin
   If MessageDlg('This is the last frame.Next question ?',mtInformation,
  [mbYes, mbNo],0) = mrYes then
  Begin
  Form2.Edit1.Text := Edit1.Text;
  Form2.Edit2.Text := Edit2.Text;
  Form2.Edit3.Text := Edit3.Text;
  Form2.Edit4.Text := Edit4.Text;
  Form2.Edit5.Text := Edit5.Text;
  Form2.Show;
  Form1.Hide;
  end;


end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
If MessageDlg('Do you really want to exit?',mtInformation,
  [mbYes, mbNo],0) = mrYes then
   Application.Terminate else
   Action := caNone;
end;

end.
********************************************************
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls;

type
  TForm2 = class(TForm)
    RadioGroup1: TRadioGroup;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Button3: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    procedure Button1Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    UpdateCalcs : Boolean;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  Checked : Boolean;
implementation

uses Unit1;
 
{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True
  else if RadioButton2.Checked = true then
    Correct:= False
  else if RadioButton3.Checked = true then
    Correct:= False
  else begin
  ShowMessage('No selection has been made');
  Exit;
  end;
  if UpdateCalcs then
  begin
    UpdateCalcs:= False;

    Answ := StrToInt(Edit2.Text);
    Corr := StrToInt(Edit3.Text);
    Wng  := StrToInt(Edit4.Text);
    Rem  := StrToInt(Edit5.Text);

    Inc(Answ);
    if Correct then Inc(Corr)
    else Inc(Wng);
    Dec(Rem);

    Edit2.Text := IntToStr(Answ);
    Edit3.Text := IntToStr(Corr);
    Edit4.Text := IntToStr(Wng);
    Edit5.Text := IntToStr(Rem);
  end;

  if Correct then
    ShowMessage('Right answer !')
  else ShowMessage('Wrong answer !')
end;




procedure TForm2.BitBtn1Click(Sender: TObject);
begin
Form1.Edit1.Text := Edit1.Text;
Form1.Edit2.Text := Edit2.Text;
Form1.Edit3.Text := Edit3.Text;
Form1.Edit4.Text := Edit4.Text;
Form1.Edit5.Text := Edit5.Text;
Form1.Show;
Form2.Hide;
end;

procedure TForm2.FormShow(Sender: TObject);
Var i:Integer;
begin
 for i := 1 to 3 do
  TRadioButton(FindComponent('RadioButton'+inttostr(i))).
  Checked := false;
  UpdateCalcs:=True;
end;

procedure TForm2.Button3Click(Sender: TObject);
begin
If MessageDlg('Do you really want to exit?',mtInformation,
  [mbYes, mbNo],0) = mrYes then
   Application.Terminate
   
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
If MessageDlg('Do you really want to exit?',mtInformation,
  [mbYes, mbNo],0) = mrYes then
   Application.Terminate   else
   Action := caNone;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin

end;

end.
0
karooCommented:
hi Enej :)

oops, the I/O error,
we have to rewrite the the file just after we assigned it:
procedure TForm1.FormCreate(Sender: TObject);
begin
  AssignFile(F, ExtractFilePath(Application.ExeName) + 'Log.txt');
  Rewrite(F); //add this line
  UpdateCalcs := True;
end;

so now you can't cheat the q's eh?:)

regarding the second comment:
In Form2 you set UpdateCalcs true in the FormShow event which means that every time Form2.Show is called UpdateCalcs will be true again... see the logic here? UpdateCalcs must only be True *once* for every Form Question tha is why we initially set it to True on the FormCreate event (which will only occur once when the form is created)
So remove UpdateCalcs := True from the TForm2.FormShow event and add it to TForm2.FormCreate event.

see you are getting pretty comfortable with Delphi here:)

will send the q app to you later tday

Regards
Ben:)
0
EnejAuthor Commented:
Free at last....
     Free at last....
          Free at last...
50 Points for your time!
Thank you!
0
interCommented:
good work pals ;-)
regards,
igor
0
karooCommented:
thanks igor,

that is a big compliment because i follow your opinions here in EE and rate them very high.

Regards
Ben.
0
EnejAuthor Commented:
I am affraid it is not over yet....
(Devil is in details they say...)
OK,now I have a fully functional 3 forms
and they function perfect,The problem is in the button 'Reset counters'.
The code for the button:
*********************
public
   F: TextFile;
   procedure ResetCounters;
***********************
Procedure is :
procedure TForm1.ResetCounters;
var
 I, J: integer;
begin
  with Screen do
  for I:= 0 to FormCount -1 do
    for J:= 0 to Forms[I].ComponentCount -1 do
      if Forms[I].Components[J] is TEdit then
        if (TEdit(Forms[I].Components[J]).Name = 'Edit1') or
           (TEdit(Forms[I].Components[J]).Name = 'Edit5') then
          TEdit(Forms[I].Components[J]).Text := '100'
        else
          TEdit(Forms[I].Components[J]).Text := '0';
end;
******************
On click for the 'Reset counters' button is :
procedure TForm1.Button3Click(Sender: TObject);
begin
UpdateCalcs:=True;
ResetCounters;
end;

*************************
Now this is what happens:
When I switch between forms everything functions as it should.No more than 1 answer is allowed.Beautifull!
Then I click on the 'Reset Counters'
button (Form1) and this is what happens:
On Form1 (where that button resides),I
can answer a question and editboxes will count.But not on other forms!!!
Over there I can click anything I like but no change in editboxes!
This is the code for Form1:
**************************************

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Buttons;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    Button1: TButton;
    Button2: TButton;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Button3: TButton;
    Panel5: TPanel;
    Image1: TImage;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    StaticText4: TStaticText;
    StaticText5: TStaticText;
    StaticText6: TStaticText;
    Panel6: TPanel;
    StaticText7: TStaticText;
    Panel7: TPanel;
    StaticText8: TStaticText;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Button4: TButton;
    Button5: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    private
     UpdateCalcs: Boolean;
   public
   F: TextFile;
   procedure ResetCounters;
   end;
 var
  Form1: TForm1;
  CHECKED :BOOLEAN;
implementation

uses Unit2;
  {$R Slike.res}
{$R *.DFM}
procedure TForm1.ResetCounters;
var
 I, J: integer;
begin
  with Screen do
  for I:= 0 to FormCount -1 do
    for J:= 0 to Forms[I].ComponentCount -1 do
      if Forms[I].Components[J] is TEdit then
        if (TEdit(Forms[I].Components[J]).Name = 'Edit1') or
           (TEdit(Forms[I].Components[J]).Name = 'Edit5') then
          TEdit(Forms[I].Components[J]).Text := '100'
        else
          TEdit(Forms[I].Components[J]).Text := '0';
   end;

procedure TForm1.Button1Click(Sender: TObject);
 var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True
  else if RadioButton2.Checked = true then
    Correct:= False
  else if RadioButton3.Checked = true then
    Correct:= False
  else begin
    ShowMessage('No selection has been made');
    Exit;
  end;
  if UpdateCalcs then
  begin
    UpdateCalcs:= False;
    Answ := StrToInt(Edit2.Text);
    Corr := StrToInt(Edit3.Text);
    Wng  := StrToInt(Edit4.Text);
    Rem  := StrToInt(Edit5.Text);
    Inc(Answ);
    if Correct then Inc(Corr)
    else Inc(Wng);
    Dec(Rem);
    Edit2.Text := IntToStr(Answ);
    Edit3.Text := IntToStr(Corr);
    Edit4.Text := IntToStr(Wng);
    Edit5.Text := IntToStr(Rem);
  end;
  if Correct then
    ShowMessage('Right answer !')
  else ShowMessage('Wrong answer !')
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Application.Terminate
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Form2.Edit1.Text := Edit1.Text;
Form2.Edit2.Text := Edit2.Text;
Form2.Edit3.Text := Edit3.Text;
Form2.Edit4.Text := Edit4.Text;
Form2.Edit5.Text := Edit5.Text;
Form2.Show;
Form1.Hide;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
 begin
   If MessageDlg('This is the last frame.Next question ?',mtInformation,
  [mbYes, mbNo],0) = mrYes then
  Begin
  Form2.Edit1.Text := Edit1.Text;
  Form2.Edit2.Text := Edit2.Text;
  Form2.Edit3.Text := Edit3.Text;
  Form2.Edit4.Text := Edit4.Text;
  Form2.Edit5.Text := Edit5.Text;
  Form2.Show;
  Form1.Hide;
  end;

end;
procedure TForm1.FormShow(Sender: TObject);
Var i:Integer;
begin
 for i := 1 to 3 do
  TRadioButton(FindComponent('RadioButton'+inttostr(i))).
  Checked := false;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
UpdateCalcs:=True;
ResetCounters;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AssignFile(F, ExtractFilePath(Application.ExeName) + 'Log.txt');
 Rewrite(F);
 UpdateCalcs := True;
 end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseFile(F);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if Button4.Caption = 'Start logging' then
  begin
    Rewrite(F);
    Button4.Caption := 'Stop logging';
   end
  else Button4.Caption := 'Start logging';
  WriteLn(F, '');
  WriteLn(F, 'Questions Attempted : ' + Edit2.Text);
  WriteLn(F, 'Right Answers : ' + Edit3.Text);
  WriteLn(F, 'Wrong Answers : ' + edit4.Text);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
winexec('notepad.exe Log.txt',SW_SHOWDEFAULT);
end;

end.
Code for Form2:
***************************************
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, ExtCtrls, StdCtrls;

type
  TForm2 = class(TForm)
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    Button1: TButton;
    Button2: TButton;
    Panel1: TPanel;
    Panel2: TPanel;
    StaticText4: TStaticText;
    Edit1: TEdit;
    Panel3: TPanel;
    StaticText6: TStaticText;
    Edit3: TEdit;
    Panel4: TPanel;
    StaticText5: TStaticText;
    Edit2: TEdit;
    Panel6: TPanel;
    StaticText7: TStaticText;
    Edit4: TEdit;
    Panel7: TPanel;
    StaticText8: TStaticText;
    Edit5: TEdit;
    Button3: TButton;
    Panel5: TPanel;
    Image1: TImage;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
   UpdateCalcs:Boolean; { Private declarations }
  public
   procedure ResetCounters;
     { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1, Unit3;

{$R *.DFM}
procedure TForm2.ResetCounters;
var
 I, J: integer;
begin
  with Screen do
  for I:= 0 to FormCount -1 do
    for J:= 0 to Forms[I].ComponentCount -1 do
      if Forms[I].Components[J] is TEdit then
        if (TEdit(Forms[I].Components[J]).Name = 'Edit1') or
           (TEdit(Forms[I].Components[J]).Name = 'Edit5') then
          TEdit(Forms[I].Components[J]).Text := '100'
        else
          TEdit(Forms[I].Components[J]).Text := '0';
 
  end;



procedure TForm2.BitBtn1Click(Sender: TObject);
begin
Form1.Edit1.Text := Edit1.Text;
Form1.Edit2.Text := Edit2.Text;
Form1.Edit3.Text := Edit3.Text;
Form1.Edit4.Text := Edit4.Text;
Form1.Edit5.Text := Edit5.Text;

Form1.Show;
Form2.Hide;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
Application.Terminate
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True
  else if RadioButton2.Checked = true then
    Correct:= False
  else if RadioButton3.Checked = true then
    Correct:= False

  else begin
    ShowMessage('No selection has been made');
    Exit;
  end;

  if UpdateCalcs then
  begin
    UpdateCalcs:= False;

    Answ := StrToInt(Edit2.Text);
    Corr := StrToInt(Edit3.Text);
    Wng  := StrToInt(Edit4.Text);
    Rem  := StrToInt(Edit5.Text);

    Inc(Answ);
    if Correct then Inc(Corr)
    else Inc(Wng);
    Dec(Rem);

    Edit2.Text := IntToStr(Answ);
    Edit3.Text := IntToStr(Corr);
    Edit4.Text := IntToStr(Wng);
    Edit5.Text := IntToStr(Rem);
  end;

  if Correct then
    ShowMessage('Right answer !')
  else ShowMessage('Wrong answer !')
end;


procedure TForm2.FormShow(Sender: TObject);
Var i:Integer;
begin
 for i := 1 to 3 do
  TRadioButton(FindComponent('RadioButton'+inttostr(i))).
  Checked := false;
  Edit1.Text:= '100';
 

end;

procedure TForm2.BitBtn2Click(Sender: TObject);
begin
Form3.Edit1.Text := Edit1.Text;
Form3.Edit2.Text := Edit2.Text;
Form3.Edit3.Text := Edit3.Text;
Form3.Edit4.Text := Edit4.Text;
Form3.Edit5.Text := Edit5.Text;
Form3.Show;
Form2.Hide;

end;

procedure TForm2.Button3Click(Sender: TObject);
begin
ResetCounters;
 UpdateCalcs:=True;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
UpdateCalcs:=True;
end;
end.
**************************************
Code for Form3 :
unit Unit3;

interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, ExtCtrls, StdCtrls;

type
  TForm3 = class(TForm)
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    Button1: TButton;
    Button2: TButton;
    Panel1: TPanel;
    Panel2: TPanel;
    StaticText4: TStaticText;
    Edit1: TEdit;
    Panel3: TPanel;
    StaticText6: TStaticText;
    Edit3: TEdit;
    Panel4: TPanel;
    StaticText5: TStaticText;
    Edit2: TEdit;
    Panel6: TPanel;
    StaticText7: TStaticText;
    Edit4: TEdit;
    Panel7: TPanel;
    StaticText8: TStaticText;
    Edit5: TEdit;
    Button3: TButton;
    Panel5: TPanel;
    Image1: TImage;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
  UpdateCalcs : Boolean;  { Private declarations }
  public
   procedure ResetCounters;
     { Public declarations }
  end;
var
  Form3: TForm3;

implementation

uses Unit2;

{$R *.DFM}
 procedure TForm3.ResetCounters;
var
 I, J: integer;
begin
  with Screen do
  for I:= 0 to FormCount -1 do
    for J:= 0 to Forms[I].ComponentCount -1 do
      if Forms[I].Components[J] is TEdit then
        if (TEdit(Forms[I].Components[J]).Name = 'Edit1') or
           (TEdit(Forms[I].Components[J]).Name = 'Edit5') then
          TEdit(Forms[I].Components[J]).Text := '100'
        else
          TEdit(Forms[I].Components[J]).Text := '0';
 end;
procedure TForm3.BitBtn1Click(Sender: TObject);
begin
Form2.Edit1.Text := Edit1.Text;
Form2.Edit2.Text := Edit2.Text;
Form2.Edit3.Text := Edit3.Text;
Form2.Edit4.Text := Edit4.Text;
Form2.Edit5.Text := Edit5.Text;
Form2.Show;
Form3.Hide;
end;

procedure TForm3.Button1Click(Sender: TObject);
var
  Answ, Corr, Wng, Rem: Integer;
  Correct: Boolean;
begin
  if RadioButton1.Checked = true then
    Correct:= True
  else if RadioButton2.Checked = true then
    Correct:= False
  else if RadioButton3.Checked = true then
    Correct:= False

  else begin
    ShowMessage('No selection has been made');
    Exit;
  end;

  if UpdateCalcs then
  begin
    UpdateCalcs:= False;

    Answ := StrToInt(Edit2.Text);
    Corr := StrToInt(Edit3.Text);
    Wng  := StrToInt(Edit4.Text);
    Rem  := StrToInt(Edit5.Text);

    Inc(Answ);
    if Correct then Inc(Corr)
    else Inc(Wng);
    Dec(Rem);

    Edit2.Text := IntToStr(Answ);
    Edit3.Text := IntToStr(Corr);
    Edit4.Text := IntToStr(Wng);
    Edit5.Text := IntToStr(Rem);
  end;

  if Correct then
    ShowMessage('Right answer !')
  else ShowMessage('Wrong answer !')
end;


procedure TForm3.FormShow(Sender: TObject);
Var i:Integer;
begin
 for i := 1 to 3 do
  TRadioButton(FindComponent('RadioButton'+inttostr(i))).
  Checked := false;
 
 

end;

procedure TForm3.Button3Click(Sender: TObject);
begin
ResetCounters;
 UpdateCalcs:= True;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
UpdateCalcs:=True;
end;

end.
Same thing happens when I click resetcounters on other Forms....
What can be wrong?
0
karooCommented:
Hi Enej, :)

OK here goes,

Not only do we have to reset the counters in ResetCounters but we also have to set the UpdateCalcs variable to True for every form.

first off UpdateCalcs can't be a private variable anymore because we want to reset it from Form1 so
move:
private
  UpdateCalcs: Boolean;
to:
public
  { public declarations }
  //*every* unit
  UpdateCalcs: Boolean;
  //Resetcounters *only* in unit1
  procedure ResetCounters;


here is the modified ResetCounters:

procedure TForm1.ResetCounters;
var
 I, J: integer;
begin
  UpdateCalcs := True;
  Form2.UpdateCalcs := True;
  Form3.UpdateCalcs := True;
  with Screen do
  for I:= 0 to FormCount -1 do
  ..
  ..
end;

lastly Enej, you do *not* need ResetCounters procedure in Every form, Unit1 is the only unit with this procedure declared and implemented.
The reset button click in form2 and form3 must look like this:

procedure TForm2.Button3Click(Sender: TObject);
begin
  Form1.ResetCounters;
end;

procedure TForm3.Button3Click(Sender: TObject);
begin
  Form1.ResetCounters;
end;

you can remove resetcounters procedure from Form2 and Form3, form1 will take care of resetting the counters.

>>(Devil is in details they say...)
absolutely:)
it takes 20% of the time to do 80% of the work
and 80% of the time to do the next 20%

btw have you had a look at the app i sent you?

Regards
Ben:)
0
EnejAuthor Commented:
Talking of the devil...
Did as you suggested but now App. stops
on :
procedure TForm1.FormCreate(Sender: TObject);
begin
AssignFile(F, ExtractFilePath(Application.ExeName) + 'Log.txt');
 Rewrite(F);
 UpdateCalcs := True;
end;
It stops on Rewrite(F);
(??????????????????????????????)
0
EnejAuthor Commented:
Ahhh...forget it!
Somehow Delphi "doubled" my project
(maybe even my doing) and
god knows which *.exe was run....(A big-mix)I deleted one and one remaining functions  perfect.
It WORKS !!!
Alleluiah....
I have looked up your app. and will mail you some Q if I may....It is very interesting indeed...
Thank you!
0
karooCommented:
Enej,

>>I have looked up your app. and will mail you some Q if I may

absolutely:)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.