I coded an example for dynamic control creation. You may find the source
codes below:
1) This source codes are for Visual C++ .NET v.2003
2) Open a project by using "Windows Forms (.NET)."
3) Name the project as : "Panel_Denemele ri"
4) Paste the below source codes to "Form1.h" code view.
5) Compile, Debug and Build the program :)
Source Codes:
Kodlar :
// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
/*************** *************** *************** *************
Coded by Alper AKCAYOZ
September 27th, 2004
bi*********@hot mail.com
*************** *************** *************** *************/
#pragma once
#include <windows.h>
#ifdef MessageBox
#undef MessageBox
#endif
namespace Panel_Denemeler i
{
using namespace System;
using namespace System::Threadi ng;
using namespace System::Compone ntModel;
using namespace System::Collect ions;
using namespace System::Windows ::Forms;
using namespace System::Data;
using namespace System::Drawing ;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change
the
/// 'Resource File Name' property for the managed resource
compiler tool
/// associated with all .resx files this class depends on.
Otherwise,
/// the designers will not be able to interact properly with
localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows ::Forms::Form
{
public:
Form1(void)
{
InitializeCompo nent();
}
////////////////////////////////////////////////////////////////////////////////////////////
//Panel
Panel __gc *panel1;
Thread* myThread;
////////////////////////////////////////////////////////////////////////////////////////////
static Int32 i32Panel_Number = 0;
static Int32 i32Label_Number = 0;
static Int32 i32SpaceBetween Controls = 5;
static Int32 i32LabelHeight = 20;
static Int32 i32LabelWidth = 130;
static Int32 i32Current_Pos_ X = 8;
static Int32 i32Current_Pos_ Y = 8;
private: System::Windows ::Forms::Label * lblControlCount ;
private: System::Windows ::Forms::Timer * timer1;
private: System::Windows ::Forms::Button * btnPreviousPage ;
private: System::Windows ::Forms::Button * btnNextPage;
static Int32 i32Current_Colu mn_Pos_X = 8 ;
////////////////////////////////////////////////////////////////////////////////////////////
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispos e(disposing);
}
private: System::Windows ::Forms::Button * bntCreatePanel;
private: System::Windows ::Forms::TextBo x * txtSiraSayisi;
private: System::Windows ::Forms::Button * btnKadarSiraYar at;
private: System::Windows ::Forms::Button * btnClearPanel;
private: System::Windows ::Forms::Label * lblSure;
private: System::Compone ntModel::IConta iner * components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeCompo nent(void)
{
this->components = new System::Compone ntModel::Contai ner();
this->bntCreatePan el = new System::Windows ::Forms::Button ();
this->txtSiraSayis i = new System::Windows ::Forms::TextBo x();
this->btnKadarSiraYa rat = new System::Windows ::Forms::Button ();
this->btnClearPane l = new System::Windows ::Forms::Button ();
this->lblSure = new System::Windows ::Forms::Label( );
this->lblControlCoun t = new System::Windows ::Forms::Label( );
this->timer1 = new System::Windows ::Forms::Timer( this->components);
this->btnPreviousPag e = new System::Windows ::Forms::Button ();
this->btnNextPage = new System::Windows ::Forms::Button ();
this->SuspendLayout( );
//
// bntCreatePanel
//
this->bntCreatePan el->BackColor =
System::Drawing ::Color::FromAr gb((System::Byt e)255, (System::Byte)2 55,
(System::Byte)1 92);
this->bntCreatePan el->Font = new System::Drawing ::Font(S"Micros oft Sans
Serif", 9, System::Drawing ::FontStyle::Bo ld,
System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 62);
this->bntCreatePan el->ForeColor =
System::Drawing ::Color::FromAr gb((System::Byt e)0, (System::Byte)0 ,
(System::Byte)1 92);
this->bntCreatePan el->Location = System::Drawing ::Point(8, 8);
this->bntCreatePan el->Name = S"bntCreatePane l";
this->bntCreatePan el->Size = System::Drawing ::Size(104, 32);
this->bntCreatePan el->TabIndex = 0;
this->bntCreatePan el->Text = S"Create Panel";
this->bntCreatePan el->Click += new System::EventHa ndler(this,
bntCreatePanel_ Click);
//
// txtSiraSayisi
//
this->txtSiraSayis i->BorderStyle =
System::Windows ::Forms::Border Style::None;
this->txtSiraSayis i->CharacterCasin g =
System::Windows ::Forms::Charac terCasing::Uppe r;
this->txtSiraSayis i->Enabled = false;
this->txtSiraSayis i->Font = new System::Drawing ::Font(S"Micros oft Sans
Serif", 20.25F, System::Drawing ::FontStyle::Re gular,
System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 62);
this->txtSiraSayis i->ImeMode = System::Windows ::Forms::ImeMod e::NoControl;
this->txtSiraSayis i->Location = System::Drawing ::Point(128, 8);
this->txtSiraSayis i->MaxLength = 3;
this->txtSiraSayis i->Name = S"txtSiraSayisi ";
this->txtSiraSayis i->Size = System::Drawing ::Size(64, 31);
this->txtSiraSayis i->TabIndex = 1;
this->txtSiraSayis i->Text = S"";
this->txtSiraSayis i->TextAlign =
System::Windows ::Forms::Horizo ntalAlignment:: Center;
//
// btnKadarSiraYar at
//
this->btnKadarSiraYa rat->BackColor =
System::Drawing ::Color::FromAr gb((System::Byt e)255, (System::Byte)2 55,
(System::Byte)1 92);
this->btnKadarSiraYa rat->Enabled = false;
this->btnKadarSiraYa rat->Font = new System::Drawing ::Font(S"Micros oft
Sans Serif", 9, System::Drawing ::FontStyle::Bo ld,
System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 62);
this->btnKadarSiraYa rat->ForeColor =
System::Drawing ::Color::FromAr gb((System::Byt e)0, (System::Byte)0 ,
(System::Byte)1 92);
this->btnKadarSiraYa rat->Location = System::Drawing ::Point(200, 8);
this->btnKadarSiraYa rat->Name = S"btnKadarSiraY arat";
this->btnKadarSiraYa rat->Size = System::Drawing ::Size(72, 32);
this->btnKadarSiraYa rat->TabIndex = 2;
this->btnKadarSiraYa rat->Text = S"pcs. Row";
this->btnKadarSiraYa rat->Click += new System::EventHa ndler(this,
bntKadarSiraYar at_Click);
//
// btnClearPanel
//
this->btnClearPane l->BackColor =
System::Drawing ::Color::FromAr gb((System::Byt e)255, (System::Byte)2 55,
(System::Byte)1 92);
this->btnClearPane l->Enabled = false;
this->btnClearPane l->Font = new System::Drawing ::Font(S"Micros oft Sans
Serif", 9, System::Drawing ::FontStyle::Bo ld,
System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 62);
this->btnClearPane l->ForeColor =
System::Drawing ::Color::FromAr gb((System::Byt e)0, (System::Byte)0 ,
(System::Byte)1 92);
this->btnClearPane l->Location = System::Drawing ::Point(280, 8);
this->btnClearPane l->Name = S"btnClearPanel ";
this->btnClearPane l->Size = System::Drawing ::Size(80, 32);
this->btnClearPane l->TabIndex = 3;
this->btnClearPane l->Text = S"Clear Panel";
this->btnClearPane l->Click += new System::EventHa ndler(this,
btnClearPanel_C lick);
//
// lblSure
//
this->lblSure->BackColor = System::Drawing ::Color::Silver ;
this->lblSure->Font = new System::Drawing ::Font(S"Micros oft Sans Serif",
12, System::Drawing ::FontStyle::Bo ld, System::Drawing ::GraphicsUnit: :Point,
(System::Byte)1 62);
this->lblSure->ForeColor = System::Drawing ::Color::Red;
this->lblSure->Location = System::Drawing ::Point(384, 8);
this->lblSure->Name = S"lblSure";
this->lblSure->Size = System::Drawing ::Size(624, 16);
this->lblSure->TabIndex = 4;
this->lblSure->TextAlign = System::Drawing ::ContentAlignm ent::MiddleCent er;
//
// lblControlCount
//
this->lblControlCoun t->BackColor = System::Drawing ::Color::Silver ;
this->lblControlCoun t->Font = new System::Drawing ::Font(S"Micros oft Sans
Serif", 12, System::Drawing ::FontStyle::Bo ld,
System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 62);
this->lblControlCoun t->ForeColor = System::Drawing ::Color::Red;
this->lblControlCoun t->Location = System::Drawing ::Point(384, 32);
this->lblControlCoun t->Name = S"lblControlCou nt";
this->lblControlCoun t->Size = System::Drawing ::Size(624, 16);
this->lblControlCoun t->TabIndex = 5;
this->lblControlCoun t->TextAlign =
System::Drawing ::ContentAlignm ent::MiddleCent er;
//
// timer1
//
this->timer1->Enabled = true;
this->timer1->Interval = 800;
this->timer1->Tick += new System::EventHa ndler(this, timer1_Tick);
//
// btnPreviousPage
//
this->btnPreviousPag e->BackColor =
System::Drawing ::Color::FromAr gb((System::Byt e)255, (System::Byte)2 55,
(System::Byte)1 92);
this->btnPreviousPag e->Enabled = false;
this->btnPreviousPag e->Font = new System::Drawing ::Font(S"Micros oft Sans
Serif", 9, System::Drawing ::FontStyle::Bo ld,
System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 62);
this->btnPreviousPag e->ForeColor =
System::Drawing ::Color::FromAr gb((System::Byt e)0, (System::Byte)0 ,
(System::Byte)1 92);
this->btnPreviousPag e->Location = System::Drawing ::Point(1024, 8);
this->btnPreviousPag e->Name = S"btnPreviousPa ge";
this->btnPreviousPag e->Size = System::Drawing ::Size(96, 40);
this->btnPreviousPag e->TabIndex = 6;
this->btnPreviousPag e->Text = S"<<< Previous";
//
// btnNextPage
//
this->btnNextPage->BackColor =
System::Drawing ::Color::FromAr gb((System::Byt e)255, (System::Byte)2 55,
(System::Byte)1 92);
this->btnNextPage->Enabled = false;
this->btnNextPage->Font = new System::Drawing ::Font(S"Micros oft Sans
Serif", 9, System::Drawing ::FontStyle::Bo ld,
System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 62);
this->btnNextPage->ForeColor =
System::Drawing ::Color::FromAr gb((System::Byt e)0, (System::Byte)0 ,
(System::Byte)1 92);
this->btnNextPage->Location = System::Drawing ::Point(1128, 8);
this->btnNextPage->Name = S"btnNextPag e";
this->btnNextPage->Size = System::Drawing ::Size(80, 40);
this->btnNextPage->TabIndex = 7;
this->btnNextPage->Text = S"Next >>>";
//
// Form1
//
this->AutoScaleBaseS ize = System::Drawing ::Size(5, 13);
this->BackColor = System::Drawing ::Color::Black;
this->ClientSize = System::Drawing ::Size(1262, 725);
this->Controls->Add(this->btnNextPage) ;
this->Controls->Add(this->btnPreviousPag e);
this->Controls->Add(this->lblControlCoun t);
this->Controls->Add(this->lblSure);
this->Controls->Add(this->btnClearPanel) ;
this->Controls->Add(this->btnKadarSiraYa rat);
this->Controls->Add(this->txtSiraSayisi) ;
this->Controls->Add(this->bntCreatePanel );
this->FormBorderStyl e = System::Windows ::Forms::FormBo rderStyle::Fixe d3D;
this->MaximizeBox = false;
this->MinimizeBox = false;
this->Name = S"Form1";
this->StartPositio n =
System::Windows ::Forms::FormSt artPosition::Ce nterScreen;
this->Text = S"Form1 - Once Alt Alta Sonra Yan Yana Diz";
this->WindowState = System::Windows ::Forms::FormWi ndowState::Maxi mized;
this->Load += new System::EventHa ndler(this, Form1_Load);
this->DoubleClick += new System::EventHa ndler(this, Form1_DoubleCli ck);
this->ResumeLayout(f alse);
}
//THIS IS THE FUNCTION WHICH CREATES PANEL
private: System::Void bntCreatePanel_ Click(System::O bject * sender,
System::EventAr gs * e)
{
//Panel
panel1 = new Panel();
//+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++++
// Initialize the Panel control.
panel1->Location = System::Drawing ::Point(8,50);
panel1->Size = System::Drawing ::Size( (this->get_Width() - 30),
(this->get_Height() - 85));
// Set the Borderstyle for the Panel to three-dimensional.
panel1->BorderStyle = System::Windows ::Forms::Border Style::Fixed3D;
//Backcolor
panel1->set_BackColor( System::Drawing ::Color::Aqua);
//Name
panel1->Name = String::Concat( S"Panel_Name _",
(++i32Panel_Num ber).ToString() );
//TAG
panel1->Tag = i32Panel_Number .ToString();
//----------------------------------------------------------------------------------------
// Add the Panel control to the form.
this->Controls->Add(panel1);
txtSiraSayisi->Enabled = true;
btnKadarSiraYar at->Enabled = true;
bntCreatePanel->Enabled = false;
}
private: System::Void CreateRows_Func tion()
{
try
{
//Labels
Label __gc *lblA = new Label();
Label __gc *lblB = new Label();
Label __gc *lblC = new Label();
//+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++++
// Initialize the Label and TextBox controls.
lblA->Location = System::Drawing ::Point(i32Curr ent_Pos_X,
i32Current_Pos_ Y);
lblA->Text = String::Concat( S"Label_", (++i32Label_Num ber).ToString() );
lblA->Name = String::Concat( S"lblA_Name_ ",
(i32Label_Numbe r).ToString());
lblA->Size = System::Drawing ::Size(i32Label Width, i32LabelHeight) ;
lblA->TextAlign = System::Drawing ::ContentAlignm ent::MiddleLeft ;
lblA->ForeColor = System::Drawing ::Color::Yellow ;
lblA->BackColor = System::Drawing ::Color::Black;
lblA->Font = new System::Drawing ::Font(S"Micros oft Sans Serif", 9.75F,
System::Drawing ::FontStyle::Bo ld, System::Drawing ::GraphicsUnit: :Point,
(System::Byte)1 62);
//----------------------------------------------------------------------------------"------
//Update the current i32Current_Pos_ X and i32Current_Pos_ Y
i32Current_Pos_ X += (i32LabelWidth + i32SpaceBetween Controls) ;
//i32Current_Pos_ Y += ;
//+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++++
// Initialize the Label and TextBox controls.
lblB->Location = System::Drawing ::Point(i32Curr ent_Pos_X,
i32Current_Pos_ Y);
lblB->Text = String::Concat( S"lblB_Name_ ",
(i32Label_Numbe r).ToString());
lblB->Name = String::Concat( S"lblB_Name_ ",
(i32Label_Numbe r).ToString());
lblB->Size = System::Drawing ::Size(i32Label Width, i32LabelHeight) ;
lblB->TextAlign = System::Drawing ::ContentAlignm ent::MiddleLeft ;
lblB->ForeColor = System::Drawing ::Color::Yellow ;
lblB->BackColor = System::Drawing ::Color::Blue;
lblB->Font = new System::Drawing ::Font(S"Micros oft Sans Serif", 9.75F,
System::Drawing ::FontStyle::Bo ld, System::Drawing ::GraphicsUnit: :Point,
(System::Byte)1 62);
//----------------------------------------------------------------------------------------
//Update the current i32Current_Pos_ X and i32Current_Pos_ Y
i32Current_Pos_ X += (i32LabelWidth + i32SpaceBetween Controls) ;
//i32Current_Pos_ Y += ;
//+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++++
// Initialize the Label and TextBox controls.
lblC->Location = System::Drawing ::Point(i32Curr ent_Pos_X,
i32Current_Pos_ Y);
lblC->Text = String::Concat( S"lblC_Name_ ",
(i32Label_Numbe r).ToString());
lblC->Name = String::Concat( S"lblC_Name_ ",
(i32Label_Numbe r).ToString());
lblC->Size = System::Drawing ::Size(i32Label Width, i32LabelHeight) ;
lblC->TextAlign = System::Drawing ::ContentAlignm ent::MiddleLeft ;
lblC->ForeColor = System::Drawing ::Color::Yellow ;
lblC->BackColor = System::Drawing ::Color::Gray;
lblC->Font = new System::Drawing ::Font(S"Micros oft Sans Serif", 9.75F,
System::Drawing ::FontStyle::Bo ld, System::Drawing ::GraphicsUnit: :Point,
(System::Byte)1 62);
//----------------------------------------------------------------------------------------
//Update the current i32Current_Pos_ X and i32Current_Pos_ Y
i32Current_Pos_ X = i32Current_Colu mn_Pos_X ;
i32Current_Pos_ Y = i32Current_Pos_ Y + i32LabelHeight +
i32SpaceBetween Controls; ;
if ( (this->panel1->Height - i32Current_Pos_ Y) >=
(i32SpaceBetwee nControls + i32LabelHeight) )
{
}
else
{
//Alta sira konulamaz, yana konulabilir
i32Current_Colu mn_Pos_X = i32Current_Pos_ X + i32LabelWidth +
i32SpaceBetween Controls +
i32LabelWidth + i32SpaceBetween Controls + i32LabelWidth +
(5*i32SpaceBetw eenControls) ;
i32Current_Pos_ X = i32Current_Colu mn_Pos_X;
i32Current_Pos_ Y = 8;
}
// Add the Label controls to the Panel.
panel1->Controls->Add(lblA);
panel1->Controls->Add(lblB);
panel1->Controls->Add(lblC);
// Add the event handler that will handle the event when the
// mouse is hovered
lblA->Click += new System::EventHa ndler(this, labelXXX_Click) ;
lblB->Click += new System::EventHa ndler(this, labelXXX_Click) ;
lblC->MouseHover += new System::EventHa ndler(this, labelXXX_MouseH over);
//Buton Enable
btnClearPanel->Enabled = true;
}
catch(Exception * exp)
{
MessageBox::Sho w(Convert::ToSt ring(exp), S"Exception Thrown");
}
}
private: System::Void bntKadarSiraYar at_Click(System ::Object * sender,
System::EventAr gs * e)
{
if (txtSiraSayisi->Text->Trim()->Length >0 )
{
Int32 i32SiraSayisi = Convert::ToInt3 2(txtSiraSayisi->Text->Trim());
LARGE_INTEGER i64StartTime, i64StopTime , i64Frequency ;
//CPU frekansını al
QueryPerformanc eFrequency(&i64 Frequency);
if (QueryPerforman ceCounter(&i64S tartTime) != 0)
{
for( Int32 i32Index = 1; i32Index <= i32SiraSayisi; ++i32Index)
{
if ( ((this->panel1->Height - i32Current_Pos_ Y) >=
(i32SpaceBetwee nControls + i32LabelHeight) ) &&
( (this->panel1->Width - i32Current_Pos_ X) >= ( (5 *
i32SpaceBetween Controls) + (3*i32LabelWidt h))) )
{
CreateRows_Func tion();
//this->Refresh();
txtSiraSayisi->Text = Convert::ToStri ng(
Convert::ToInt3 2(txtSiraSayisi->Text->Trim()) - 1 );
}
else
{
break;
}
}
QueryPerformanc eCounter(&i64St opTime);
lblSure->Text = String::Concat( S"Row Creating Duration = ",
((long int)(double)((i 64StopTime.Quad Part - i64StartTime.Qu adPart) *
1000.0) / ((double)i64Fre quency.QuadPart )).ToString(),
S" mili seconds");
}
txtSiraSayisi->Focus();
}
}
private: System::Void btnClearPanel_C lick(System::Ob ject * sender,
System::EventAr gs * e)
{
try
{
LARGE_INTEGER i64StartTime, i64StopTime , i64Frequency ;
//CPU frekansını al
QueryPerformanc eFrequency(&i64 Frequency);
if (QueryPerforman ceCounter(&i64S tartTime) != 0)
{
panel1->Controls->Clear();
//Initialize the curretn location
i32Current_Pos_ X = 8;
i32Current_Pos_ Y = 8;
i32Current_Colu mn_Pos_X = 8 ;
i32Label_Number = 0;
//Buton Enable
btnClearPanel->Enabled = false;
QueryPerformanc eCounter(&i64St opTime);
lblSure->Text = String::Concat( S"Clearing Duration = ",
((long int)(double)((i 64StopTime.Quad Part - i64StartTime.Qu adPart) *
1000.0) / ((double)i64Fre quency.QuadPart )).ToString(),
S" mili seconds");
}
}
catch(...)
{
}
}
private: System::Void labelXXX_MouseH over(System::Ob ject * sender,
System::EventAr gs * e)
{
if (String::Compar e(sender->GetType()->ToString(),
S"System.Window s.Forms.Label") == 0)
{
// Create a button object to use in its place
Label* myLabel = dynamic_cast<La bel*>(sender);
// Display the text to the user
MessageBox::Sho w(String::Conca t(myLabel->Text, S" MouseHover"),
this->Text,
MessageBoxButto ns::OK, MessageBoxIcon: :Information);
}
}
private: System::Void labelXXX_Click( System::Object * sender,
System::EventAr gs * e)
{
if (String::Compar e(sender->GetType()->ToString(),
S"System.Window s.Forms.Label") == 0)
{
// Create a button object to use in its place
Label* myLabel = dynamic_cast<La bel*>(sender);
// Display the text to the user
MessageBox::Sho w(String::Conca t(myLabel->Text, S" is clicked!" ) ,
this->Text,
MessageBoxButto ns::OK, MessageBoxIcon: :Information);
}
}
private: System::Void Form1_Load(Syst em::Object * sender, System::EventAr gs
* e)
{
this->MinimumSize = System::Drawing ::Size(this->get_Width(),
this->get_Height() );
}
private: System::Void Form1_DoubleCli ck(System::Obje ct * sender,
System::EventAr gs * e)
{
this->WindowState = Maximized;
}
private: System::Void timer1_Tick(Sys tem::Object * sender,
System::EventAr gs * e)
{
if ( this->panel1 != NULL )
lblControlCount->Text = String::Concat( S"Number of Controls on Panel =
", this->panel1->Controls->Count.ToString ());
}
};
}