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.ConfigurationSetti ngs.GetConfig("Regions");
IDictionaryEnumerator en = hash.GetEnumerator();
_userctrls = new UserControl1[hash.Count];
int i=0;
while(en.MoveNext())
{
_userctrls[i] = new
UserControl1(en.Key.ToString(),en.Value.ToString() );
//_userctrls[i].Location = ptr;
//userctrls[i].Name = "usc" + i.ToString();
//ptr.Offset(_userctrls[i].Width,0);
//_userctrls[i].Visible = true;
this.Controls.Add(this._userctrls[i]);
}
}
}
}
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(minsfromMelbo urne));
label2.Text = thisday.ToString();
label3.Text = thisday.DayOfWeek.ToString();
}
}
}
if someone can help please do.
thanking you,
NJ 1 4062
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
"np*****@hotmail.com" wrote: 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.ConfigurationSetti ngs.GetConfig("Regions"); IDictionaryEnumerator en = hash.GetEnumerator(); _userctrls = new UserControl1[hash.Count];
int i=0; while(en.MoveNext()) { _userctrls[i] = new UserControl1(en.Key.ToString(),en.Value.ToString() ); //_userctrls[i].Location = ptr; //userctrls[i].Name = "usc" + i.ToString(); //ptr.Offset(_userctrls[i].Width,0); //_userctrls[i].Visible = true;
this.Controls.Add(this._userctrls[i]); }
} } }
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(minsfromMelbo urne)); label2.Text = thisday.ToString(); label3.Text = thisday.DayOfWeek.ToString(); } } }
if someone can help please do.
thanking you, NJ
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: sameer mowade via .NET 247 |
last post by:
Hello All,
I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
|
by: Bas Groeneveld |
last post by:
I am developing an ASP.NET application part of which consists of a data
entry wizard defined by entries in a data table - ie the controls on each
page of the wizard are determined by definitions in...
|
by: Steve Booth |
last post by:
I have a web form with a button and a placeholder, the button adds a user
control to the placeholder (and removes any existing controls). The user
control contains a single button. I have done all...
|
by: netasp |
last post by:
hi all,
how can I populate one aspx form when page is loading based on page ID? for
example:
loading page A (to search for VB code) would display labels and texboxes,
dropdown lists all related...
|
by: |
last post by:
I have made some user controls with custom properties. I can set those
properties on instances of my user controls, and I have programmed my user
control to do useful visual things in response to...
| |
by: Chris |
last post by:
I am dynamically adding a user control to each row in a gridview. The reason
I am doing it dynamically is the user control is different depending on
certain data in the gridview. The gridview...
|
by: jelle.huygen |
last post by:
Hello,
I have a problem in ASP.NET 2.0 with the viewstate of my dynamically
added user control. I have reproduced the problem with a very simple
user control and a very simple page.
On my...
|
by: Shraddha |
last post by:
Hi,
I am adding some ASP.Net user controls (.ascx file) dynamically on the
button click. The user control will get added as many times userhits
the button. Now on the click of the submit button, I...
|
by: Lewis Holmes |
last post by:
Hi
I have the following situation in one of my asp.net pages.
The user can add multiple table rows to a form by selecting a button. These
rows can contain asp.net controls. When this button is...
|
by: semomaniz |
last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |