Solved

richedit and tabsheets continued

Posted on 1997-06-12
4
334 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

15 Experts available now in Live!

Get 1:1 Help Now