Solved

How do you create objects at runtime?

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

11 Experts available now in Live!

Get 1:1 Help Now