Solved

Need code quick. How to add text to Memo and delete/add things on user input?

Posted on 2006-06-17
6
410 Views
Last Modified: 2010-04-05
Hey,

Usually I would try to figure this out, but I just don't have time so I thought I'd ask here.

Basically I am trying to write a program that reads a file (which is a list of files that have a number/letters in front of them, which are a specific length e.g. 8 characters). I need the program to open a file and read it and put that info in the Memo. Then I want a Userinput box where the user enters how long the number at the beginning is, and then click a command button to delete the characters.

This is the code I was trying to use to open the file

procedure TForm1.Open1Click(Sender: TObject);
var
  CurrentLine: string;
  DataFile: textfile;
begin
  //open a data file, reading it line by line
      OpenDialog1.Filter := 'Text files (*.txt)|*.TXT';
      if OpenDialog1.Execute then
      begin
      AssignFile(DataFile, OpenDialog1.FileName);
      Filename:= OpenDialog1.FileName; //assign opendialog1.filename to the filename
      Reset(DataFile);  //reopen file
      while not eof(DataFile) do
        begin
          ReadLn(DataFile, CurrentLine);
          memo1.text := Currentline;
        end;
end;

Then I was going to use a similar code to delete the characters

delete(CurrentLine, 1, UserInput); - where userinput is an integer value that the user has entered.

I know this will be really easy for someone to do. I will give A to whoever does it.

Thanks in advance.
0
Comment
Question by:shakoor_h
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:shakoor_h
ID: 16927361
oh yeh the updated text needs to reload into the Memo and also have a possibility to save to a file.
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 16927515
     if OpenDialog1.Execute then
      begin
         memo1.lines.LodFromFile(OpenDialog1.FileName);
....
0
 
LVL 6

Accepted Solution

by:
Amir Azhdari earned 250 total points
ID: 16928349
procedure TForm1.open1Click(Sender: TObject);
var ts:TStrings;
    sd:TSaveDialog;
    i,UserInput:integer;
    s:string;
begin
OpenDialog1.Filter := 'Text files (*.txt)|*.TXT';
if OpenDialog1.Execute then
  begin
    ts:=TStringList.Create;
    ts.LoadFromFile(OpenDialog1.FileName);
    UserInput:=strtoint(InputBox('Enter the length to delete','Enter the length to delete','2'));
    for i:=0 to ts.count-1 do
     begin
      s:=ts.strings[i];
      delete(s,1,UserInput);
      ts.Strings[i]:=s;
     end;
    Memo1.Lines:=ts;
    sd:=TSaveDialog.Create(nil);
    sd.filter := 'Text files (*.txt)|*.TXT';
    if sd.Execute then
        ts.SaveToFile(sd.FileName);
    sd.Free;
    ts.Free;
  end;
end;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shakoor_h
ID: 16929717
Exactly what I wanted. Obviously I edited it so that the functions were split up and added and insert text function as well. But at the moment I can only add to the beginning of a string. Is it possible to add to the end of a string?
0
 
LVL 6

Expert Comment

by:Amir Azhdari
ID: 16930440
in this case you should replace
delete(s,1,UserInput);
with
delete(s,Userinput,Length(s)-Userinput+1);
0
 
LVL 6

Expert Comment

by:Amir Azhdari
ID: 16930444
in this case you should replace
delete(s,1,UserInput);
with
delete(s,Userinput,Length(s)-Userinput+1);
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question