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. Disable "close box (X button)" on child forms

Disable "close box (X button)" on child forms

Scheduled Pinned Locked Moved C#
question
7 Posts 4 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.
  • P Offline
    P Offline
    peropata
    wrote on last edited by
    #1

    Hi, how do I disable "close box" on child forms, but leave it functional for "MDI form". I tried:

    private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
    {
    e.Cancel = true;
    }

    it works for child forms, but it also prevents me from closing apllication (with X button) from main MDI form.

    J OriginalGriffO 2 Replies Last reply
    0
    • P peropata

      Hi, how do I disable "close box" on child forms, but leave it functional for "MDI form". I tried:

      private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
      {
      e.Cancel = true;
      }

      it works for child forms, but it also prevents me from closing apllication (with X button) from main MDI form.

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

      The code you have shown is correct. What you need to do is: When the user closes your main form, you must notify the child forms that they are allowed to close. Your code could look something like this;

      private bool m_bAllowClose = false;

      private AllowClose(bool bAllow)
      {
      m_bAllowClose = bAllow;
      }

      private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
      {
      if(!m_bAllowClose)
      e.Cancel = true;
      }

      From your main form you would need to call "AllowClose(true);" for all your child forms

      P 1 Reply Last reply
      0
      • J JF2015

        The code you have shown is correct. What you need to do is: When the user closes your main form, you must notify the child forms that they are allowed to close. Your code could look something like this;

        private bool m_bAllowClose = false;

        private AllowClose(bool bAllow)
        {
        m_bAllowClose = bAllow;
        }

        private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
        {
        if(!m_bAllowClose)
        e.Cancel = true;
        }

        From your main form you would need to call "AllowClose(true);" for all your child forms

        P Offline
        P Offline
        peropata
        wrote on last edited by
        #3

        On which event from main MDI form should I call AllowClose()? If I call it from:

        private void MDIMainForm_FormClosed(object sender, FormClosedEventArgs e)

        OR

        private void MDIMainForm_FormClosing(object sender, FormClosedEventArgs e)

        ChildForm_FormClosing() is validated first and it does not close child form.

        J 1 Reply Last reply
        0
        • P peropata

          On which event from main MDI form should I call AllowClose()? If I call it from:

          private void MDIMainForm_FormClosed(object sender, FormClosedEventArgs e)

          OR

          private void MDIMainForm_FormClosing(object sender, FormClosedEventArgs e)

          ChildForm_FormClosing() is validated first and it does not close child form.

          J Offline
          J Offline
          JF2015
          wrote on last edited by
          #4

          Ok,if this is not working for you then you can try using the following code in your childs:

          private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
          {
          if(e.CloseReason != CloseReason.MdiFormClosing)
          e.Cancel = true;
          }

          P _ 2 Replies Last reply
          0
          • J JF2015

            Ok,if this is not working for you then you can try using the following code in your childs:

            private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
            {
            if(e.CloseReason != CloseReason.MdiFormClosing)
            e.Cancel = true;
            }

            P Offline
            P Offline
            peropata
            wrote on last edited by
            #5

            Thanks!

            1 Reply Last reply
            0
            • P peropata

              Hi, how do I disable "close box" on child forms, but leave it functional for "MDI form". I tried:

              private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
              {
              e.Cancel = true;
              }

              it works for child forms, but it also prevents me from closing apllication (with X button) from main MDI form.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              You might also want to look at Disable the Close box on a form[^] which might help with the visible "X" button - I haven;t tried it with MDI children, but it should still work.

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • J JF2015

                Ok,if this is not working for you then you can try using the following code in your childs:

                private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
                {
                if(e.CloseReason != CloseReason.MdiFormClosing)
                e.Cancel = true;
                }

                _ Offline
                _ Offline
                _Erik_
                wrote on last edited by
                #7

                Or:

                e.Cancel = e.CloseReason != CloseReason.MdiFormClosing;

                I do never use an if for just setting a bool value.

                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