|
-
Nov 4th, 2007, 06:03 PM
#1
Thread Starter
New Member
[RESOLVED] Initializing Custom Control at runtime
Hi,
I've a user control in a window form application and I want initialize it, at runtime, with a custom method in the Form1 class.
public partial class Form1 : System.Windows.Forms.Form
{
private UC1 myUserControl;
...
In the Form1.cs InitializeComponent method I've :
this.UC1 = initComponent();
This is a method of the Form1 class, that return a myUserControl object.
I can compile but if I switch to the designer I get this error :
Impossible to find the method 'System.Windows.Forms.Form.initComponent'
Why this ?
Last edited by cfirex; Nov 11th, 2007 at 06:38 PM.
-
Nov 4th, 2007, 06:31 PM
#2
Re: Initializing Custom Control at runtime
Where and how have you declared initComponent method?
-
Nov 4th, 2007, 09:49 PM
#3
Re: Initializing Custom Control at runtime
Do not, under any circumstances, edit the code in the InitializeComponent method. That code is generated by the IDE and if you do the wrong thing there you could render your form unusable. What's more, as soon as you make a change in the designer all that code will be regenerated and your code will be lost. You should be adding code either to the form's constructor, AFTER the call to InitializeComponent, or else in the Load event handler.
-
Nov 5th, 2007, 01:41 AM
#4
Re: Initializing Custom Control at runtime
When you drop a user created control on to a form it should automatically get included in the forms initialization code. You shouldnt have to edit it at all.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 5th, 2007, 01:45 AM
#5
Thread Starter
New Member
Re: Initializing Custom Control at runtime
For example, this is Form1.Designer.cs
Code:
namespace WindowsApplication1
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
// this line GENERATED AN ERROR if I try to open the designer :
this.myUserControl = myNewUserControl();
}
#endregion
}
}
and Form1.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : System.Windows.Forms.Form
{
private UserControl1 myUserControl;
public Form1()
{
InitializeComponent();
}
private UserControl1 myNewUserControl()
{
// example empty initialization method !!!
UserControl1 myUC = new UserControl1();
return myUC;
}
}
}
This is a very simple example code, the UserControl1 is a simple label + textbox user control.
-
Nov 5th, 2007, 02:47 AM
#6
Re: Initializing Custom Control at runtime
A UserControl is just like any other control. You're supposed to add them to the form in the designer, just like any other control. If you do need to create one at run time then you do it like this:
C# Code:
MyUserControl uc = new MyUserControl();
// Set properties here.
this.Controls.Add(uc);
just like any other control. As I have already said, do NOT add your own code to the InitializeComponent method. As I have already said, do NOT add your own code to the InitializeComponent method. As I have already said twice, do NOT add your own code to the InitializeComponent method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|