Results 1 to 6 of 6

Thread: [RESOLVED] Initializing Custom Control at runtime

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    Resolved [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.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Initializing Custom Control at runtime

    Where and how have you declared initComponent method?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. MyUserControl uc = new MyUserControl();
    2.  
    3. // Set properties here.
    4.  
    5. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width