Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating forms at runtime

Status
Not open for further replies.

MasterPeter

Programmer
Nov 17, 2003
1
DE
Hi.

I need to create a form dynamically at runtime and add a few controls in c#.

Adding the controls programmatically is no issue, but... creating the form programatically seems to be a great issue, since the Forms class doesn't have a collection object and therefore the form must be instantiated by design time code.

Can anyone help me?

Regards.
 
You could create form at run time with no problem but you have to follow some steps to accomplish it (like Designer is doing).
Code:
1. Derive your form MyForm from the System.Windows.Forms.Form.
2.Declare as minimum aSystem.ComponentModel.Container member like :
private System.ComponentModel.Container components = null;
3. Declare other controls (Menu, TextEdit, buttons, timers etc) and member variables depending on your needs.
private System.Windows.Forms.MainMenu m_MnuMain;
private System.Windows.Forms.MenuItem m_MnuConnect;
private System.Windows.Forms.OpenFileDialog m_OpenFileDialog;
private System.Windows.Forms.StatusBar m_StatusBar;
//...
4. Implement a constructor  in which will create the above objects and do any initialization
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MyForm));
this.m_MnuMain = new System.Windows.Forms.MainMenu();
this.m_MnuConnect = new System.Windows.Forms.MenuItem();  
this.m_OpenFileDialog = new System.Windows.Forms.OpenFileDialog();
this.m_StatusBar = new System.Windows.Forms.StatusBar();
//...
this.SuspendLayout();
// Set propoerty for each above object
	// m_MnuMain
this.m_MnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
				  this.m_MnuFile,
                 		  this.m_MnuEdit,
				  this.m_MnuConnect});
this.m_MnuConnect.Index = 0;
this.m_MnuConnect.Text = "&Connect";
this.m_MnuConnect.Click += new System.EventHandler(this.m_MnuConnect_Click);
//
this.m_MnuOpenFile.Enabled = false;
this.m_MnuOpenFile.Index = 3;
this.m_MnuOpenFile.Text = "&Open File...";
this.m_MnuOpenFile.Click += new System.EventHandler(this.m_MnuOpenFile_Click);
// m_StatusBar
// you should find out the right (x,y) 
this.m_StatusBar.Location = new System.Drawing.Point(0, 244);
this.m_StatusBar.Name = "m_StatusBar";
this.m_StatusBar.Size = new System.Drawing.Size(728, 22);
this.m_StatusBar.TabIndex = 1;
this.m_StatusBar.Text = "Ready...";

// MyForm
this.Controls.AddRange(new System.Windows.Forms.Control[] {
			  this.m_StatusBar});
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.IsMdiContainer = true;
this.Menu = this.m_MnuMain;
this.Name = "MYForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "My Form";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
this.Load += new System.EventHandler(this.MyForm_Load);
//...
this.ResumeLayout(false);
5. Implement the handlers:
private void MyForm_Load(object sender, System.EventArgs e)
{
     //...
}
private void m_MnuConnect_Click(object sender, System.EventArgs e)
{
    //...
}
private void m_MnuOpenFile_Click(object sender, System.EventArgs e)
{
m_OpenFileDialog.Title="Select source file to open";
//...
}
private void MyForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   //...
}
6. Implement Dispose(bool)
 protected override void Dispose( bool disposing )
{
	if( disposing )
	{
		if (components != null) 
		{
                        // cleanup here
			components.Dispose();
		}
	}
	base.Dispose( disposing );
}
7. Now you can instantiate your form like :
 MyForm mf = new MyForm();
 mf.Show();
etc...

-obislavu-
 
No easy way to do it -- it's just a lot of code.

For an example, look at the section of code in a VS.NET designed form that is marked "designer generated code". You would need to replicate that sort of stuff.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Potentially/probably ignorant response from Delphi programmer who's playing with C#:


I instantiate multiple instances of forms at run time in C#. Is it really not possible to instantiate an empty form and create components on it?



Brian
"There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group." - tag line I stole
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top