0

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.

Community
  • 1
  • 1
Pierre Kruger
  • 31
  • 2
  • 5
  • I am working on a testimonial writer program that has 123 predefined sentences. So i need to choose different sentences to make up the final testimonial. – Pierre Kruger May 17 '13 at 14:53
  • Wait, I've answered an almost identical question before... http://stackoverflow.com/questions/16003220/multiple-line-addition-in-tmemo/16003244#16003244 Not necessarily the same question but the exact same answer. – Jerry Dodge May 17 '13 at 15:26

2 Answers2

2

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Ken what i am thinking of is to have a button with the line number to choose or to have a place where you type in the row number for example - 1, 2, 23 or 123 – Pierre Kruger May 17 '13 at 14:57
  • @Pierre: OK. :-) I'm still not sure what you're asking us to help you with, though. As you can see, David and I both got different meanings from your question. Can you [edit] to add more details about what specifically you're asking us to help you do? Wait. I think I get (from your comment to your own question) what you're asking. Give me a couple of minutes... – Ken White May 17 '13 at 15:19
  • Ken i have done the code as you sugested. It works but i dont want to select the group of sentences in the list box. What i have is a questionare where they tick (a b) or (c d) which will result in a combination of: ac=1 ad=2 a=3 bc=4 bd=5 b=6 c=7 d=8 and also different sections 1 - 11. So the combination will be - 3 Reliabiltity=a, Neatness=d combined becomes 3ad = 32. So when you enter the combination then the sentence must be displayed. Thanks so far for your help – Pierre Kruger May 17 '13 at 17:02
  • That's what your other question asked, and it was closed because no one can understand what your requirements are from reading it. That's also *not* the question you asked here. I've answered the one asked here. :-) If you want to ask the other one, you need to edit it and improve it, and then ask to have it reopened. You can't bypass the guidelines here by trying to get the same question through in a comment here. (I have no idea what you mean by `ac=1 ad=2 a=3`, either here or in that question, and I'd suspect no one else does either, which is why it was closed. It needs a table or image.) – Ken White May 17 '13 at 17:07
  • (continued) or some other way to illustrate what you're looking to do, because the text description isn't clear. – Ken White May 17 '13 at 17:09
  • I was just about to say some pictures / screenshots showing us step-by-step what you want to do would probably help us better :) – Jerry Dodge May 17 '13 at 18:05
  • @KenWhite: I have edited my code regarding previous question and need the question to be re-opened. Any help appreciated. – Pierre Kruger May 20 '13 at 11:22
  • I've added more information that should help. If you have more changes, please post a **new question** and ask it there, instead of modifying this one again. As I've said before, once you get an answer, **do not** change the meaning of the question. Post a new question instead. – Ken White May 20 '13 at 12:39
1

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;
NOTSermsak
  • 356
  • 1
  • 8
  • 8