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. showing a form twice

showing a form twice

Scheduled Pinned Locked Moved Windows Forms
helpquestion
4 Posts 4 Posters 4 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.
  • G Offline
    G Offline
    godzooky
    wrote on last edited by
    #1

    i have a stupid problem. i show my form, close it and try to show it again, but i get an error saying i'm trying to access a disposed object. how can i prevent that? i must be missing something obvious. thanks.

    P S C 3 Replies Last reply
    0
    • G godzooky

      i have a stupid problem. i show my form, close it and try to show it again, but i get an error saying i'm trying to access a disposed object. how can i prevent that? i must be missing something obvious. thanks.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You're trying to reuse a disposed reference. This occurs because you have created an object which has subsequently been disposed. What you need to do is something like the following:

      private void CreateMyForm()
      {
        using (MyForm myForm = new MyForm()) 
        {
          myForm.Show();
        }
      }
      

      Then call CreateMyForm whenever you want to create an instance of this form.

      Deja View - the feeling that you've seen this post before.

      1 Reply Last reply
      0
      • G godzooky

        i have a stupid problem. i show my form, close it and try to show it again, but i get an error saying i'm trying to access a disposed object. how can i prevent that? i must be missing something obvious. thanks.

        S Offline
        S Offline
        sthotakura
        wrote on last edited by
        #3

        You can use GC::KeepAlive(yourForm)

        1 Reply Last reply
        0
        • G godzooky

          i have a stupid problem. i show my form, close it and try to show it again, but i get an error saying i'm trying to access a disposed object. how can i prevent that? i must be missing something obvious. thanks.

          C Offline
          C Offline
          CompMan44
          wrote on last edited by
          #4

          I've had a similar problem. My solution was to catch the FormClosing event, and when e.CloseReason == CloseReason.UserClosing, set e.Cancel to true and Hide the form.

          A little learning is a dangerous thing; Drink deep, or taste not, the Pierian Spring. —Alexander Pope

          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