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. Windows Forms
  4. close form from its usercontrol

close form from its usercontrol

Scheduled Pinned Locked Moved Windows Forms
questioncsharpwinformscom
9 Posts 5 Posters 2 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.
  • M Offline
    M Offline
    Marcel Vreuls www agentbase nl
    wrote on last edited by
    #1

    Hi All, Just another question. I have several winforms with a usercontrol on it. I re-use these usercontrols on several different forms. SO far so good, but what i would like to do is close the usercontrol AND form on which it resides from a button on the usercontrol. I have been experimenting with events but until now i cannot get it done. Any ideas? suggestions?? Best regads, marcel

    Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>

    M L M 3 Replies Last reply
    0
    • M Marcel Vreuls www agentbase nl

      Hi All, Just another question. I have several winforms with a usercontrol on it. I re-use these usercontrols on several different forms. SO far so good, but what i would like to do is close the usercontrol AND form on which it resides from a button on the usercontrol. I have been experimenting with events but until now i cannot get it done. Any ideas? suggestions?? Best regads, marcel

      Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>

      M Offline
      M Offline
      Matt U
      wrote on last edited by
      #2

      So using a button on the UserControl you want to close its parent form? I think it would be as simple as calling "this.Parent.Close()". I don't know how this wouldn't work since the UserControl's parent should be the form that contains it.

      E 1 Reply Last reply
      0
      • M Matt U

        So using a button on the UserControl you want to close its parent form? I think it would be as simple as calling "this.Parent.Close()". I don't know how this wouldn't work since the UserControl's parent should be the form that contains it.

        E Offline
        E Offline
        Eslam Afifi
        wrote on last edited by
        #3

        One small issue with this is that the parent might not be the Form, it might be, for example, a Panel that might also be a hosted in another Panel. So if this scenario is possible (now or later in the future), the OP should either keep looking for the Form as in the code below or just give the UserControl a reference to the Form during initialization or something so it closes it directly, which is better (constant time instead of linear time).

        var parent = this.Parent;
        while (!(parent is Form))
        parent = parent.Parent;
        parent.Close();

        Eslam Afifi

        J 1 Reply Last reply
        0
        • M Marcel Vreuls www agentbase nl

          Hi All, Just another question. I have several winforms with a usercontrol on it. I re-use these usercontrols on several different forms. SO far so good, but what i would like to do is close the usercontrol AND form on which it resides from a button on the usercontrol. I have been experimenting with events but until now i cannot get it done. Any ideas? suggestions?? Best regads, marcel

          Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          as the others said, however you could take advantage of Control.TopLevelControl to get to the parent Form. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          M 1 Reply Last reply
          0
          • L Luc Pattyn

            as the others said, however you could take advantage of Control.TopLevelControl to get to the parent Form. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            M Offline
            M Offline
            Matt U
            wrote on last edited by
            #5

            Hehe, very true. See, this is why I love CP. Learn something new everyday! :-P

            1 Reply Last reply
            0
            • M Marcel Vreuls www agentbase nl

              Hi All, Just another question. I have several winforms with a usercontrol on it. I re-use these usercontrols on several different forms. SO far so good, but what i would like to do is close the usercontrol AND form on which it resides from a button on the usercontrol. I have been experimenting with events but until now i cannot get it done. Any ideas? suggestions?? Best regads, marcel

              Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>

              M Offline
              M Offline
              Marcel Vreuls www agentbase nl
              wrote on last edited by
              #6

              Thanks Guys, With your help i have found a working solution for me. .NET 2.0 framework. The var i cannot use yet :laugh: private void butSave_Click(object sender, EventArgs e) { try { bool YesNo = xxxSaveData(); if (YesNo) { if (Parent is Form) { Form myParent = (Form)Parent; myParent.Close(); } } } catch (Exception ex) { ExceptionManager.Publish(ex); } }

              Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>

              J 1 Reply Last reply
              0
              • E Eslam Afifi

                One small issue with this is that the parent might not be the Form, it might be, for example, a Panel that might also be a hosted in another Panel. So if this scenario is possible (now or later in the future), the OP should either keep looking for the Form as in the code below or just give the UserControl a reference to the Form during initialization or something so it closes it directly, which is better (constant time instead of linear time).

                var parent = this.Parent;
                while (!(parent is Form))
                parent = parent.Parent;
                parent.Close();

                Eslam Afifi

                J Offline
                J Offline
                johannesnestler
                wrote on last edited by
                #7

                Oh no! Don't do it like this!!! use Control.TopLevelControl property!!!

                E 1 Reply Last reply
                0
                • M Marcel Vreuls www agentbase nl

                  Thanks Guys, With your help i have found a working solution for me. .NET 2.0 framework. The var i cannot use yet :laugh: private void butSave_Click(object sender, EventArgs e) { try { bool YesNo = xxxSaveData(); if (YesNo) { if (Parent is Form) { Form myParent = (Form)Parent; myParent.Close(); } } } catch (Exception ex) { ExceptionManager.Publish(ex); } }

                  Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>

                  J Offline
                  J Offline
                  johannesnestler
                  wrote on last edited by
                  #8

                  Haven't you read the comments? Change to:

                  private void butSave_Click(object sender, EventArgs e)
                  {
                  try
                  {
                  bool YesNo = xxxSaveData();
                  if (YesNo)
                  {
                  Control ctrlSender = sender;
                  (ctrlSender.TopLevelControl as Form).Close();
                  }
                  }
                  catch (Exception ex)
                  {
                  ExceptionManager.Publish(ex);
                  }
                  }

                  or your code depends on the control beeing placed on the form directly.

                  1 Reply Last reply
                  0
                  • J johannesnestler

                    Oh no! Don't do it like this!!! use Control.TopLevelControl property!!!

                    E Offline
                    E Offline
                    Eslam Afifi
                    wrote on last edited by
                    #9

                    Yeah, Luc Pattyn's reply[^] reminded me of it. Of course a direct reference to the Form is better, that's what I said in my reply, I just forgot that the reference is already there as the TopLevelControl property provided by the framework.

                    Eslam Afifi

                    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