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. Form can't be closed

Form can't be closed

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

    Hi, I'm adding some controls on Panel at runtime. This works fine, but after removing this controls, Application can't be closed (and Closing event isn't called). :omg: Creating controls:

    MyControl ctrl=new MyControl(); // MyControl is derived from UserControl
    ar.Add(ctrl);
    panel1.Controls.Add(ctrl);

    Removing all controls:

    panel1.Controls.Clear();
    foreach(MyControl ctrl in ar) ctrl.Dispose();
    ar.Clear();

    When I'm not removing controls it works :confused: i'm only pointer to myself

    L R J 3 Replies Last reply
    0
    • T Tomas Petricek

      Hi, I'm adding some controls on Panel at runtime. This works fine, but after removing this controls, Application can't be closed (and Closing event isn't called). :omg: Creating controls:

      MyControl ctrl=new MyControl(); // MyControl is derived from UserControl
      ar.Add(ctrl);
      panel1.Controls.Add(ctrl);

      Removing all controls:

      panel1.Controls.Clear();
      foreach(MyControl ctrl in ar) ctrl.Dispose();
      ar.Clear();

      When I'm not removing controls it works :confused: i'm only pointer to myself

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Tomas Petricek wrote: MyControl ctrl=new MyControl(); // MyControl is derived from UserControl ar.Add(ctrl); panel1.Controls.Add(ctrl); You are duplicating code..... Tomas Petricek wrote: panel1.Controls.Clear(); foreach(MyControl ctrl in ar) ctrl.Dispose(); ar.Clear(); It seems the GC might be invoked too early, therefore modifying the collection while enumerating thru it, which is illegal. Rather use a while loop:

      while (Controls.Count > 0)
      {
      Control ctrl = Controls[0];
      Controls.Remove(ctrl);
      ctrl.Dispose();
      }

      Hope this helps :) Who is this miscrosoft, and what devilish plans have they for us?

      1 Reply Last reply
      0
      • T Tomas Petricek

        Hi, I'm adding some controls on Panel at runtime. This works fine, but after removing this controls, Application can't be closed (and Closing event isn't called). :omg: Creating controls:

        MyControl ctrl=new MyControl(); // MyControl is derived from UserControl
        ar.Add(ctrl);
        panel1.Controls.Add(ctrl);

        Removing all controls:

        panel1.Controls.Clear();
        foreach(MyControl ctrl in ar) ctrl.Dispose();
        ar.Clear();

        When I'm not removing controls it works :confused: i'm only pointer to myself

        R Offline
        R Offline
        Ray Cassick
        wrote on last edited by
        #3

        I heard somewhere that this can happen if controls are hidden.


        Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall."
        George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things."


        1 Reply Last reply
        0
        • T Tomas Petricek

          Hi, I'm adding some controls on Panel at runtime. This works fine, but after removing this controls, Application can't be closed (and Closing event isn't called). :omg: Creating controls:

          MyControl ctrl=new MyControl(); // MyControl is derived from UserControl
          ar.Add(ctrl);
          panel1.Controls.Add(ctrl);

          Removing all controls:

          panel1.Controls.Clear();
          foreach(MyControl ctrl in ar) ctrl.Dispose();
          ar.Clear();

          When I'm not removing controls it works :confused: i'm only pointer to myself

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          There is a bug where a Form can't be closed if you remove a control that has the current focus. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

          T 1 Reply Last reply
          0
          • J James T Johnson

            There is a bug where a Form can't be closed if you remove a control that has the current focus. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

            T Offline
            T Offline
            Tomas Petricek
            wrote on last edited by
            #5

            Thank you ! Setting focus to another control solved my problem. :) i'm only pointer to myself

            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