Solved

Long text on New Line in Delphi, how?

Posted on 2004-08-27
17
774 Views
Last Modified: 2010-04-04
Dear Delphi Experts,

Is there to assign a variable that will be on multiple lines
without having to always close the line with a ' +   ?

i.e.

page1:= '<form method="POST" action="proclogin.asp">' +
'<table border="0" width="671" height="401">' +
'<tr><td><div align="center">' +
'<table border="0" width="39%" cellspacing="0" cellpadding="0" height="127">' +
'<tr><td width="65" style="border-bottom-style: solid; border-bottom-width: 3px" bgcolor="#C0C0C0" height="22">'+
'<p align="left">' +
[...]

like this, if I have (per page) 200 lines... it is going to be a lot of work!!

I'm trying to port web pages into web applications in delphi...

thx a lot!

Dave
0
Comment
Question by:dcrudo
  • 5
  • 3
  • 2
  • +6
17 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11913652
No...
Besides, string constants in Delphi are limited to 255 characters in the first place (Inheritance from the old Turbo Pascal) thus you'll always have to cut your string constants into small strings.

However, as an alternative you could store these long texts into e.g. your resource file or just create a simple stringlist component that you can drop on your form with a single string property that you can modify in runtime...

But using the resource would be easier, though. Let me explain... Create the following files:

--- TestApp.rc ------------------------------
SOURCE SOURCEFILE "TestApp.dpr"
--- TestApp.rc end ------------------------------

--- TestApp.dpr ------------------------------
program TestApp;
{$APPTYPE Console}
{$R 'TestApp.res' 'TestApp.rc'}
uses Classes;
var
  Res: TResourceStream;
  Value: string;
begin
  Res := TResourceStream.Create( HInstance, 'SOURCE', 'SOURCEFILE' );
  SetLength( Value, Res.Size );
  Res.Read( PChar(Value)^, Res.Size );
  Res.Free;
  WriteLn( Value );
  Write( 'Press <Enter> to close.' );
  ReadLn;
end.
--- TestApp.dpr end ------------------------------

What this will do is include the actual sourcecode of your project (Big piece of text) as a resource in your application. The resource is called SOURCE and is of type SOURCEFILE, which are just names that you like. Because of the {$R ...} line, this file will be compiled whenever you compile this tiny project. The code of the project will just read the string from the resource and write it to the console screen, then wait for you to press <Enter>. The big advantage of this is that you can keep those HTML pages as separate files within your project, editing them whenever you need to edit them, then do a rebuild to update the resource file again.
It is a bit more complicated than just assigning them in your code, but also a bit more useful.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11913694
(And basically it means that if you're trying to put 200 pages like this in your Delphi application, you will just get 200 lines in your *.RC file like this:

PAGE1 HTMLPAGE "Page1.html"
PAGE2 HTMLPAGE "Page2.html"
PAGE3 HTMLPAGE "Page3.html"
PAGE4 HTMLPAGE "Page4.html"
...

Do you get the image? :)

Oh, before I forget, to add the *.RC file to your project, select the menu: Project/Add to Project option. In the file dialog, change files of type into *.RC and then select your *.RC file. You can then edit the file even from Delphi...
0
 
LVL 13

Assisted Solution

by:BlackTigerX
BlackTigerX earned 100 total points
ID: 11914221
"Besides, string constants in Delphi are limited to 255 characters in the first place (Inheritance from the old Turbo Pascal)"

huh... no... that's not true

from the Delphi help I quote:
"AnsiString      ~2^31 characters      4 bytes to 2GB      "

"The reserved word string functions like a generic type identifier. For example,

var S: string;

creates a variable S that holds a string. In the default {$H+} state, the compiler interprets string (when it appears without a bracketed number after it) as AnsiString. Use the {$H-} directive to turn string into ShortString."

however the idea of storing those strings in a resource file is good... or store it in any other file, then load it at runtime, that way is not hardcoded in your program and you could change it any time
0
 
LVL 4

Expert Comment

by:tobjectpascal
ID: 11914472
Delphi's IDE / Compiler is limited to 255 char strings, sure enough you can (AT RUNTIME KEY WORD *****RUNTIME*****)

can make the string any size up to whatever limit you just said, i personally would save all the data you wish to save in a text file, load in the text file and post that.
0
 
LVL 4

Assisted Solution

by:tobjectpascal
tobjectpascal earned 150 total points
ID: 11914612
Function FileToStr(FileN: String): String;
Var
  T: TextFile;
  Tmp: String;
Begin
 Result:='';
 IF FileExists(FileN) Then
  Begin
   System.Assign(T,FileN);
   System.Reset(T);
    While Not (Eof(T)) Do
     Begin
      Readln(T,Tmp);
      Result:=Result+Tmp;
     End;
  End
   else
  ShowMessage('File Not Found!');
End;

procedure TForm1.Button1Click(Sender: TObject);
Var
 S: String;
begin
 S:=FileToStr('c:\winnt\setuplog.txt');
 Memo1.Text:=S; //obviously you can do what you like with it.

//it may take a while with that example, you might want to try a smaller file, or at least, mine was quite large.
end;
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11914727
@BlackTigerX,
as tobjectpascal already said, string CONSTANTS are limited to 255 characters. You get a compiler error is you use string constants longer than 255 characters. However, you could break up a long string constant into multiple shorter string constants by using the + to concatenate several smaller strings into one bigger one. And since many developers do break up long string constants in a bunch of small ones, no one ever really notices this limit...

@tobjectpascal,
I still prefer the method of including the strings as files into a resource since that way, they will become part of the executable, just like all string constants. If you create a project with several loose text files, there is a chance that users will examine and perhaps even modify those texts. With files stored inside the executable, this is a bit harder to do.

@dcrudo,
Well, one other solution would be to put a TMemo on your form, make it invisible and use it's Lines property to store your long texts. That way, all you have to do is call TMemo.Lines.Text to get your string. But it would be nicer if you just create a simple component, inherited from TComponent, with only a property Lines of type TStringList. Such a component would hardly require any code anyway. You then drop it on your form and you can use the text stored in it any way you like...
Still, I prefer the Resource solution.
0
 
LVL 6

Expert Comment

by:bpana
ID: 11914760
nice the ideea with the memo :)
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 11916756
at first I didn't read the "constant" of your comment... and since didn't seem like he wasn't using a constant... anyway...

so why not just a TStringList instead of the TMemo?

or why not use a TPageProducer (Internet tab)... that's what that component is for...
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Expert Comment

by:Slick812
ID: 11919283
I was doing code that required tons of multi-line text (copy and paste) into string variables and constansts, and had the same probles as you, adding all those  '  and   + to each line. . . so I made a separate program with a tmemo in it, and I would copy the text in this tmemo, and then press one button, and the program would add the delphi string   '    to the begining and end of each line, , and a  +  after each  '  at the end of the line, I would then press a "Copy" button which would copy all of the tmemo text and then I would just copy into my code segment ,, , in some I had the option to  add the #13#10 for line breaks,
saved alot of time
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11921127
var
  page1 : TStringList;
begin
  page1 := TStringList.Create;
  page1.Text := '<form method="POST" action="proclogin.asp">' +
                        '<table border="0" width="671" height="401">' +
                        '<tr><td><div align="center">' +
                        '<table border="0" width="39%" cellspacing="0" cellpadding="0" height="127">' +
                        '<tr><td width="65" style="border-bottom-style: solid; border-bottom-width: 3px" bgcolor="#C0C0C0" '+
                        'height="22"><p align="left">'

and finally after you are finished...
   page1.Free;

0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 11921565
mm... what was the advantage of that?... he originally asked this:
"Is there to assign a variable that will be on multiple lines
without having to always close the line with a ' +   ?"
0
 
LVL 4

Expert Comment

by:ceoworks
ID: 11922889
As the Ivanov said, i think using the TStringList is really a good method for to do that.

Regards,

Oktay
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11923141
The TStringList is a good idea but the TStringList is not a component that you can fill in during design-time. dcrudo clearly wants a design-time solution so either a TMemo would be an option or just a new component with a TStringList property. You could even use a TQuery component for this and "abuse" the SQL property to store the string constant in. But hey, I still stick to the idea that using a resource file is the best solution here. Especially since this way you can include whole textfiles as string constants. Modifying those is as easy as using notepad. ;)

Did you guys also read this: "I'm trying to port web pages into web applications in delphi..."
dcrudo is trying to include several existing webpages in his Delphi application. Those strings already exist as existing HTML pages...Do I really need to say more?
0
 
LVL 1

Author Comment

by:dcrudo
ID: 11923686
Dear All!!

Thank you very much for your wonderful feedback... I Like the idea of the external files because I can modify them easily... in the meantime I will pump
up the points to 500 because I think I will have to split them across more than one expert ;)

Let me try your suggestions and I'll be back!

Thx again!

Dave.
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11923861
if you want to use external files ... then you can just use TStringList.LoadFromFile and TStringList.SaveToFile methods ...
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 250 total points
ID: 11929529
Ivanov, the problem with LoadFromFile would be that those external files would have to stay external. Makes it a bit more difficult to move the applications to other systems since you'd have to move those files too. The advantage however would be that you could modify the pages even after the application has been written. I think it basically depends on how dcrudo wants to handle these pages. They could be part of the executable, making one big executable and preventing people from changing the pages without modifying the application. Or they stay in an external file in which case I still would not use string lists if all I want to do is to get them in a single string. How? Simple. :-)

var
  Res: TFileStream;
  Value: string;
begin
  Res := TFileStream.Create( 'Index.html', fmOpenRead );
  SetLength( Value, Res.Size );
  Res.Read( PChar(Value)^, Res.Size );
  Res.Free;

It could even be nicer by using this:

var
  Res: TStream;
  Value: string;
begin
  if FileExists('Index.html') then begin
    Res := TFileStream.Create( 'Index.html', fmOpenRead );
  end else begin
    Res := TResourceStream.Create( HInstance, 'SOURCE', 'SOURCEFILE' );
  end;
  SetLength( Value, Res.Size );
  Res.Read( PChar(Value)^, Res.Size );
  Res.Free;

This code would load the string from a file if it exists or otherwise it would retrieve it from the resource. This would be an interesting technique to store error pages just in case a file isn't found. And yes, I know... Stringlist have a LoadFromFile and LoadFromStream method so you could use those too if you prefer stringlists above simple strings. A stringlist just allows you to access individual lines but if all you do is pass along the string from A to B, a string is just as easy. Changing from string to stringlist and back again isn't hard either...
0
 
LVL 1

Expert Comment

by:primusmagestri
ID: 11967237
I'm not sure exactly what were the suggestions. Take a good advice: use the HTML you have and change it a little bit to support parameters. When you need to dispatch a certain page, load the html from the file (html file, I say), put the parameter values and send it to be dispatched. It works.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

13 Experts available now in Live!

Get 1:1 Help Now