Solved

How do you create objects at runtime?

Posted on 2001-08-11
4
596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

628 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