当您从TStringList 指定哪个项目(索引或行号)时,我认为您是在询问将TStringList 中的单行放入TMemo 中。如果是这样的话,你可以使用这样的东西:
Memo1.Lines.Add(SL[Index]);
所以如果keyfile.txt 的第一行是
During this time he has distinguished himself in the academic, sporting and cultural spheres of school life.
你会使用
Memo1.Lines.Add(SL[0]); // Desired line number - 1
好的,在您对您的问题发表评论后,我想我知道您想要做什么。这是一种方法:
在您的表单上添加TListBox、TButton 和TMemo。我将我的ListBox 放在左侧,旁边的按钮(在右上角),然后是按钮右侧的备忘录。
在FormCreate 事件中,使用您的文本文件填充TListBox 并清除现有的备忘录内容:
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear;
ListBox1.Items.LoadFromFile('c:\testimeng\keyfil.txt');
end;
双击按钮添加OnClick处理程序:
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
// If there's an item selected in the listbox...
if ListBox1.ItemIndex <> -1 then
begin
// Get the selected item
s := ListBox1.Items[ListBox1.ItemIndex];
// See if it's already in the memo. If it's not, add it at the end.
if Memo1.Lines.IndexOf(s) = -1 then
Memo1.Lines.Add(s);
end;
end;
现在运行应用程序。单击列表框中的项目,然后单击按钮。如果该项目尚未出现在备忘录中,它将作为新的最后一行添加。如果它已经存在,则不会添加(以防止重复)。
如果您想将它添加到当前最后一行的末尾(可能是扩展段落),那么您可以这样做:
// Add selected sentence to the end of the last line of the memo,
// separating it with a space from the content that's there.
Memo1.Lines[Memo1.Lines.Count - 1] := Memo1.Lines[Memo1.Lines.Count - 1] + #32 + s;
所以,现在应该很清楚,要添加到特定行的末尾,您只需抓取已经存在的内容
在那里并添加到它。例如,如果用户将3 输入到TEdit:
procedure TForm1.FormCreate(Sender: TObject);
begin
SL := TStringList.Create;
SL.LoadFromFile('c:\testimeng\keyfil.txt');
end;
procedure TForm1.ButtonAddTextClick(Sender: TObject);
var
TheLine: Integer;
begin
// SL is the TStringList from the FormCreate code above
TheLine := StrToIntDef(Edit1.Text, -1);
if (TheLine > -1) and (TheLine < Memo1.Lines.Count) then
if TheLine < SL.Count then
Memo1.Lines[TheLine] := Memo1.Lines[TheLine] + SL[TheLine];
end;