当前位置: 动力学知识库 > 问答 > 编程问答 >

delphi - write specific line in tmemo according to line number

问题描述:

I am using the following to insert text from a text file into 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;

What i want to know is how to add a single line according to row number to TMemo when i choose the specific row number.

Example output:

During this time he has distinguished himself in the academic, sporting and cultural spheres of school life.

During this time he has distinguished himself in the academic and sporting spheres of school life.

During this time he has distinguished himself in the academic and cultural spheres of school life.

During this time he has distinguished himself in the academic aspect of school life.

During this time he has distinguished himself in both the sporting and cultural aspects of school life.

Any help appreciated.

网友答案:

I think you're asking about putting a single line from the TStringList into the TMemo when you specify which item (index, or line number) from the TStringList. If that's the case, you can use something like this:

Memo1.Lines.Add(SL[Index]);

So if the first line in your keyfile.txt is

During this time he has distinguished himself in the academic, sporting and cultural spheres of school life.

You would use

Memo1.Lines.Add(SL[0]);  // Desired line number - 1

Ok, after your comment to your question, I think I know what you're wanting to do. Here's one way to do it:

Drop a TListBox, a TButton, and a TMemo on your form. I arranged mine with the ListBox on the left, the button next to it (at the top right corner), and then the memo just to the right of the button.

In the FormCreate event, populate the TListBox with your text file and clear the existing memo content:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Clear;
  ListBox1.Items.LoadFromFile('c:\testimeng\keyfil.txt');
end;

Double-click the button to add an OnClick handler:

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;

Now run the app. Click on an item in the listbox, and then click the button. If the item is not already present in the memo, it will be added as a new last line. If it's already there, it won't be added (to prevent duplicates).

If you're wanting to add it to the end of the current last line (extending the paragraph, perhaps), then you'd do it like this:

// 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;

So, it should be clear by now that to add to the end of a specific line, you just grab the content that's already there and add to it. For instance, if the user types 3 into a 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;
网友答案:

Write a specific line with String by Mouse Clicking on 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;
分享给朋友:
您可能感兴趣的文章:
随机阅读: