Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Making one user control aware of another

Making one user control aware of another

Scheduled Pinned Locked Moved C#
helpwinformsquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    frogb0x
    wrote on last edited by
    #1

    Hello, I have a problem that I'm still stuck on. I have a form with a pair of user controls. The first user control is displayed when the program runs. When a button is clicked, I want the first user control to remove itself from the form, and the second user control to appear. However, when I write the code for the button, all I can do is get the first UC to disappear; the second UC never draws itself. Someone earlier told me to instantiate an instance of the second user control in the form's constructor, and add this line into the form: this.Parent.Controls.Add(newUserControl); However, this still doesn't solve the issue of the first user control not knowing about the second user control. Can someone point me in the right direction?

    L 1 Reply Last reply
    0
    • F frogb0x

      Hello, I have a problem that I'm still stuck on. I have a form with a pair of user controls. The first user control is displayed when the program runs. When a button is clicked, I want the first user control to remove itself from the form, and the second user control to appear. However, when I write the code for the button, all I can do is get the first UC to disappear; the second UC never draws itself. Someone earlier told me to instantiate an instance of the second user control in the form's constructor, and add this line into the form: this.Parent.Controls.Add(newUserControl); However, this still doesn't solve the issue of the first user control not knowing about the second user control. Can someone point me in the right direction?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      public class Form1 : System.Windows.Forms.Form
      {
      private System.Windows.Forms.Button button1;
      private System.Windows.Forms.Button button2;

      private System.ComponentModel.Container components = null;

      public Form1()
      {
      	InitializeComponent();
      }
      
      protected override void Dispose( bool disposing )
      {
      	if( disposing )
      	{
      		if (components != null) 
      		{
      			components.Dispose();
      		}
      	}
      	base.Dispose( disposing );
      }
      
      private void InitializeComponent()
      {
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      
      this.button1.Location = new System.Drawing.Point(64, 112);
      this.button1.Name = "button1";
      this.button1.TabIndex = 0;
      this.button1.Text = "button1";
      this.button1.Click += new System.EventHandler(this.button1\_Click);
      
      this.button2.Location = new System.Drawing.Point(160, 112);
      this.button2.Name = "button2";
      this.button2.TabIndex = 1;
      this.button2.Text = "button2";
      this.button2.Visible = false;
      this.button2.Click += new System.EventHandler(this.button2\_Click);
      
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control\[\] {
                                                                  this.button2,
                                                                  this.button1});
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);
      

      }

      private void button1\_Click(object sender, System.EventArgs e)
      

      {
      button2.Visible = true;
      button1.Visible = false;
      }

      private void button2_Click(object sender, System.EventArgs e)
      {
      button2.Visible = false;
      button1.Visible = true;
      }
      }

      leppie::AllocCPArticle(Generic DFA State Machine for .NET);

      N 1 Reply Last reply
      0
      • L leppie

        public class Form1 : System.Windows.Forms.Form
        {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;

        private System.ComponentModel.Container components = null;

        public Form1()
        {
        	InitializeComponent();
        }
        
        protected override void Dispose( bool disposing )
        {
        	if( disposing )
        	{
        		if (components != null) 
        		{
        			components.Dispose();
        		}
        	}
        	base.Dispose( disposing );
        }
        
        private void InitializeComponent()
        {
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        
        this.button1.Location = new System.Drawing.Point(64, 112);
        this.button1.Name = "button1";
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.Click += new System.EventHandler(this.button1\_Click);
        
        this.button2.Location = new System.Drawing.Point(160, 112);
        this.button2.Name = "button2";
        this.button2.TabIndex = 1;
        this.button2.Text = "button2";
        this.button2.Visible = false;
        this.button2.Click += new System.EventHandler(this.button2\_Click);
        
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.AddRange(new System.Windows.Forms.Control\[\] {
                                                                    this.button2,
                                                                    this.button1});
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        

        }

        private void button1\_Click(object sender, System.EventArgs e)
        

        {
        button2.Visible = true;
        button1.Visible = false;
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
        button2.Visible = false;
        button1.Visible = true;
        }
        }

        leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        How does this help answer the question? Where are the user controls?

        F 1 Reply Last reply
        0
        • N Not Active

          How does this help answer the question? Where are the user controls?

          F Offline
          F Offline
          frogb0x
          wrote on last edited by
          #4

          If it helps, here my code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Copper { public class frmInit : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox pictureBox1; private Copper.UCInit ucInit1; private System.ComponentModel.Container components = null; public frmInit() { InitializeComponent(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point(376, 8); newPanel.Size = new Size (280, 140); this.Parent.Controls.Add(newPanel); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmInit)); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.ucInit1 = new Copper.UCInit(); this.SuspendLayout(); this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(375, 256); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // ucInit1 // this.ucInit1.Location = new System.Drawing.Point(376, 8); this.ucInit1.Name = "ucInit1"; this.ucInit1.Size = new System.Drawing.Size(280, 184); this.ucInit1.TabIndex = 0; this.ucInit1.Load += new System.EventHandler(this.ucInit1_Load); // // frmInit // this.

          L 1 Reply Last reply
          0
          • F frogb0x

            If it helps, here my code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Copper { public class frmInit : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox pictureBox1; private Copper.UCInit ucInit1; private System.ComponentModel.Container components = null; public frmInit() { InitializeComponent(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point(376, 8); newPanel.Size = new Size (280, 140); this.Parent.Controls.Add(newPanel); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmInit)); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.ucInit1 = new Copper.UCInit(); this.SuspendLayout(); this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(375, 256); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // ucInit1 // this.ucInit1.Location = new System.Drawing.Point(376, 8); this.ucInit1.Name = "ucInit1"; this.ucInit1.Size = new System.Drawing.Size(280, 184); this.ucInit1.TabIndex = 0; this.ucInit1.Load += new System.EventHandler(this.ucInit1_Load); // // frmInit // this.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            OK here's a quick and dirty way :) Just make the button public. Just add the following to the main form:

            ucInit1.button.Clicked += new EventHandler(ShowUCCreate);

            ....

            void ShowUCCreate(object sender, EventArgs e)
            {
            UCCreate newPanel = new UCCreate();
            newPanel.Location = new Point(376, 8);
            newPanel.Size = new Size (280, 140);
            this.Parent.Controls.Add(newPanel);
            }

            You should get the idea. Once you understand events they make life alot easier. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

            F 1 Reply Last reply
            0
            • L leppie

              OK here's a quick and dirty way :) Just make the button public. Just add the following to the main form:

              ucInit1.button.Clicked += new EventHandler(ShowUCCreate);

              ....

              void ShowUCCreate(object sender, EventArgs e)
              {
              UCCreate newPanel = new UCCreate();
              newPanel.Location = new Point(376, 8);
              newPanel.Size = new Size (280, 140);
              this.Parent.Controls.Add(newPanel);
              }

              You should get the idea. Once you understand events they make life alot easier. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

              F Offline
              F Offline
              frogb0x
              wrote on last edited by
              #6

              I think that should work. Is there a good primer on Events and the EventHandler() method in general, somewhere?

              L 1 Reply Last reply
              0
              • F frogb0x

                I think that should work. Is there a good primer on Events and the EventHandler() method in general, somewhere?

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                frogb0x wrote: Is there a good primer on Events and the EventHandler() method in general, somewhere? The ones in MSDN are rather difficult to grasp. It took me quite a while to figure out what they are and why I need them. To understand events you need to understand delegates. In fact an event is really just a very limited delegate. A delegate (or function pointer) is basically a function/methods signature. WHat I mean by this is, is that it looks basically like a method but it has the delegate keyword. Think of this as a placeholder for a method. Example:

                delegate void FooHandler(string name);

                class ABC
                {
                public FooHandler Foo;

                public void InvokeFoo()
                {
                Foo("Hello from ABC");
                }
                }

                class XYZ
                {
                static void MyFoo(string name)
                {
                Console.WriteLine(name);
                }

                static void Main()
                {
                ABC abc = new ABC();
                abc.Foo = new FooHandler(MyFoo);
                abc.InvokeFoo();
                }
                }

                Now step into this with the debugger, step into every function (F11). This will show you exactly how it works. This allows you specify a method in a different place or you can change them dynamically. Once you have grasped this, I suggest you look at the MSDN documentation on events. Hope this helps :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups