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. How to close a form in its Load event handler

How to close a form in its Load event handler

Scheduled Pinned Locked Moved C#
comtutorial
10 Posts 7 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.
  • W Offline
    W Offline
    Waleed Eissa
    wrote on last edited by
    #1

    Hi, I have a form, let's say Form1, in Form1 I have some code in its Load event handler, now I want to close Form1 if the code in its Load event handler throws an exception. this.close() simply doesn't work from inside the Load event handler. Thanks for any suggestions... (a.k.a. Wal2k)
    www.wal2k.com

    N M D D W 7 Replies Last reply
    0
    • W Waleed Eissa

      Hi, I have a form, let's say Form1, in Form1 I have some code in its Load event handler, now I want to close Form1 if the code in its Load event handler throws an exception. this.close() simply doesn't work from inside the Load event handler. Thanks for any suggestions... (a.k.a. Wal2k)
      www.wal2k.com

      N Offline
      N Offline
      nemopeti
      wrote on last edited by
      #2

      I don't know why do you want to close a form immediatelly, but the metod is: private void Form1_Load(object sender, System.EventArgs e) { this.Close(); } It must works!

      1 Reply Last reply
      0
      • W Waleed Eissa

        Hi, I have a form, let's say Form1, in Form1 I have some code in its Load event handler, now I want to close Form1 if the code in its Load event handler throws an exception. this.close() simply doesn't work from inside the Load event handler. Thanks for any suggestions... (a.k.a. Wal2k)
        www.wal2k.com

        M Offline
        M Offline
        MoustafaS
        wrote on last edited by
        #3

        What about

        Application.Exit();

        1 Reply Last reply
        0
        • W Waleed Eissa

          Hi, I have a form, let's say Form1, in Form1 I have some code in its Load event handler, now I want to close Form1 if the code in its Load event handler throws an exception. this.close() simply doesn't work from inside the Load event handler. Thanks for any suggestions... (a.k.a. Wal2k)
          www.wal2k.com

          M Offline
          M Offline
          MoustafaS
          wrote on last edited by
          #4

          Simply you can :

          void form1_load(object sender,eventargs e)
          {
          CloseIt();
          }
          void CloseIt()
          {
          this.Close();
          }

          1 Reply Last reply
          0
          • W Waleed Eissa

            Hi, I have a form, let's say Form1, in Form1 I have some code in its Load event handler, now I want to close Form1 if the code in its Load event handler throws an exception. this.close() simply doesn't work from inside the Load event handler. Thanks for any suggestions... (a.k.a. Wal2k)
            www.wal2k.com

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

            hi, Waleed wrote: now I want to close Form1 if the code in its Load event handler throws an exception. The reason why your form didn't close ( but crashed ) is because exceptions change flow of your program. You have to wrap exception throwing code with try-catch block.

            void Page_Load(...)
            {
              try
              {
                YourCodeWhichThrowsAndException();
              }
              catch{ this.Close(); }
            }
            

            Does it help? David

            1 Reply Last reply
            0
            • W Waleed Eissa

              Hi, I have a form, let's say Form1, in Form1 I have some code in its Load event handler, now I want to close Form1 if the code in its Load event handler throws an exception. this.close() simply doesn't work from inside the Load event handler. Thanks for any suggestions... (a.k.a. Wal2k)
              www.wal2k.com

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              A much better method would be to determine if your form needs to be displayed before you even instantiate it. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              1 Reply Last reply
              0
              • W Waleed Eissa

                Hi, I have a form, let's say Form1, in Form1 I have some code in its Load event handler, now I want to close Form1 if the code in its Load event handler throws an exception. this.close() simply doesn't work from inside the Load event handler. Thanks for any suggestions... (a.k.a. Wal2k)
                www.wal2k.com

                W Offline
                W Offline
                Waleed Eissa
                wrote on last edited by
                #7

                hello, thanks for all your replies .. I'm afraid though the problem hasn't been solved .. as I said in my first post, using this.Close() doesn't work at all, doesn't matter if you call it directly from the Load event handler (let's say Form1_Load) or put it in a method and call this method from within Form1_Load.. it still doesn't work! (a.k.a. Wal2k)
                www.wal2k.com

                1 Reply Last reply
                0
                • W Waleed Eissa

                  Hi, I have a form, let's say Form1, in Form1 I have some code in its Load event handler, now I want to close Form1 if the code in its Load event handler throws an exception. this.close() simply doesn't work from inside the Load event handler. Thanks for any suggestions... (a.k.a. Wal2k)
                  www.wal2k.com

                  W Offline
                  W Offline
                  Waleed Eissa
                  wrote on last edited by
                  #8

                  I agree with Dave Kreskowiak, but in this case I have to put all the code in another form, for example if I have two forms, Form1 and Form2, and I want to open Form2 from within Form1, in this case I have to put all the code I have in Form2_Load in that place in Form1 where I create a new instance of Form2, this is something I don't want to do. To make things clearer, let's say I have a form on which I display data retrieved from a database, I put the code that retrieves the data in the Load event handler for that form, now what I want is that if something goes wrong while retrieving the data, the form is closed before it's even shown to the user. (a.k.a. Wal2k)
                  www.wal2k.com

                  M R 2 Replies Last reply
                  0
                  • W Waleed Eissa

                    I agree with Dave Kreskowiak, but in this case I have to put all the code in another form, for example if I have two forms, Form1 and Form2, and I want to open Form2 from within Form1, in this case I have to put all the code I have in Form2_Load in that place in Form1 where I create a new instance of Form2, this is something I don't want to do. To make things clearer, let's say I have a form on which I display data retrieved from a database, I put the code that retrieves the data in the Load event handler for that form, now what I want is that if something goes wrong while retrieving the data, the form is closed before it's even shown to the user. (a.k.a. Wal2k)
                    www.wal2k.com

                    M Offline
                    M Offline
                    methodincharge
                    wrote on last edited by
                    #9

                    There could be two approaches that I can think of that may be of use. The first would involve creating a Timer event, and if the data fails, start the Timer and allow the Load event to close and maybe a second or something liket that before firing it to close the Form. The second is to use a ShowDialog() error box inside the load before the form itself gets loaded, and upon the error window exiting, close the form. I have done the second, however using Application.Exit() so it may not work for simply closing the form. I would say the timer is the best approach. Also do you just have one form that you want to close or is a child form (havent read through the posts) because if I do this.Close() in the main or child in the Load it does close it. Maybe you have to Garbage Collect or cleanup the variables?

                    1 Reply Last reply
                    0
                    • W Waleed Eissa

                      I agree with Dave Kreskowiak, but in this case I have to put all the code in another form, for example if I have two forms, Form1 and Form2, and I want to open Form2 from within Form1, in this case I have to put all the code I have in Form2_Load in that place in Form1 where I create a new instance of Form2, this is something I don't want to do. To make things clearer, let's say I have a form on which I display data retrieved from a database, I put the code that retrieves the data in the Load event handler for that form, now what I want is that if something goes wrong while retrieving the data, the form is closed before it's even shown to the user. (a.k.a. Wal2k)
                      www.wal2k.com

                      R Offline
                      R Offline
                      Robert Rohde
                      wrote on last edited by
                      #10

                      Then put the code into a seperate public function of Form2 and call it prior to showing it (either catch the exception in Form1 or catch it in the function and return False indicating something went wrong).

                      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