Solved

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

Posted on 2006-06-17
6
409 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Print Graphic and Text to Epson TM-T88v 12 543
Create a path if not exists 7 124
Multi-layered image in FireMonkey 9 90
Broadcast a message using ICS 2 50
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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