• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 981
  • Last Modified:

Long text on New Line in Delphi, how?

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
dcrudo
Asked:
dcrudo
  • 5
  • 3
  • 2
  • +6
3 Solutions
 
Wim ten BrinkSelf-employed developerCommented:
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
 
Wim ten BrinkSelf-employed developerCommented:
(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
 
BlackTigerXCommented:
"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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
tobjectpascalCommented:
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
 
tobjectpascalCommented:
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
 
Wim ten BrinkSelf-employed developerCommented:
@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
 
bpanaCommented:
nice the ideea with the memo :)
0
 
BlackTigerXCommented:
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
 
Slick812Commented:
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
 
Ivanov_GCommented:
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
 
BlackTigerXCommented:
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
 
ceoworksCommented:
As the Ivanov said, i think using the TStringList is really a good method for to do that.

Regards,

Oktay
0
 
Wim ten BrinkSelf-employed developerCommented:
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
 
dcrudoAuthor Commented:
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
 
Ivanov_GCommented:
if you want to use external files ... then you can just use TStringList.LoadFromFile and TStringList.SaveToFile methods ...
0
 
Wim ten BrinkSelf-employed developerCommented:
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
 
primusmagestriCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now