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