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. Displaying a second user control in a form

Displaying a second user control in a form

Scheduled Pinned Locked Moved C#
csharphelpquestion
5 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

    I'm having a problem making a User Control display upon a form in C#. My form starts off with a single UC on it. Its called UCInit. UCInit contains some text and a couple of buttons. When one of the buttons is clicked, I want UCInit to remove itself from the form, and a second User Control, called UCCreate, to appear. I can get rid of the first UserControl, but UCCreate never appears. This is my code so far for when the button in UCInit is clicks: private void btnCreate_Click(object sender, System.EventArgs e) { this.Hide(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point (376, 8); newPanel.Show(); } UCInit removes itself but the instance of UCCreate never shows itself. What am I doing wrong?

    J 1 Reply Last reply
    0
    • F frogb0x

      I'm having a problem making a User Control display upon a form in C#. My form starts off with a single UC on it. Its called UCInit. UCInit contains some text and a couple of buttons. When one of the buttons is clicked, I want UCInit to remove itself from the form, and a second User Control, called UCCreate, to appear. I can get rid of the first UserControl, but UCCreate never appears. This is my code so far for when the button in UCInit is clicks: private void btnCreate_Click(object sender, System.EventArgs e) { this.Hide(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point (376, 8); newPanel.Show(); } UCInit removes itself but the instance of UCCreate never shows itself. What am I doing wrong?

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      You also need to do:

      newPanel.Size=[whatever-the-size-is];
      this.Controls.Add(newPanel);

      "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
      "You must be the change you wish to see in the world." - Mahatma Gandhi

      F 1 Reply Last reply
      0
      • J J Dunlap

        You also need to do:

        newPanel.Size=[whatever-the-size-is];
        this.Controls.Add(newPanel);

        "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
        "You must be the change you wish to see in the world." - Mahatma Gandhi

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

        Still no love. This is my amended code: private void btnCreate_Click(object sender, System.EventArgs e) { this.Hide(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point(376, 8); newPanel.Size = new Size (280, 140); this.Controls.Add(newPanel); newPanel.Show(); }

        J 1 Reply Last reply
        0
        • F frogb0x

          Still no love. This is my amended code: private void btnCreate_Click(object sender, System.EventArgs e) { this.Hide(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point(376, 8); newPanel.Size = new Size (280, 140); this.Controls.Add(newPanel); newPanel.Show(); }

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          You're adding UCCreate to the control you are hiding, you need to add it to the controls collection of the form instead. this.Parent.Controls.Add(newPanel); should give you what you want. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

          F 1 Reply Last reply
          0
          • J James T Johnson

            You're adding UCCreate to the control you are hiding, you need to add it to the controls collection of the form instead. this.Parent.Controls.Add(newPanel); should give you what you want. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

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

            So once I add that code into the constructor of my form, how does the first user control know about it? That is, how do I let other Controls know that newPanel exists? Thanks for the tip! :)

            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