Creating a questionnaire

jeff00
jeff00 used Ask the Experts™
on
Hi there,

Does anybody has an example on creating a questionnaire in Delphi?

I've to create a questionnaire for about 10 questions and when finishing the questionnaire return the result.

Every question should be on a new form and the questions are multiple choice.

I saw some suggestions about using an ini file. But maybe somebody has some example.

Thanx for your help!

Jeff
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

Here is a simple example using INI file. There are a TLabel (question test), TListBox (possible answers) and TButton on a form.

The code is:
---
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Label1: TLabel;
    function ReadQuestion(i: integer): boolean;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  IniName = 'q.ini';

var
  Form1: TForm1;
  qNumber: integer; // current question
  qAnswer: integer; // the right answer
  qResults: TStringList; // results

implementation

{$R *.DFM}

uses IniFiles;

function TForm1.ReadQuestion(i: integer): boolean;
begin
  result := true;
  with TIniFile.Create(ExtractFilePath(Application.ExeName)+IniName) do begin
    Label1.Caption := ReadString(IntToStr(i),'qText','');
    if Label1.Caption = '' then begin // there are no more questions
      result := false;
      exit;
    end;
    qAnswer := StrToInt(ReadString(IntToStr(i),'qA','0'));
    ReadSection(IntToStr(i)+'a', ListBox1.Items);
    Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  qNumber := 1;
  qResults := TStringList.Create;
  ReadQuestion(qNumber);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  qResults.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if ListBox1.Selected[qAnswer - 1] then begin // right answer
    qResults.Add('question ' + IntToStr(qNumber) + ' - right');
  end else begin
    qResults.Add('question ' + IntToStr(qNumber) + ' - wrong');
  end;
  qNumber := qNumber + 1;
  if not ReadQuestion(qNumber) then begin // show results
    ShowMessage(qResults.Text);
    Close;
  end;
end;

end.
---
and the ini file looks like (qA is the number of the right answer):

[1]
qText=question 1
qA=2
[1a]
qPA1=answer a1
qPA2=answer a2

[2]
qText=question 2
qA=1
[2a]
qPA1=answer b1
qPA2=answer b2
qPA3=answer b3

Regards, Geo

Author

Commented:
Geobul,

Thanx! for your fast reply!

It worked!

Thanx!
Jeff

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial