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 form when closing child form

Disable form when closing child form

Scheduled Pinned Locked Moved C#
helpquestioncsharp
3 Posts 3 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.
  • D Offline
    D Offline
    Deresen
    wrote on last edited by
    #1

    Hello everyone, I have an question about forms in C#. This is a problem which I have had since the start of programming in C#. This is the problem: In the (main) Form there is a button to open another (child) Form. But when I use that Form I would like to have the main Form to be enabled. This is no problem. But when I close the child Form I would like to disable the main Form. But I want the main form to do this. So not something like this:

    //Form main:
    public void createForm(){
    Child child = new Child(this);
    child.show();
    this.Enabled = true;
    }
    public void show(){
    this.Enabled = false;
    }
    //Form child
    public Child(Main m){
    this.main = m;
    }
    public void Dispose(){
    main.show();
    }

    I would rather like to have something like this:

    //Form main
    public void createForm(){
    Child child = new Child();
    child.show();
    this.Enabled = true;
    }
    public void child_Disposed(object sender, EventArgs e){
    this.Enabled = false;
    }

    If I'm not wrong, I'll have to work with delegates. But I don't know how, can somebody help me?

    H N 2 Replies Last reply
    0
    • D Deresen

      Hello everyone, I have an question about forms in C#. This is a problem which I have had since the start of programming in C#. This is the problem: In the (main) Form there is a button to open another (child) Form. But when I use that Form I would like to have the main Form to be enabled. This is no problem. But when I close the child Form I would like to disable the main Form. But I want the main form to do this. So not something like this:

      //Form main:
      public void createForm(){
      Child child = new Child(this);
      child.show();
      this.Enabled = true;
      }
      public void show(){
      this.Enabled = false;
      }
      //Form child
      public Child(Main m){
      this.main = m;
      }
      public void Dispose(){
      main.show();
      }

      I would rather like to have something like this:

      //Form main
      public void createForm(){
      Child child = new Child();
      child.show();
      this.Enabled = true;
      }
      public void child_Disposed(object sender, EventArgs e){
      this.Enabled = false;
      }

      If I'm not wrong, I'll have to work with delegates. But I don't know how, can somebody help me?

      H Offline
      H Offline
      Harvey Saayman
      wrote on last edited by
      #2

      you might be able to catch the childs onFormClosing event on the parent and the disable the parent in the handler... read my article on Delegates And Events[^]

      Harvey Saayman - South Africa Junior Developer .Net, C#, SQL

      you.suck = (you.passion != Programming)

      1 Reply Last reply
      0
      • D Deresen

        Hello everyone, I have an question about forms in C#. This is a problem which I have had since the start of programming in C#. This is the problem: In the (main) Form there is a button to open another (child) Form. But when I use that Form I would like to have the main Form to be enabled. This is no problem. But when I close the child Form I would like to disable the main Form. But I want the main form to do this. So not something like this:

        //Form main:
        public void createForm(){
        Child child = new Child(this);
        child.show();
        this.Enabled = true;
        }
        public void show(){
        this.Enabled = false;
        }
        //Form child
        public Child(Main m){
        this.main = m;
        }
        public void Dispose(){
        main.show();
        }

        I would rather like to have something like this:

        //Form main
        public void createForm(){
        Child child = new Child();
        child.show();
        this.Enabled = true;
        }
        public void child_Disposed(object sender, EventArgs e){
        this.Enabled = false;
        }

        If I'm not wrong, I'll have to work with delegates. But I don't know how, can somebody help me?

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        Your second code looks OK, only thing is you haven't hooked the Disposed event.

        //Form main
        public void createForm(){
        Child child = new Child();
        child.Disposed += this.child_Disposed;
        child.show();
        this.Enabled = true;
        }

        private void child_Disposed(object sender, EventArgs e){
        this.Enabled = false;
        }

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        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