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_Denemeleri"
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
(e-mail address removed)
**********************************************************/
#pragma once
#include <windows.h>
#ifdef MessageBox
#undef MessageBox
#endif
namespace Panel_Denemeleri
{
using namespace System;
using namespace System::Threading;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:
ata;
using namespace System:
rawing;
/// <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)
{
InitializeComponent();
}
////////////////////////////////////////////////////////////////////////////////////////////
//Panel
Panel __gc *panel1;
Thread* myThread;
////////////////////////////////////////////////////////////////////////////////////////////
static Int32 i32Panel_Number = 0;
static Int32 i32Label_Number = 0;
static Int32 i32SpaceBetweenControls = 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_Column_Pos_X = 8 ;
////////////////////////////////////////////////////////////////////////////////////////////
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super:
ispose(disposing);
}
private: System::Windows::Forms::Button * bntCreatePanel;
private: System::Windows::Forms::TextBox * txtSiraSayisi;
private: System::Windows::Forms::Button * btnKadarSiraYarat;
private: System::Windows::Forms::Button * btnClearPanel;
private: System::Windows::Forms::Label * lblSure;
private: System::ComponentModel::IContainer * 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 InitializeComponent(void)
{
this->components = new System::ComponentModel::Container();
this->bntCreatePanel = new System::Windows::Forms::Button();
this->txtSiraSayisi = new System::Windows::Forms::TextBox();
this->btnKadarSiraYarat = new System::Windows::Forms::Button();
this->btnClearPanel = new System::Windows::Forms::Button();
this->lblSure = new System::Windows::Forms::Label();
this->lblControlCount = new System::Windows::Forms::Label();
this->timer1 = new System::Windows::Forms::Timer(this->components);
this->btnPreviousPage = new System::Windows::Forms::Button();
this->btnNextPage = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// bntCreatePanel
//
this->bntCreatePanel->BackColor =
System:
rawing::Color::FromArgb((System::Byte)255, (System::Byte)255,
(System::Byte)192);
this->bntCreatePanel->Font = new System:
rawing::Font(S"Microsoft Sans
Serif", 9, System:
rawing::FontStyle::Bold,
System:
rawing::GraphicsUnit:
oint, (System::Byte)162);
this->bntCreatePanel->ForeColor =
System:
rawing::Color::FromArgb((System::Byte)0, (System::Byte)0,
(System::Byte)192);
this->bntCreatePanel->Location = System:
rawing:
oint(8, 8);
this->bntCreatePanel->Name = S"bntCreatePanel";
this->bntCreatePanel->Size = System:
rawing::Size(104, 32);
this->bntCreatePanel->TabIndex = 0;
this->bntCreatePanel->Text = S"Create Panel";
this->bntCreatePanel->Click += new System::EventHandler(this,
bntCreatePanel_Click);
//
// txtSiraSayisi
//
this->txtSiraSayisi->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtSiraSayisi->CharacterCasing =
System::Windows::Forms::CharacterCasing::Upper;
this->txtSiraSayisi->Enabled = false;
this->txtSiraSayisi->Font = new System:
rawing::Font(S"Microsoft Sans
Serif", 20.25F, System:
rawing::FontStyle::Regular,
System:
rawing::GraphicsUnit:
oint, (System::Byte)162);
this->txtSiraSayisi->ImeMode = System::Windows::Forms::ImeMode::NoControl;
this->txtSiraSayisi->Location = System:
rawing:
oint(128, 8);
this->txtSiraSayisi->MaxLength = 3;
this->txtSiraSayisi->Name = S"txtSiraSayisi";
this->txtSiraSayisi->Size = System:
rawing::Size(64, 31);
this->txtSiraSayisi->TabIndex = 1;
this->txtSiraSayisi->Text = S"";
this->txtSiraSayisi->TextAlign =
System::Windows::Forms::HorizontalAlignment::Center;
//
// btnKadarSiraYarat
//
this->btnKadarSiraYarat->BackColor =
System:
rawing::Color::FromArgb((System::Byte)255, (System::Byte)255,
(System::Byte)192);
this->btnKadarSiraYarat->Enabled = false;
this->btnKadarSiraYarat->Font = new System:
rawing::Font(S"Microsoft
Sans Serif", 9, System:
rawing::FontStyle::Bold,
System:
rawing::GraphicsUnit:
oint, (System::Byte)162);
this->btnKadarSiraYarat->ForeColor =
System:
rawing::Color::FromArgb((System::Byte)0, (System::Byte)0,
(System::Byte)192);
this->btnKadarSiraYarat->Location = System:
rawing:
oint(200, 8);
this->btnKadarSiraYarat->Name = S"btnKadarSiraYarat";
this->btnKadarSiraYarat->Size = System:
rawing::Size(72, 32);
this->btnKadarSiraYarat->TabIndex = 2;
this->btnKadarSiraYarat->Text = S"pcs. Row";
this->btnKadarSiraYarat->Click += new System::EventHandler(this,
bntKadarSiraYarat_Click);
//
// btnClearPanel
//
this->btnClearPanel->BackColor =
System:
rawing::Color::FromArgb((System::Byte)255, (System::Byte)255,
(System::Byte)192);
this->btnClearPanel->Enabled = false;
this->btnClearPanel->Font = new System:
rawing::Font(S"Microsoft Sans
Serif", 9, System:
rawing::FontStyle::Bold,
System:
rawing::GraphicsUnit:
oint, (System::Byte)162);
this->btnClearPanel->ForeColor =
System:
rawing::Color::FromArgb((System::Byte)0, (System::Byte)0,
(System::Byte)192);
this->btnClearPanel->Location = System:
rawing:
oint(280, 8);
this->btnClearPanel->Name = S"btnClearPanel";
this->btnClearPanel->Size = System:
rawing::Size(80, 32);
this->btnClearPanel->TabIndex = 3;
this->btnClearPanel->Text = S"Clear Panel";
this->btnClearPanel->Click += new System::EventHandler(this,
btnClearPanel_Click);
//
// lblSure
//
this->lblSure->BackColor = System:
rawing::Color::Silver;
this->lblSure->Font = new System:
rawing::Font(S"Microsoft Sans Serif",
12, System:
rawing::FontStyle::Bold, System:
rawing::GraphicsUnit:
oint,
(System::Byte)162);
this->lblSure->ForeColor = System:
rawing::Color::Red;
this->lblSure->Location = System:
rawing:
oint(384, 8);
this->lblSure->Name = S"lblSure";
this->lblSure->Size = System:
rawing::Size(624, 16);
this->lblSure->TabIndex = 4;
this->lblSure->TextAlign = System:
rawing::ContentAlignment::MiddleCenter;
//
// lblControlCount
//
this->lblControlCount->BackColor = System:
rawing::Color::Silver;
this->lblControlCount->Font = new System:
rawing::Font(S"Microsoft Sans
Serif", 12, System:
rawing::FontStyle::Bold,
System:
rawing::GraphicsUnit:
oint, (System::Byte)162);
this->lblControlCount->ForeColor = System:
rawing::Color::Red;
this->lblControlCount->Location = System:
rawing:
oint(384, 32);
this->lblControlCount->Name = S"lblControlCount";
this->lblControlCount->Size = System:
rawing::Size(624, 16);
this->lblControlCount->TabIndex = 5;
this->lblControlCount->TextAlign =
System:
rawing::ContentAlignment::MiddleCenter;
//
// timer1
//
this->timer1->Enabled = true;
this->timer1->Interval = 800;
this->timer1->Tick += new System::EventHandler(this, timer1_Tick);
//
// btnPreviousPage
//
this->btnPreviousPage->BackColor =
System:
rawing::Color::FromArgb((System::Byte)255, (System::Byte)255,
(System::Byte)192);
this->btnPreviousPage->Enabled = false;
this->btnPreviousPage->Font = new System:
rawing::Font(S"Microsoft Sans
Serif", 9, System:
rawing::FontStyle::Bold,
System:
rawing::GraphicsUnit:
oint, (System::Byte)162);
this->btnPreviousPage->ForeColor =
System:
rawing::Color::FromArgb((System::Byte)0, (System::Byte)0,
(System::Byte)192);
this->btnPreviousPage->Location = System:
rawing:
oint(1024, 8);
this->btnPreviousPage->Name = S"btnPreviousPage";
this->btnPreviousPage->Size = System:
rawing::Size(96, 40);
this->btnPreviousPage->TabIndex = 6;
this->btnPreviousPage->Text = S"<<< Previous";
//
// btnNextPage
//
this->btnNextPage->BackColor =
System:
rawing::Color::FromArgb((System::Byte)255, (System::Byte)255,
(System::Byte)192);
this->btnNextPage->Enabled = false;
this->btnNextPage->Font = new System:
rawing::Font(S"Microsoft Sans
Serif", 9, System:
rawing::FontStyle::Bold,
System:
rawing::GraphicsUnit:
oint, (System::Byte)162);
this->btnNextPage->ForeColor =
System:
rawing::Color::FromArgb((System::Byte)0, (System::Byte)0,
(System::Byte)192);
this->btnNextPage->Location = System:
rawing:
oint(1128, 8);
this->btnNextPage->Name = S"btnNextPage";
this->btnNextPage->Size = System:
rawing::Size(80, 40);
this->btnNextPage->TabIndex = 7;
this->btnNextPage->Text = S"Next >>>";
//
// Form1
//
this->AutoScaleBaseSize = System:
rawing::Size(5, 13);
this->BackColor = System:
rawing::Color::Black;
this->ClientSize = System:
rawing::Size(1262, 725);
this->Controls->Add(this->btnNextPage);
this->Controls->Add(this->btnPreviousPage);
this->Controls->Add(this->lblControlCount);
this->Controls->Add(this->lblSure);
this->Controls->Add(this->btnClearPanel);
this->Controls->Add(this->btnKadarSiraYarat);
this->Controls->Add(this->txtSiraSayisi);
this->Controls->Add(this->bntCreatePanel);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
this->MaximizeBox = false;
this->MinimizeBox = false;
this->Name = S"Form1";
this->StartPosition =
System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = S"Form1 - Once Alt Alta Sonra Yan Yana Diz";
this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
this->Load += new System::EventHandler(this, Form1_Load);
this->DoubleClick += new System::EventHandler(this, Form1_DoubleClick);
this->ResumeLayout(false);
}
//THIS IS THE FUNCTION WHICH CREATES PANEL
private: System::Void bntCreatePanel_Click(System::Object * sender,
System::EventArgs * e)
{
//Panel
panel1 = new Panel();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Initialize the Panel control.
panel1->Location = System:
rawing:
oint(8,50);
panel1->Size = System:
rawing::Size( (this->get_Width() - 30),
(this->get_Height() - 85));
// Set the Borderstyle for the Panel to three-dimensional.
panel1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
//Backcolor
panel1->set_BackColor(System:
rawing::Color::Aqua);
//Name
panel1->Name = String::Concat(S"Panel_Name_",
(++i32Panel_Number).ToString());
//TAG
panel1->Tag = i32Panel_Number.ToString();
//----------------------------------------------------------------------------------------
// Add the Panel control to the form.
this->Controls->Add(panel1);
txtSiraSayisi->Enabled = true;
btnKadarSiraYarat->Enabled = true;
bntCreatePanel->Enabled = false;
}
private: System::Void CreateRows_Function()
{
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:
rawing:
oint(i32Current_Pos_X,
i32Current_Pos_Y);
lblA->Text = String::Concat(S"Label_", (++i32Label_Number).ToString());
lblA->Name = String::Concat(S"lblA_Name_",
(i32Label_Number).ToString());
lblA->Size = System:
rawing::Size(i32LabelWidth, i32LabelHeight);
lblA->TextAlign = System:
rawing::ContentAlignment::MiddleLeft;
lblA->ForeColor = System:
rawing::Color::Yellow;
lblA->BackColor = System:
rawing::Color::Black;
lblA->Font = new System:
rawing::Font(S"Microsoft Sans Serif", 9.75F,
System:
rawing::FontStyle::Bold, System:
rawing::GraphicsUnit:
oint,
(System::Byte)162);
//----------------------------------------------------------------------------------"------
//Update the current i32Current_Pos_X and i32Current_Pos_Y
i32Current_Pos_X += (i32LabelWidth + i32SpaceBetweenControls) ;
//i32Current_Pos_Y += ;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Initialize the Label and TextBox controls.
lblB->Location = System:
rawing:
oint(i32Current_Pos_X,
i32Current_Pos_Y);
lblB->Text = String::Concat(S"lblB_Name_",
(i32Label_Number).ToString());
lblB->Name = String::Concat(S"lblB_Name_",
(i32Label_Number).ToString());
lblB->Size = System:
rawing::Size(i32LabelWidth, i32LabelHeight);
lblB->TextAlign = System:
rawing::ContentAlignment::MiddleLeft;
lblB->ForeColor = System:
rawing::Color::Yellow;
lblB->BackColor = System:
rawing::Color::Blue;
lblB->Font = new System:
rawing::Font(S"Microsoft Sans Serif", 9.75F,
System:
rawing::FontStyle::Bold, System:
rawing::GraphicsUnit:
oint,
(System::Byte)162);
//----------------------------------------------------------------------------------------
//Update the current i32Current_Pos_X and i32Current_Pos_Y
i32Current_Pos_X += (i32LabelWidth + i32SpaceBetweenControls) ;
//i32Current_Pos_Y += ;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Initialize the Label and TextBox controls.
lblC->Location = System:
rawing:
oint(i32Current_Pos_X,
i32Current_Pos_Y);
lblC->Text = String::Concat(S"lblC_Name_",
(i32Label_Number).ToString());
lblC->Name = String::Concat(S"lblC_Name_",
(i32Label_Number).ToString());
lblC->Size = System:
rawing::Size(i32LabelWidth, i32LabelHeight);
lblC->TextAlign = System:
rawing::ContentAlignment::MiddleLeft;
lblC->ForeColor = System:
rawing::Color::Yellow;
lblC->BackColor = System:
rawing::Color::Gray;
lblC->Font = new System:
rawing::Font(S"Microsoft Sans Serif", 9.75F,
System:
rawing::FontStyle::Bold, System:
rawing::GraphicsUnit:
oint,
(System::Byte)162);
//----------------------------------------------------------------------------------------
//Update the current i32Current_Pos_X and i32Current_Pos_Y
i32Current_Pos_X = i32Current_Column_Pos_X ;
i32Current_Pos_Y = i32Current_Pos_Y + i32LabelHeight +
i32SpaceBetweenControls; ;
if ( (this->panel1->Height - i32Current_Pos_Y) >=
(i32SpaceBetweenControls + i32LabelHeight) )
{
}
else
{
//Alta sira konulamaz, yana konulabilir
i32Current_Column_Pos_X = i32Current_Pos_X + i32LabelWidth +
i32SpaceBetweenControls +
i32LabelWidth + i32SpaceBetweenControls + i32LabelWidth +
(5*i32SpaceBetweenControls) ;
i32Current_Pos_X = i32Current_Column_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::EventHandler(this, labelXXX_Click);
lblB->Click += new System::EventHandler(this, labelXXX_Click);
lblC->MouseHover += new System::EventHandler(this, labelXXX_MouseHover);
//Buton Enable
btnClearPanel->Enabled = true;
}
catch(Exception* exp)
{
MessageBox::Show(Convert::ToString(exp), S"Exception Thrown");
}
}
private: System::Void bntKadarSiraYarat_Click(System::Object * sender,
System::EventArgs * e)
{
if (txtSiraSayisi->Text->Trim()->Length >0 )
{
Int32 i32SiraSayisi = Convert::ToInt32(txtSiraSayisi->Text->Trim());
LARGE_INTEGER i64StartTime, i64StopTime , i64Frequency ;
//CPU frekansını al
QueryPerformanceFrequency(&i64Frequency);
if (QueryPerformanceCounter(&i64StartTime) != 0)
{
for( Int32 i32Index = 1; i32Index <= i32SiraSayisi; ++i32Index)
{
if ( ((this->panel1->Height - i32Current_Pos_Y) >=
(i32SpaceBetweenControls + i32LabelHeight)) &&
( (this->panel1->Width - i32Current_Pos_X) >= ( (5 *
i32SpaceBetweenControls) + (3*i32LabelWidth))) )
{
CreateRows_Function();
//this->Refresh();
txtSiraSayisi->Text = Convert::ToString(
Convert::ToInt32(txtSiraSayisi->Text->Trim()) - 1 );
}
else
{
break;
}
}
QueryPerformanceCounter(&i64StopTime);
lblSure->Text = String::Concat( S"Row Creating Duration = ",
((long int)(double)((i64StopTime.QuadPart - i64StartTime.QuadPart) *
1000.0) / ((double)i64Frequency.QuadPart)).ToString(),
S" mili seconds");
}
txtSiraSayisi->Focus();
}
}
private: System::Void btnClearPanel_Click(System::Object * sender,
System::EventArgs * e)
{
try
{
LARGE_INTEGER i64StartTime, i64StopTime , i64Frequency ;
//CPU frekansını al
QueryPerformanceFrequency(&i64Frequency);
if (QueryPerformanceCounter(&i64StartTime) != 0)
{
panel1->Controls->Clear();
//Initialize the curretn location
i32Current_Pos_X = 8;
i32Current_Pos_Y = 8;
i32Current_Column_Pos_X = 8 ;
i32Label_Number = 0;
//Buton Enable
btnClearPanel->Enabled = false;
QueryPerformanceCounter(&i64StopTime);
lblSure->Text = String::Concat( S"Clearing Duration = ",
((long int)(double)((i64StopTime.QuadPart - i64StartTime.QuadPart) *
1000.0) / ((double)i64Frequency.QuadPart)).ToString(),
S" mili seconds");
}
}
catch(...)
{
}
}
private: System::Void labelXXX_MouseHover(System::Object * sender,
System::EventArgs * e)
{
if (String::Compare(sender->GetType()->ToString(),
S"System.Windows.Forms.Label") == 0)
{
// Create a button object to use in its place
Label* myLabel = dynamic_cast<Label*>(sender);
// Display the text to the user
MessageBox::Show(String::Concat(myLabel->Text, S" MouseHover"),
this->Text,
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
}
private: System::Void labelXXX_Click(System::Object * sender,
System::EventArgs * e)
{
if (String::Compare(sender->GetType()->ToString(),
S"System.Windows.Forms.Label") == 0)
{
// Create a button object to use in its place
Label* myLabel = dynamic_cast<Label*>(sender);
// Display the text to the user
MessageBox::Show(String::Concat(myLabel->Text, S" is clicked!" ) ,
this->Text,
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
}
private: System::Void Form1_Load(System::Object * sender, System::EventArgs
* e)
{
this->MinimumSize = System:
rawing::Size(this->get_Width(),
this->get_Height());
}
private: System::Void Form1_DoubleClick(System::Object * sender,
System::EventArgs * e)
{
this->WindowState = Maximized;
}
private: System::Void timer1_Tick(System::Object * sender,
System::EventArgs * e)
{
if ( this->panel1 != NULL )
lblControlCount->Text = String::Concat(S"Number of Controls on Panel =
", this->panel1->Controls->Count.ToString());
}
};
}