Solved

How do you create objects at runtime?

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

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

Title # Comments Views Activity
loading dll from external path 1 212
Wininet read php file from internet issue 6 83
Print Graphic and Text to Epson TM-T88v 12 185
Error E2158 compiling with Delphi XE10 Seattle 2 88
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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

862 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

21 Experts available now in Live!

Get 1:1 Help Now