我使用下面的内容将文本文件中的文本插入到TMemo中。
procedure TForm1.Button1Click(Sender: TObject);
var
SL: TStringList;
begin
SL := TStringList.Create;
try
SL.LoadFromFile('c:\testimeng\keyfil.txt');
Memo1.Lines.Assign(SL);
finally
SL.Free;
end;
end;我想知道的是,当我选择特定的行号时,如何根据行号向TMemo添加一行。
示例输出:
在此期间,他在学校生活的学术、体育和文化领域表现突出。 在此期间,他在学术和体育领域的学校生活中脱颖而出。 在此期间,他在学术和文化领域的学校生活中脱颖而出。 在此期间,他在学校生活的学术方面取得了卓越的成就。 在此期间,他在学校生活的体育和文化方面都表现突出。
任何帮助都很感激。
发布于 2013-05-17 14:00:50
当您指定来自TStringList的哪个项(索引或行号)时,我想您是在询问如何将TStringList中的一行放入TStringList中。如果是这样的话,你可以使用这样的方法:
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;发布于 2014-09-02 18:36:20
通过鼠标单击TMemo编写带有字符串的特定行
Procedure TForm1.Button1Click(Sender: TObject);
Var SL: TStringList;
LineNumber : Integer;
Begin
LineNumber := Memo1.Perform(EM_LINEFROMCHAR, Memo1.SelStart, 0);
Memo1.SelStart := Memo1.Perform(EM_LINEINDEX, LineNumber, 0);
Memo1.SelLength := Length(Memo1.Lines[LineNumber]) ;
Memo1.SetFocus;
SL := TStringList.Create;
try
SL.LoadFromFile('c:\testimeng\keyfil.txt');
Memo1.SelText := SL.Strings[0];
finally
SL.Free;
end;
End;https://stackoverflow.com/questions/16610849
复制相似问题