Advanced Delphi

Moderators: None (Apply to moderate this forum)
Number of threads: 333
Number of posts: 743

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
writing to specific line in either Stringlist or document file Posted by Deffcon on 16 Apr 2010 at 5:47 AM
Hi! I've got a problem and it's about writing to specific lines in either a TStringList or a document file (end result is always a .txt or .html file) ...

So basic problem : I've got text in 2 RichEdit's and i want the text to intertwine itself when i write it to my file ...example is :
(RE=RichEdit)
RE1-text
RE2-text
RE1-text-from-2nd-line
RE2-text-from-2nd-line
and so on
So my question now is how do i do that...is it possible in TStringList?
P.s. my text may be long up to 100 lines ...

Please advise....thank you!
Report
Re: writing to specific line in either Stringlist or document file Posted by DWDuck on 20 Jan 2011 at 9:20 PM
Wellyou have two options, one you can either use code to distribute the line to a 3rd richtext box and then use the build in functions to save.

Of you write code and save directly to a text file.

Personally I prefer the first one, but thats me... As far as I know you are not limited to a number of lines by any of the two methods so you can go mad....

Code for the first option:
Place a 3rd richedit on the form and make in not visible...



Procedure Tform1.SaveMyFile(Filename: String);
Var
J,K : Integer;
Str1,Str2 : String;
Begin
If Richedit1.Lines.Count-1 = Richedit2.Lines.Count-1 then
K := Richedit1.lines.count-1 else
Begin
If Richedit1.Lines.Count-1 > Richedit2.Lines.COunt-1 then
K := Richedit1.lines.count-1 else
K := Richedit2.lines.count-1;
End;

Richedit3.Lines.Clear;
For J := 0 to K do
Begin
Str1 := '';
Str2 := '';
If J <= Richedit1.Lines.Count-1 then
Str1 := Richedit1.Lines.Strings[J];
If J <= Richedit1.LInes.Count-1 then
Str2 := Richedit2.Lines.Strings[J];
If Str1 <> '' then
Richedit3.Lines.add(Str1);
If Str2 <> '' then
Richedit3.Lines.Add(Str1);
End;
Richedit3.Lines.savetofile(Filename);
End;


Code for option 2:

Procedure TForm1.SaveMYFile
Var
F : TextFile;
Str1,Str2 : String;
J,K :Integer;
Begin
If Richedit1.Lines.Count-1 = Richedit2.Lines.Count-1 then
K := Richedit1.lines.count-1 else
Begin
If Richedit1.Lines.Count-1 > Richedit2.Lines.COunt-1 then
K := Richedit1.lines.count-1 else
K := Richedit2.lines.count-1;
End;
Assignfile(F,'C:\out.txt');
rewrite(F);
For J := 0 to K do
Begin
Str1 := '';
Str2 := '';
If J <= Richedit1.Lines.Count-1 then
Str1 := Richedit1.Lines.Strings[J];
If J <= Richedit1.LInes.Count-1 then
Str2 := Richedit2.Lines.Strings[J];
If Str1 <> '' then
Write(F,Str1);
If Str2 <> '' then
Write(F,Str2);
End;
Closefile(F);
End;

hope this helped you out!!!

Darkwing Duck aka DWduck signing off :)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.