Форум по Delphi программированию

Delphi Sources



Вернуться   Форум по Delphi программированию > Все о Delphi > [ "Начинающим" ]
Ник
Пароль
Регистрация <<         Правила форума         >> FAQ Пользователи Календарь Поиск Сообщения за сегодня Все разделы прочитаны

Ответ
 
Опции темы Поиск в этой теме Опции просмотра
  #1  
Старый 22.03.2009, 19:07
Improviz Improviz вне форума
Прохожий
 
Регистрация: 25.02.2009
Сообщения: 22
Репутация: 10
По умолчанию Как связать файлы с программой?

Здравствуйте!
В программе вместо вопросов и ответов из файла выводятся "Label".
Вот код программы
Код:
unit tester_;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, ExtCtrls,
  jpeg;

type
  TForm1 = class(TForm)
    // вопрос
    Label5: TLabel;
    // альтернативные ответы
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    // радиокнопки выбора ответа
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    Image1: TImage;
    RadioButton5: TRadioButton;
    Button1: TButton;
    Panel1: TPanel;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure RadioButtonClick(Sender: TObject);
    // Эти объявления вставлены сюда вручную
    procedure Info;
    procedure VoprosToScr;
    procedure ShowPicture; // выводит иллюстрацию
    procedure ResetForm;   // "очистка" формы перед выводом очередного вопроса
    procedure Itog;
    procedure FormCreate(Sender: TObject); // результат тестирования
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;     // форма
implementation
{$R *.DFM}
const
   N_LEV=4; // четыре уровня оценки
   N_ANS=4; // четыре варианта ответов
var
  f:TextFile;
  fn:string; // имя файла вопросов
  level:array[1..N_LEV] of integer; // сумма, соответствующая уровню
  mes:array[1..N_LEV] of string;    // сообщение, соответствующее уровню
  score:array[1..N_ANS] of integer; // оценка за выбор ответа
  summa:integer;  // набрано очков
  vopros:integer; // номер текущего вопроса
  otv:integer;    // номер выбранного ответа
// вывод информации о тесте
procedure Tform1.Info;
var
   s,buf:string;
begin
   readln(f,s);
   Form1.Caption := s;
   buf:='';
   repeat
         readln(f,s);
         if s[1] <> '.'
            then buf := buf +s + #13;
   until s[1] ='.';
   Label5.caption:=buf;
end;
// прочитать информацию об оценках за тест
Procedure GetLevel;
var
   i:integer;
   buf:string;
begin
     i:=1;
     repeat
         readln(f,buf);
         if buf[1] <> '.' then begin
            mes[i]:=buf;        // сообщение
            readln(f,level[i]); // оценка
            i:=i+1;
         end;
     until buf[1]='.';
end;
// масштабирование иллюстрации
Procedure TForm1.ShowPicture;
var
  w,h: integer; // максимально возможные размеры картинки
begin
  // вычислить допустимые размеры картинки
  w:=ClientWidth-10;
  h:=ClientHeight
         - Panel1.Height -10
         - Label5.Top
         - Label5.Height - 10;
  // вопросы
  if Label1.Caption <> ''
     then h:=h-Label1.Height-10;
  if Label2.Caption <> ''
     then h:=h-Label2.Height-10;
  if Label3.Caption <> ''
     then h:=h-Label3.Height-10;
  if Label4.Caption <> ''
     then h:=h-Label4.Height-10;
  // если размер картинки меньше w на h,
  // то она не масштабируется
  Image1.Top:=Form1.Label5.Top+Label5.Height+10;
  if Image1.Picture.Bitmap.Height > h
        then Image1.Height:=h
        else Image1.Height:= Image1.Picture.Height;
  if Image1.Picture.Bitmap.Width > w
        then Image1.Width:=w
        else Image1.Width:=Image1.Picture.Width;
  Image1.Visible := True;
end;
// вывести вопрос
Procedure TForm1.VoprosToScr;
  var
   i:integer;
   s,buf:string;
   ifn:string; // файл иллюстрации
  begin
     vopros:=vopros+1;
     caption:='Вопрос ' + IntToStr(vopros);
     // прочитать вопрос
     buf:='';
     repeat
           readln(f,s);
           if (s[1] <> '.') and (s[1] <> '\')
              then buf:=buf+s+' ';
     until (s[1] ='.') or (s[1] ='\');
     Label5.caption:=buf;  // вывести вопрос
     { Иллюстрацию прочитаем, но выведем только после
       того, как прочитаем альтернативные ответы
       и определим максимально возможный размер
       области формы, который можно использовать
       для ее вывода.}
     if s[1] <> '\'
       then Image1.Tag:=0 // к вопросу нет иллюстрации
       else // к вопросу есть иллюстрация
        begin
          Image1.Tag:=1;
          ifn:=copy(s,2,length(s));
          try
            Image1.Picture.LoadFromFile(ifn);
            except
              on E:EFOpenError do
                 Image1.Tag:=0;
          end;
        end;
     // Читаем варианты ответов
     i:=1;
     repeat
        buf:='';
        repeat // читаем текст варианта ответа
          readln(f,s);
          if (s[1]<>'.') and (s[1] <> ',')
                then buf:=buf+s+' ';
        until (s[1]=',')or(s[1]='.');
        // прочитан альтернативный ответ
        score[i]:= StrToInt(s[2]);
        case i of
            1: Label1.caption:=buf;
            2: Label2.caption:=buf;
            3: Label3.caption:=buf;
            4: Label4.caption:=buf;
        end;
        i:=i+1;
     until s[1]='.';
     // здесь прочитана иллюстрация и альтернативные ответы
     // текст вопроса уже выведен
     if Image1.Tag =1  // есть иллюстрация к вопросу
        then ShowPicture;
     // вывод альтернативных ответов
     if Form1.Label1.Caption <> ''
     then begin
        if Form1.Image1.Tag =1
            then Label1.top:=Image1.Top+Image1.Height+10
            else Label1.top:=Label5.Top+Label5.Height+10;
        RadioButton1.top:=Label1.top;
        Label1.visible:=TRUE;
        RadioButton1.visible:=TRUE;
     end;
     if Form1.Label2.Caption <> ''
     then begin
        Label2.top:=Label1.top+ Label1.height+10;
        RadioButton2.top:=Label2.top;
        Label2.visible:=TRUE;
        RadioButton2.visible:=TRUE;
     end;
     if Form1.Label3.Caption <> ''
     then begin
        Label3.top:=Label2.top+ Label2.height+10;
        RadioButton3.top:=Label3.top;
        Label3.visible:=TRUE;
        RadioButton3.visible:=TRUE;
     end;
     if Form1.Label4.Caption <> ''
     then begin
        Label4.top:=Label3.top+ Label3.height+10;
        RadioButton4.top:=Label4.top;
        Label4.visible:=TRUE;
        RadioButton4.visible:=TRUE;
     end;
  end;
Procedure TForm1.ResetForm;
begin // сделать невидимыми все метки и радиокнопки
     Label1.Visible:=FALSE;
     Label1.caption:='';
     Label1.width:=ClientWidth-Label1.left-5;
     RadioButton1.Visible:=FALSE;
     Label2.Visible:=FALSE;
     Label2.caption:='';
     Label2.width:=ClientWidth-Label2.left-5;
     RadioButton2.Visible:=FALSE;
     Label3.Visible:=FALSE;
     Label3.caption:='';
     Label3.width:=ClientWidth-Label3.left-5;
     RadioButton3.Visible:=FALSE;
     Label4.Visible:=FALSE;
     Label4.caption:='';
     Label4.width:=ClientWidth-Label4.left-5;
     RadioButton4.Visible:=FALSE;
     Label5.width:=ClientWidth-Label5.left-5;
     Image1.Visible:=FALSE;
end;
// определение достигнутого уровня
procedure TForm1.Itog;
  var
   i:integer;
   buf:string;
  begin
   buf:='';
   buf:='Результаты тестирования'+ #13
		+'Всего баллов: '+ IntToStr(summa);
   i:=1;
   while (summa < level[i]) and (i<N_LEV) do
         i:=i+1;
   buf:=buf+ #13+mes[i];
   Label5.Top:=20;
   Label5.caption:=buf;
  end;
procedure TForm1.FormActivate(Sender: TObject);
  begin
     ResetForm;
     if ParamCount = 0
     then begin
             Label5.caption:= 'Не задан файл вопросов теста.';
             Button1.caption:='Ok';
             Button1.tag:=2;
             Button1.Enabled:=TRUE
          end
     else begin
          fn := ParamStr(1);
          assignfile(f,fn);
          try
            reset(f);
          except
            on EFOpenError do
               begin
                    ShowMessage('Файл теста '+fn+' не найден.');
                    Button1.caption:='Ok';
                    Button1.tag:=2;
                    Button1.Enabled:=TRUE;
                    exit;
               end;
          end;
          Info; // прочитать и вывести информацию о тесте
          GetLevel;    // прочитать информацию об уровнях оценок
     end;
end;
// щелчок на кнопке Button1
procedure TForm1.Button1Click(Sender: TObject);
  begin
     case Button1.tag of
       0: begin
               Button1.caption:='Дальше';
               Button1.tag:=1;
               RadioButton5.Checked:=TRUE;
               // вывод первого вопроса
               Button1.Enabled:=False;
               ResetForm;
               VoprosToScr;
          end;
       1: begin // вывод остальных вопросов
               summa:=summa+score[otv];
               RadioButton5.Checked:=TRUE;
               Button1.Enabled:=False;
               ResetForm;
               if not eof(f)
                  then VoprosToScr
                  else
                     begin
                        summa:=summa+score[otv];
                        closefile(f);
                        Button1.caption:='Ok';
                        Form1.caption:='Результат';
                        Button1.tag:=2;
                        Button1.Enabled:=TRUE;
                        Itog; // вывести результат
                     end;
          end;
       2: begin // завершение работы
            Form1.Close;
          end;
     end;
end;
// Процедура обработки события OnClick
// для компонентов RadioButton1-RadioButton4
procedure TForm1.RadioButtonClick(Sender: TObject);
begin
  if sender = RadioButton1
  then otv:=1
  else if sender = RadioButton1
          then otv:=2
          else if sender = RadioButton3
                  then otv:=3
                  else otv:=4;
  Button1.enabled:=TRUE;
end;
// обеспечивает настройку компонентов
procedure TForm1.FormCreate(Sender: TObject);
begin
    Image1.AutoSize := False;
    Image1.Proportional := True;
    RadioButton1.Visible := False;
end;
end.
Помогите, пожалуйста, решить эту проблему...
Ответить с цитированием
  #2  
Старый 22.03.2009, 20:25
san-46 san-46 вне форума
Активный
 
Регистрация: 25.04.2008
Сообщения: 383
Репутация: 33
По умолчанию

Чтобы в этой помойке что-то понять, как минимум, нужен еще файл с расширением .dfm
__________________
Не забывайте делать резервные копии
Ответить с цитированием
  #3  
Старый 23.03.2009, 08:12
Improviz Improviz вне форума
Прохожий
 
Регистрация: 25.02.2009
Сообщения: 22
Репутация: 10
По умолчанию

Вот файл tester.dfm
Код:
object Form1: TForm1
  Left = 204
  Top = 199
  Width = 696
  Height = 435
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  DesignSize = (
    688
    404)
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 96
    Top = 104
    Width = 32
    Height = 13
    Caption = 'Label1'
  end
  object Label2: TLabel
    Left = 96
    Top = 144
    Width = 32
    Height = 13
    Caption = 'Label2'
  end
  object Label3: TLabel
    Left = 96
    Top = 184
    Width = 32
    Height = 13
    Caption = 'Label3'
  end
  object Label4: TLabel
    Left = 96
    Top = 216
    Width = 32
    Height = 13
    Caption = 'Label4'
  end
  object Label5: TLabel
    Left = 64
    Top = 16
    Width = 32
    Height = 13
    Caption = 'Label5'
  end
  object Image1: TImage
    Left = 56
    Top = 56
    Width = 41
    Height = 41
  end
  object RadioButton1: TRadioButton
    Left = 64
    Top = 104
    Width = 17
    Height = 17
    Caption = 'RadioButton1'
    TabOrder = 0
  end
  object RadioButton2: TRadioButton
    Left = 64
    Top = 144
    Width = 17
    Height = 17
    Caption = 'RadioButton2'
    TabOrder = 1
  end
  object RadioButton3: TRadioButton
    Left = 64
    Top = 184
    Width = 17
    Height = 17
    Caption = 'RadioButton3'
    TabOrder = 2
  end
  object RadioButton4: TRadioButton
    Left = 64
    Top = 216
    Width = 17
    Height = 17
    Caption = 'RadioButton4'
    TabOrder = 3
  end
  object RadioButton5: TRadioButton
    Left = 64
    Top = 248
    Width = 113
    Height = 17
    Caption = 'RadioButton5'
    TabOrder = 4
  end
  object Button1: TButton
    Left = 288
    Top = 315
    Width = 75
    Height = 25
    Anchors = [akBottom]
    Caption = 'OK'
    TabOrder = 5
  end
  object Panel1: TPanel
    Left = 0
    Top = 359
    Width = 688
    Height = 45
    Align = alBottom
    TabOrder = 6
  end
end
Ответить с цитированием
  #4  
Старый 23.03.2009, 08:57
san-46 san-46 вне форума
Активный
 
Регистрация: 25.04.2008
Сообщения: 383
Репутация: 33
По умолчанию

А файл из которого читаем вопросы и оценки где? Как в нем строки устроены попробуй догадайся, особенно, если никогда его не видел.
И где картинка (что это за картинка такая секретная)?
И dfm файл не совсем от этого проекта - в нем нет ни одного присвоенного события (ни FormActivate, ни FormCreate, ни..., и т.д.). Ну здесь проще - можно догадаться что и как, а вот файл, где вопросы - надо знать чего в нем.
__________________
Не забывайте делать резервные копии

Последний раз редактировалось san-46, 23.03.2009 в 09:33.
Ответить с цитированием
  #5  
Старый 23.03.2009, 13:33
Improviz Improviz вне форума
Прохожий
 
Регистрация: 25.02.2009
Сообщения: 22
Репутация: 10
По умолчанию

вот файл, из которого берутся вопросы и ответы
Код:
What have you learned about America?
Сейчас Вам будут предложены вопросы об Америке. 
Вы должны выбрать правильный ответ из нескольких предложенных вариантов ответов. Удачи!
. 
Вы прекрасно знаете историюАмерики!
5
Вы многое знаете об Америке, но на некоторые вопросы ответили не правильно.
4
Вы, вероятно, только начали знакомиться с историей Америки?
3
Вы плохо подготовились по истории Америки.
2
.
Christopher Columbs landed is America in:
\Columb(1).bmp
1620
,0
1542
,0
1492	
.1
The American flag has:
\AmericanFlag.bmp
thirteen stripes
,1
thirty stripes
,0
fifty stripes
.0
How many states are there in America?
\State(3).bmp
52
,0
50
,0
25
.1
Where is the State of Liberty?
\
New York
,1
Massachusetts
,0
California
.0
What is the national symbol of America?
\
The rose
,0
The bald eagle
,1
The Shamrock
.0
What is the tallest building in the world?
\
The Empire State Building
,1
The Sears Tower
,0
The Washington Monument
.0
Who was the first president of the USA?
\Washington(7).bmp
Thomas Jefferson
,0
Abraham Lincoln
,0
George Washington
.1
What is national sport in America?
\Baseball(8).bmp
Football
,0
Soccer
,0
Baseball
.1
Washington D.C. is a:
\
State
,0
Country
,0
District
.1
The popular American food is:
\barbeky(10).bmp
Barbecue ribs
,1
Pelmeni
,0
Fish and chips
.0
In which month is Thanksgiring Day celebrated?
\ThanksgiringDay(11).bmp
December
,0
February
,0
November
.1
A famous American artist is:
\
Norman Rockwell
,1
Jack London
,0
Robert Lee Frost
.0
Who built the first car?
\Henry Ford(13).bmp
Benjamin Franklin
,0
Alexander Graham Bell
,0
Henry Ford
.1
The first university in America was:
\Harvard(14).bmp
Boston University
,0
Harvard
,1
Oxford
.0
Who arrived at Plymouth Rock in 1620?
\
The Indians
,0
The Piligrims
,1
The French
.0
What is the capital of Georgia?
\Atlanta(16).bmp
Atlanta
,1
San Francisco
,0
Los Angeles
.0
Ответить с цитированием
  #6  
Старый 23.03.2009, 14:49
san-46 san-46 вне форума
Активный
 
Регистрация: 25.04.2008
Сообщения: 383
Репутация: 33
По умолчанию

Во вложении проект. Практически ничего не правил. Читает файл нормально. Отображает то что надо.
За исключением изображений (при попытке чтения файлов картинок происходит ошибка - из-за того что файлов нет).
Вложения
Тип файла: zip tester_.zip (5.4 Кбайт, 21 просмотров)
__________________
Не забывайте делать резервные копии
Ответить с цитированием
Ответ


Delphi Sources

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра
Комбинированный вид Комбинированный вид

Ваши права в разделе
Вы не можете создавать темы
Вы не можете отвечать на сообщения
Вы не можете прикреплять файлы
Вы не можете редактировать сообщения

BB-коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход


Часовой пояс GMT +3, время: 03:41.


 

Сайт

Форум

FAQ

Соглашения

Прочее

 

Copyright © Форум "Delphi Sources" by BrokenByte Software, 2004-2025