Solved

richedit and tabsheets continued

Posted on 1997-06-12
4
329 Views
Last Modified: 2013-11-22
When using this code at startup:

Procedure TForm1.FormCreate(Sender: TObject);
var
parameters: string;
x: integer;
begin
 TabSheet1.Caption := 'untitled';
 RichEdit1.SetFocus;
 RichEdit1.Modified := False;
 parameters := '';
for x := 1 to paramCount do
begin
parameters := parameters + paramStr(x);
if x < paramCount then parameters := parameters +' ';{dont add a space to the last parameter}
end;
{Process Parameters}
if Parameters <> '' then OpenCode(Parameters);

end;

and the opencode:

procedure Tform1.OpenCode(const CreateFile: AnsiString);
var
  ExtType1 : string;
begin

     RichEdit1.Lines.LoadFromFile(CreateFile);
     TabSheet1.Caption :=(CreateFile);
     begin
      with RichEdit1 do
       begin
       ExtType1 := ExtractFileExt(CreateFile);
       if UpperCase(ExtType1) = '.TXT' then
       RichEdit1.PlainText := true
       else RichEdit1.PlainText := false;
       Lines.LoadFromFile(CreateFile);
       SelStart := 0;
       end;
     RichEdit1.Modified := False;
     end;
end;

it should;
1) set the tabsheet caption to 'untitled'
2) when clicked in Windows explorer it should launch the editor and place the document in the richedit field, and set the caption to the filename.
3) SetFocus to RichEdit so I can immidiately start typing, but it doesn't the tabsheet is always selected.
It doesn't do any of this, what is wrong here
even with simple code in the FormCreate like:
Color := clBlack; that doesn't work either the form remains Silver in color.
This code works on another editor I made, without a tabsheet component.
It seems if the FormCreate does not work? is this possible?

is it perhaps better to remove tabsheet1 and richedit1 and use this instead:

Procedure TForm1.FormCreate(Sender: TObject);
var
TabSheet: TTabSheet;
begin
TabSheet := TTabSheet.Create(Self);
TabSheet.Caption := 'Untitled';
TabSheet.PageControl := PageControl1;
with TRichEdit.Create(Self) do
begin
Parent := TabSheet;
end;
PageControl1.ActivePage := TabSheet;
Save1.Enabled := True;
end;
end;

But that doesn't work either, the form does not create the tabsheet, or the richedit why?
and then ofcourse how should I use the Opencode if the last is better, do I destroy the TabSheet first and then recreate it or what? please demonstrate.
also in the Open command it is used as this:

procedure TForm1.OpenClick(Sender: TObject);
var
 TabSheet: TTabSheet;
begin
if OpenDialog1.Execute then
begin
TabSheet := TTabSheet.Create(Self);
TabSheet.Caption := LowerCase(OpenDialog1.Filename);
TabSheet.PageControl := PageControl1;
FileHist1.AddFile(OpenDialog1.FileName);
with TRichEdit.Create(Self) do
begin
Parent := TabSheet;
Lines.LoadFromFile(OpenDialog1.Filename);
end;
PageControl1.ActivePage := TabSheet;
Save1.Enabled := True;
end;
end;

but the problem is that it makes a new tabsheet with richedit, and loads the document in here.
but if RichEdit1 is still empty it should fill that one first and then start creating an new tabsheet.
how do I do this? And if it is better to use a create tabsheet at startup (above) how should I use it then? destroy the new created tab first and then make a new one and fill it with the document.


Also I have at the New command this code;

procedure TForm1.NewClick(Sender: TObject);
var
 TabSheet: TTabSheet;
 RichEdit: TRichEdit;
begin
TabSheet := TTabSheet.Create(Self);
TabSheet.Caption := 'Untitled';
TabSheet.PageControl := PageControl1;
with TRichEdit.Create(Self) do
begin
Parent := TabSheet;
end;
PageControl1.ActivePage := TabSheet;
Save1.Enabled := True;
end;

this works fine, but the richedit component is not the right size;
if I use this

....
begin
parent := TabSheet;
RichEdit.Align := alClient;
RichEdit.Height := 335;
RichEdit.Width := 714;
I get an EAccessViolation at adress ...., or a Memory access violation. even if I just use one parameter.
how do I get the richedit component to fill the tabsheet, because the tabsheet itself is set to alClient in relation to Form1.
This is really important.

also The code for closing 1 tabsheet is this:

procedure TForm1.closericheditClick(Sender: TObject);
begin
CheckFileSave;
PageControl1.ActivePage.Free;
PageControl1.SelectNextPage(False);
end;

What code should I use to close All Tabsheets, and then immidiately create a new tabsheet.

and for saving a File I have this;

procedure TMainForm.Save1Click(Sender: TObject);
begin
with PageControl1.ActivePage
do (Controls[0] as TRichEdit).Lines.SaveToFile(Caption)
end;  

Again what should I use to Save all files direct if they already have a filename, and if some or all are 'untitled' to save them 'AS' for each different document.
(i.e. ask for each unsaved document to enter a name and then save, and then go to the next one).

0
Comment
Question by:hellfire052497
4 Comments
 
LVL 4

Expert Comment

by:erajoj
ID: 1336987
Lot's of text to read for 50 pts. ;^)
Send the FULL project to me and I'll have a look.
john.johansson@era.ericsson.se

/// John

0
 

Expert Comment

by:kangadru
ID: 1336988
The following code Creates and populates a RichEdit on a PageControl and TTabSheet.  The SetFocus is Try..except blocked for use with the form create event, where it WILL except out after setting the focus.  Please ignore the parseFile call, that is where I parse for syntax highlighting.Andy
function TfrmMain.OpenFile(FileName : string) : boolean;
var
  newTS : TTabSheet;
  newRE : TRichEdit;
begin
  // Create a new TabSheet
  newTS := TTabSheet.Create(nil);
  newTS.Caption := FileName;;

  // Create the RichEdit
  newRE := TrichEdit.Create(nil);
  newRE.Align := alClient;
  newRE.PopupMenu := popMain;
  newRE.Font.Name := 'Lucida Console';
  newRE.Font.Size := 9;
  newRE.ScrollBars := ssBoth;

  newRE.Parent := newTS;

  // Insert the Tab Control
  newTS.PageControl := pgMain;

  if FileName <> '.untitled' then
    newRE.Lines.LoadFromFile(FileName);

  try
    pgMAin.ActivePage  := newTS;
    newRE.SetFocus;
  except
    // Focus Failed
  end;

  parseFile(newRE);
 
  frmMain.Caption     := pgMain.ActivePage.Caption;
  Application.Title   := pgMain.ActivePage.Caption;

  result := true;
end;

0
 

Author Comment

by:hellfire052497
ID: 1336989
this takes care of the opening and creating of files.

but now how to save them?, saving them 1by1 is working. But how to save them all at once, if they have been edited or if some files are still 'untitled'.

I guess, I need to make a routine to check if anything has changed to the text and filename.

Any tips on this. Because it will need to monitor multiple filenames and texts.
0
 
LVL 2

Accepted Solution

by:
icampbe1 earned 50 total points
ID: 1336990
I am answering your question up to:
 
  It should  1)... 2).... 3)....    I got tired of reading after this.

OK, when you are in the constructor of the main form, like FormCreate, the application is not processing window messages.  This only occurs when the application.run step is executed (see the .DPR file).

I would recommend that you do your setup of the tabsheet and edit control/LoadFromFile etc etc etc after the program has started 'really' running.

Create a startup procedure that you invoke after the Constructors have been run, and 'POST' a message to yourself to run it.

CONST
  WM_StartUpMsg = WM_User + 101;

{The code below is the class definition for your form}
TYPE
  TForm1 = CLass(Tform)
  .....
  PRIVATE
  .....
     PROCEDURE WMStartUpMsg( VAR Message: TMessage); Message WM_StartUpMsg;
 ......
  PUBLIC
 ......
  END;

PROCEDURE TForm1.FormCreate(Sender:TObject);
BEGIN
 ......
 ......
  {After 'simple' setup activities like object creates etc.  call the workhorse setup}
   PostMessage( Self.Handle, WM_StartUpMsg, 0, 0 );
END;

PROCEDURE TForm1.WMStartUpMsg( VAR Message: TMessage );
BEGIN
 {Now, do all your real setup here (LoadFromFile etc etc!! }
END;


This should solve your problem.  You will also notice that setting colors/fonts etc etc will work for you (where befire it didn't).

Cheers,
Ian C.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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