Solved

How do you create objects at runtime?

Posted on 2001-08-11
4
594 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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