pascal添加tedit文本进行记录

时间:2016-02-04 18:08:50

标签: pascal lazarus

我遇到了将我输入的文字添加到记录中的问题。 这是我目前的代码:

procedure TForm7.AddNewQuestionClick(Sender: TObject);
 var
   w: integer;
   QuestDesc, QuestAnsr: string;

 begin
   NewQuestID.text:=(GetNextQuestionID); //Increments QuestionID part of record
   w:=Length(TQuestions);  
   SetLength(TQuestions, w+1);
   QuestDesc:= NewQuestDesc.text;
   QuestAnsr:= NewQuestAns.text;
   TQuestionArray[w+1].Question:= QuestDesc; // Error on this line (No default property available)
   TQuestionArray[w+1].Answer:= QuestAnsr;

 end;

以下是我要添加的记录:

 TQuestion = record
  public
    QuestionID: integer;
    Question: shortstring;
    Answer: shortstring;
    procedure InitQuestion(anID:integer; aQ, anA:shortstring);
 end;  

TQuestionArray = array of TQuestion;

非常感谢任何解决此问题的帮助。

1 个答案:

答案 0 :(得分:0)

你错过了一些东西。您已经宣布了一个帮助初始化新问题的程序 - 您应该使用它。

这应该让你前进:

type
  TQuestion = record
    QuestionID: integer;
    Question: ShortString;
    Answer: ShortString;
    procedure InitQuestion(anID: Integer; aQ, aAns: ShortString);
  end;

  TQuestionArray = array of TQuestion;
var
  Form3: TForm3;

var
  Questions: TQuestionArray;

procedure TForm7.AddNewQuestionClick(Sender: TObject);
begin
  SetLength(Questions, Length(Questions) + 1);
  Questions[High(Questions)].InitQuestion(GetNextQuestionID, 
                                            NewQuestDesc.Text,
                                            NewQuestAns.Text);
end;

如果你真的想单独设置字段:

procedure TForm7.AddNewQuestionClick(Sender: TObject);
var
  Idx: Integer;
begin
  SetLength(Questions, Length(Questions) + 1);
  Idx := High(Questions);
  Questions[Idx].QuestionID := GetNextQuestionID;
  Questions[Idx].Question := NewQuestDesc.Text;
  Questions[Idx].Answer := NewQuestAns.Text;
end;