Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How do I add controls on a window through code
 

How do I add controls on a window through code

I have a CLR form dialog application with some menues. When I select that menue item from the program a function is executed. I wan't that function to add a control to my application window so I can then click it and perform other tasks. How do I do that? And I might point out that I assume this is best accomplished with a control. But I actually wan't a picture to show up on my application window and then being able to detect when the mouse is clicked in it.
jfri
Jan Friberg  Saturday, August 22, 2009 8:31 PM
Hi Jan Friberg,

Sorry I do not know that you are using C++.NET. My previous code is made in C#. And here is the code for C++.
#pragma once
namespace WinformCLR {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
newBtn = gcnew System::Windows::Forms::Button();
}

protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::MenuStrip^ menuStrip1;
protected:
private: System::Windows::Forms::ToolStripMenuItem^ fileToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ showButtonToolStripMenuItem;

private:
System::ComponentModel::Container ^components;
System::Windows::Forms::Button^ newBtn;

#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->showButtonToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->menuStrip1->SuspendLayout();
this->SuspendLayout();
//
// menuStrip1
//
this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->fileToolStripMenuItem});
this->menuStrip1->Location = System::Drawing::Point(0, 0);
this->menuStrip1->Name = L"menuStrip1";
this->menuStrip1->Size = System::Drawing::Size(487, 24);
this->menuStrip1->TabIndex = 0;
this->menuStrip1->Text = L"menuStrip1";
//
// fileToolStripMenuItem
//
this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->showButtonToolStripMenuItem});
this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
this->fileToolStripMenuItem->Size = System::Drawing::Size(37, 20);
this->fileToolStripMenuItem->Text = L"File";
//
// showButtonToolStripMenuItem
//
this->showButtonToolStripMenuItem->Name = L"showButtonToolStripMenuItem";
this->showButtonToolStripMenuItem->Size = System::Drawing::Size(152, 22);
this->showButtonToolStripMenuItem->Text = L"Show Button";
this->showButtonToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::showButtonToolStripMenuItem_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(487, 349);
this->Controls->Add(this->menuStrip1);
this->MainMenuStrip = this->menuStrip1;
this->Name = L"Form1";
this->Text = L"Form1";
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void showButtonToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
if(!this->Controls->Contains(newBtn))
{
newBtn->Text = L"New Button";
newBtn->Location = System::Drawing::Point(20, 50);
this->Controls->Add(newBtn);
newBtn->Click += gcnew System::EventHandler(this, &Form1::newBtn_Click);
}
}

private: System::Void newBtn_Click(System::Object^ sender, System::EventArgs^ e){
System::Windows::Forms::MessageBox::Show(L"New button is clicked");
}
};
}

Please try the above code and tell me if you have any problem.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, August 26, 2009 1:51 AM
Hi Jan Friberg,

Based on my understanding, you want to create a button and handle it click event dynamically in the code. So please look at the following sample.
public partial class Form1 : Form
{
private Button newBtn = new Button();
public Form1()
{
InitializeComponent();
}

private void showButtonToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!this.Controls.Contains(newBtn))
{
newBtn.Text = "New Button";
newBtn.Location = new Point(20, 50);
newBtn.Click += new EventHandler(newBtn_Click);
this.Controls.Add(newBtn);
}
}

void newBtn_Click(object sender, EventArgs e)
{
MessageBox.Show("Button Clicked");
}
}
In the example, when the ShowButton menu item is clicked, it will add a button named newBtn to the form and handle its click event in the code also. If the form already contains that button, it won't be added. Does this what you need?

If you want to add more button and don't want to calculate its location, you can use FlowLayoutPanel. The FlowLayoutPanel.Controls.Add method can add the button one by one.

If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, August 25, 2009 3:36 AM
I have problems understanding how to use and where to put the code sample. So far it has generated compiler errors
The line private Button newBtn = new Button(); gives compiler error. First it required it to be private: then I got the error message

only static data members can be initialized inside a ref class or value type


In my program I have one

public

ref class Form1 : public System::Windows::Forms::Form

{
lot off stuff Are you reffering to this class with 'public partial class Form1 : Form'?
}


jfri
Jan Friberg  Tuesday, August 25, 2009 11:46 PM
Hi Jan Friberg,

Sorry I do not know that you are using C++.NET. My previous code is made in C#. And here is the code for C++.
#pragma once
namespace WinformCLR {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
newBtn = gcnew System::Windows::Forms::Button();
}

protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::MenuStrip^ menuStrip1;
protected:
private: System::Windows::Forms::ToolStripMenuItem^ fileToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ showButtonToolStripMenuItem;

private:
System::ComponentModel::Container ^components;
System::Windows::Forms::Button^ newBtn;

#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->showButtonToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->menuStrip1->SuspendLayout();
this->SuspendLayout();
//
// menuStrip1
//
this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->fileToolStripMenuItem});
this->menuStrip1->Location = System::Drawing::Point(0, 0);
this->menuStrip1->Name = L"menuStrip1";
this->menuStrip1->Size = System::Drawing::Size(487, 24);
this->menuStrip1->TabIndex = 0;
this->menuStrip1->Text = L"menuStrip1";
//
// fileToolStripMenuItem
//
this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->showButtonToolStripMenuItem});
this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
this->fileToolStripMenuItem->Size = System::Drawing::Size(37, 20);
this->fileToolStripMenuItem->Text = L"File";
//
// showButtonToolStripMenuItem
//
this->showButtonToolStripMenuItem->Name = L"showButtonToolStripMenuItem";
this->showButtonToolStripMenuItem->Size = System::Drawing::Size(152, 22);
this->showButtonToolStripMenuItem->Text = L"Show Button";
this->showButtonToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::showButtonToolStripMenuItem_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(487, 349);
this->Controls->Add(this->menuStrip1);
this->MainMenuStrip = this->menuStrip1;
this->Name = L"Form1";
this->Text = L"Form1";
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void showButtonToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
if(!this->Controls->Contains(newBtn))
{
newBtn->Text = L"New Button";
newBtn->Location = System::Drawing::Point(20, 50);
this->Controls->Add(newBtn);
newBtn->Click += gcnew System::EventHandler(this, &Form1::newBtn_Click);
}
}

private: System::Void newBtn_Click(System::Object^ sender, System::EventArgs^ e){
System::Windows::Forms::MessageBox::Show(L"New button is clicked");
}
};
}

Please try the above code and tell me if you have any problem.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, August 26, 2009 1:51 AM

You can use google to search for other answers

Custom Search

More Threads

• Custom control and P/Invoke
• Loading cursors from the resource in VS 2003 C++.NET
• Icon transparency fails in ListViews etc.
• Publishing properties
• updating a ListView causes it to flash
• where can I download RegionMaster Controls?
• Hi can some one help me on this ( its not a big question )
• C# Active Directory Contact List
• How to add controls(components) to a form at design time?
• Designer Support when Control has abstract base class