Solved

How do you create objects at runtime?

Posted on 2001-08-11
4
589 Views
Last Modified: 2010-04-06
Hello everyone,
I am dynamically creating a Memo inside of a Notebook page at runtime. The problem that I am having is that the Memo is not appearing in the Notebook1.PageIndex that I set for it at runtime.

The Memo is using the Notebook as the parent, but it is NOT using the new page in the Notebook.

Does anyone know how to assign a new visual object to a Notebook page dynamically?

Here is what I put together?

var
  Form1: TForm1;
  varCntPage : Integer = 1;
  myPage : TMemo;

implementation

{$R *.DFM}

procedure TForm1.CreatePage();
begin
    with Notebook1 do
     begin
         Pages.Add(IntToStr(varCntPage));
         PageIndex := varCntPage;
         myPage := TMemo.Create(self);
         with myPage do
         begin
             Parent := self;
          Align := alClient;
          Lines.Add('Hello'+IntToStr(varCntPage));
         end;
     end;

     TabSet1.Tabs.Add(IntToStr(varCntPage));
     TabSet1.TabIndex := varCntPage;
     Inc(varCntPage);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    CreatePage();
end;


P.S.
I tried this?

with myPage do
         begin
             Parent := Notebook1;
          Align := alClient;
          Lines.Add('Hello'+IntToStr(varCntPage));
         end;

And the Memo would appear on the last Notebook1.Pageindex ? and not the others, after I created 10 or so new pages in the Notebook.

I appreciate the help.
0
Comment
Question by:arterberry
4 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6376036
Notebook1.PageIndex starts at index 0...
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6376128
Please use TPageControl instead of TNotebook. TNotebook is only there because of easy portability for 16bit programs. TPageControl is the way to go. With TPageControl each page is a TTabSheet object. You can then set the parent of the TMemo to any TTabSheet.

Regards, Madshi.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 300 total points
ID: 6376869
digged in my sample and found this
(using a tpagecontrol)

unit tn_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;  //Creates Pages
    PageControl1: TPageControl;
    CheckBox1: TCheckBox;  // Toggle Wordwrap
    Button2: TButton;  //Insert Lines
    procedure Button1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    Procedure ChangecolorOnMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    Procedure ChangecolorOnMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Var ActiveMemo : TMemo = nil;  //Holds the selected Memo
    OldColor : TColor;

//Predefined events

Procedure TForm1.ChangecolorOnMouseDown(Sender: TObject; Button: TMouseButton;
                                        Shift: TShiftState; X, Y: Integer);
begin
  OldColor := ActiveMemo.Font.Color;
  ActiveMemo.Font.Color := clred;
end;

Procedure TForm1.ChangecolorOnMouseUp(Sender: TObject; Button: TMouseButton;
                                        Shift: TShiftState; X, Y: Integer);
begin
  ActiveMemo.Font.Color := OldColor;
end;


procedure TForm1.Button1Click(Sender: TObject);  //Creates New Page
var
  TS : TTabsheet;
  TM : TMemo;
begin
  TS := TTabsheet.Create(PageControl1);
  TS.Parent := PageControl1;
  TS.PageControl := PageControl1;
  TM := TMemo.Create(TS);
  TM.Parent := TS;
  TM.Align := alClient;
  ActiveMemo := TM;
  ActiveMemo.OnMouseDown := ChangecolorOnMouseDown;  //Set Events
  ActiveMemo.OnMouseUp := ChangecolorOnMouseUp;
  CheckBox1.checked := TM.WordWrap;
  PageControl1.ActivePage := TS;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);  //Toggle WordWrap On Off
begin
  If ActiveMemo <> Nil then
    ActiveMemo.WordWrap := CheckBox1.Checked;
end;

procedure TForm1.PageControl1Change(Sender: TObject);  //Get the new ActiveMemo
var i : Integer;
begin
  if PageControl1.PageCount > 0 then
    for i := 0 to PageControl1.ActivePage.ControlCount - 1 do
      if (PageControl1.ActivePage.Controls[i] is TMemo) then
      begin
        ActiveMemo := TMemo(PageControl1.ActivePage.Controls[i]);
        CheckBox1.Checked := ActiveMemo.WordWrap;
      end;
end;

procedure TForm1.Button2Click(Sender: TObject);  // Add Lines to ActiveMemo
begin
  If ActiveMemo <> Nil then
    ActiveMemo.Lines.Add('This Line will be added');
end;


end.


meikl ;-)
0
 

Author Comment

by:arterberry
ID: 6377515
Hello Epsylon, Madshi, and Meikl Kretzschmar -

First, I want to thank all of you for your help. I ran into a nice little error when trying to assign the memo to the named tabsheet. I casted the name of the tabsheet to the memo?s parent, using TWinControl?see below


myPage := TMemo.Create(self);
         with myPage do
         begin
             Parent := TWinControl(varName + IntToStr(CntPage));

But then I saw what you added Meikl, and that worked out for me. As I create all of these tabsheets and memo?s dynamically, I think that I will make the variables?

var
TS : TTabsheet;
TM : TMemo;

- global, that way I can do this later, when the memo is no longer needed?

TS.PageControl.Pages[CntPage].Destroy;

This worked out well. Thank you all for your help.

-John Eric Arterberry
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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 will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

776 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