pascal 添加 tedit 文本到记录

pascal adding tedit text to record

我在将输入到 tedit 中的文本添加到记录中时遇到问题。 这是我目前拥有的代码:

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;

如果能帮助解决这个问题,我们将不胜感激。

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

这应该让你继续:

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;