简体   繁体   中英

enable a disabled button by exiting off a form inside a form

OK This is what I am doing.

StartMenu form has button2- When button 2 is clicked it brings up a new form and gets disabled. Now new form (InchMm conversion form) is up and when I am done with that form I hit the X button. When this is done I want Button2 from StartMenu form to enable. My code below.

StartMenu

 private void button2_Click(object sender, EventArgs e)
    {
        this.IsMdiContainer = true;
        InchMm_Conversion f = new InchMm_Conversion();
        f.MdiParent = this;
        f.Show();
        button2.Enabled = false;
    }


    private void button3_Click(object sender, EventArgs e)
    {
        this.LayoutMdi(MdiLayout.ArrangeIcons);
    }

    public void enableB() 
    {
        button2.Enabled = true;
    }

InchMm Conversion form

private void InchMm_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
    {
        StartMenu.enableB();

    }

Now I am getting a error that says a object reference is required for the non-static field, method, or property. Now I know this should be its right in form of my face but I am still learning alot and I think I am close.

I would try this

private void button2_Click(object sender, EventArgs e)
{
        this.IsMdiContainer = true;
        InchMm_Conversion f = new InchMm_Conversion();
        f.MdiParent = this;
//Here you set an event. When the form closes the here specified method is called 
        f.FormClosed += f_FormClosed; 
        f.Show();
        button2.Enabled = false;
}


private void button3_Click(object sender, EventArgs e)
{
    this.LayoutMdi(MdiLayout.ArrangeIcons);
}       

//This method is executed when the form is closed
    void f_FormClosed(object sender, FormClosedEventArgs e)
    {
           button2.Enabled = true;
    }

Modify your InchMm_Conversion() constructor to InchMm_Conversion(StartMenuForm) . So you can pass the 'parent' dialog to InchMm_Conversion in construction. Hold a reference in a private field and you can access it when you close the child form. ( ReferenceToStartMenuForm.enableB() ) (I know there are more elegant solutions, but this is quite easy to implement)

InchMm_Conversion(StartMenuForm form)
{
    myForm = form;    
}

private void InchMm_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
{
    myForm.enableB();

}
private StartMenuForm myForm;

Beside other answers, which are correct, I want to suggest other solution. Because you using IsMdiContainer and MDIParent (by the way its strange that you setting MDI Container on button click) in your child form you can access parent form like this:

private void InchMm_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
{
    StartMenu form = this.MDIParent as StartMenu;
    if(form != null)
       form.enableB();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM