dynamically adding user controls

N

npaulus

Hi,
I am trying to dynamically add user controls on to my web form but for
some reason my form isnt displaying the user control.

form1.cs:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WorldClock
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Point ptr;
private WorldClock.UserControl1[] _userctrls;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(704, 334);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
ptr = new Point(0,0);

Hashtable hash;
hash =
(Hashtable)System.Configuration.ConfigurationSettings.GetConfig("Regions");
IDictionaryEnumerator en = hash.GetEnumerator();
_userctrls = new UserControl1[hash.Count];


int i=0;
while(en.MoveNext())
{
_userctrls = new
UserControl1(en.Key.ToString(),en.Value.ToString());
//_userctrls.Location = ptr;
//userctrls.Name = "usc" + i.ToString();
//ptr.Offset(_userctrls.Width,0);
//_userctrls.Visible = true;

this.Controls.Add(this._userctrls);
}

}
}
}

usercontrol1.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WorldClock
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
private string city="";
private string minsfromMelbourne="";

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitializeComponent call
}

public UserControl1(string City, string MinsFromMelbourne)
{
city = City;
minsfromMelbourne = MinsFromMelbourne;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 24);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 64);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "label2";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 104);
this.label3.Name = "label3";
this.label3.TabIndex = 2;
this.label3.Text = "label3";
//
// UserControl1
//
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "UserControl1";
this.Load += new System.EventHandler(this.UserControl1_Load);
this.ResumeLayout(false);

}
#endregion

private void UserControl1_Load(object sender, System.EventArgs e)
{
label1.Text = city;
TimeSpan ts = new TimeSpan(0,0,int.Parse(minsfromMelbourne),0,0);
DateTime thisday = new DateTime();
thisday = DateTime.Now.AddMinutes(Double.Parse(minsfromMelbourne));
label2.Text = thisday.ToString();
label3.Text = thisday.DayOfWeek.ToString();
}
}
}

if someone can help please do.

thanking you,
NJ
 
G

Guest

You need to add a call to "InitializeComponent" in your custom usercontrol
constructor.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Hi,
I am trying to dynamically add user controls on to my web form but for
some reason my form isnt displaying the user control.

form1.cs:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WorldClock
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Point ptr;
private WorldClock.UserControl1[] _userctrls;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(704, 334);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
ptr = new Point(0,0);

Hashtable hash;
hash =
(Hashtable)System.Configuration.ConfigurationSettings.GetConfig("Regions");
IDictionaryEnumerator en = hash.GetEnumerator();
_userctrls = new UserControl1[hash.Count];


int i=0;
while(en.MoveNext())
{
_userctrls = new
UserControl1(en.Key.ToString(),en.Value.ToString());
//_userctrls.Location = ptr;
//userctrls.Name = "usc" + i.ToString();
//ptr.Offset(_userctrls.Width,0);
//_userctrls.Visible = true;

this.Controls.Add(this._userctrls);
}

}
}
}

usercontrol1.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WorldClock
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
private string city="";
private string minsfromMelbourne="";

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitializeComponent call
}

public UserControl1(string City, string MinsFromMelbourne)
{
city = City;
minsfromMelbourne = MinsFromMelbourne;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 24);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 64);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "label2";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 104);
this.label3.Name = "label3";
this.label3.TabIndex = 2;
this.label3.Text = "label3";
//
// UserControl1
//
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "UserControl1";
this.Load += new System.EventHandler(this.UserControl1_Load);
this.ResumeLayout(false);

}
#endregion

private void UserControl1_Load(object sender, System.EventArgs e)
{
label1.Text = city;
TimeSpan ts = new TimeSpan(0,0,int.Parse(minsfromMelbourne),0,0);
DateTime thisday = new DateTime();
thisday = DateTime.Now.AddMinutes(Double.Parse(minsfromMelbourne));
label2.Text = thisday.ToString();
label3.Text = thisday.DayOfWeek.ToString();
}
}
}

if someone can help please do.

thanking you,
NJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top