Solved

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

Posted on 2006-06-17
6
407 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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