Solved

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

Posted on 2006-06-17
6
408 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
  • 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi : could not find program, '...exe' 2 212
Magic Software info 18 142
can't find the executable in Simulator 1 112
scroll down TListBox component in Delphi 1 30
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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