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. Closing a User Control from itself

Closing a User Control from itself

Scheduled Pinned Locked Moved C#
6 Posts 5 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.
  • C Offline
    C Offline
    caksabre2
    wrote on last edited by
    #1

    Hi, Does anyone know how I can remove a user control from a panel it has been added too, by a button on itself. Like an x button. Cheers, Chris

    C A K D 4 Replies Last reply
    0
    • C caksabre2

      Hi, Does anyone know how I can remove a user control from a panel it has been added too, by a button on itself. Like an x button. Cheers, Chris

      C Offline
      C Offline
      Calin Tatar
      wrote on last edited by
      #2

      try by using Dispose() method. Calin

      1 Reply Last reply
      0
      • C caksabre2

        Hi, Does anyone know how I can remove a user control from a panel it has been added too, by a button on itself. Like an x button. Cheers, Chris

        A Offline
        A Offline
        ABitSmart
        wrote on last edited by
        #3

        Just guessing, In the so called 'x' button's clickl Button btn = sender as Button; UserControl u = btn.Parent as UserControl ; Panel p = u.Parent as Panel ; p .Controls.Remove(u); (Assuming, Button is in UserControl and UserControl is in the Panel)

        C 1 Reply Last reply
        0
        • C caksabre2

          Hi, Does anyone know how I can remove a user control from a panel it has been added too, by a button on itself. Like an x button. Cheers, Chris

          K Offline
          K Offline
          kbalias
          wrote on last edited by
          #4

          Hi Perhaps you can created a Delegate in the User Control that when fired, will call a method in the parent form that can then remove the User Control. I have not tried the code, but perhaps something like this: In the User Control:

          //declare delegate
          public event EventHandler RemoveSelf;

          private void btnRemoveSelf_Click(object sender, EventArgs e)
          {
          try
          {
          if (RemoveSelf != null)
          {
          RemoveSelf(this, e); //Fire event
          }
          }
          catch (Exception ex)
          {
          MessageBox.Show(ex.Message);
          }
          }

          In the parent form:

          private void Form_ Load(object sender, EventArgs e)
          {
          try
          {
          //Add event handler to User Control
          userControl1.UpdateData += new EventHandler(UC_ RemoveSelf);
          }
          catch (Exception ex)
          {
          MessageBox.Show(ex.Message);
          }
          }

          private void UC_RemoveSelf(object sender, EventArgs e)
          {
          try
          {
          //Code to remove User control
          }
          catch (Exception ex)
          {
          MessageBox.Show(ex.Message);
          }
          }

          As I said I have not tried it, but let me know if it works. Regards. Kobus

          1 Reply Last reply
          0
          • C caksabre2

            Hi, Does anyone know how I can remove a user control from a panel it has been added too, by a button on itself. Like an x button. Cheers, Chris

            D Offline
            D Offline
            Deresen
            wrote on last edited by
            #5

            If I where you I should use events and delegates. You make a delegate in your user control and when you click on the button you will call this. An event in the parents control will catch this call. Because it is an event, you can give the user control as parameter. So you can do something like this: -> loop through the panel -> if the name from the 'sender' (which you cast to a control) is one in the panel -> remove this one

            1 Reply Last reply
            0
            • A ABitSmart

              Just guessing, In the so called 'x' button's clickl Button btn = sender as Button; UserControl u = btn.Parent as UserControl ; Panel p = u.Parent as Panel ; p .Controls.Remove(u); (Assuming, Button is in UserControl and UserControl is in the Panel)

              C Offline
              C Offline
              caksabre2
              wrote on last edited by
              #6

              Thanks, this worked perfectly :)

              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