Solved

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

Posted on 2006-06-17
6
404 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now