简体   繁体   English

通过退出表单内的表单来启用禁用的按钮

[英]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. StartMenu表单具有button2-单击按钮2时,它将弹出一个新表单并被禁用。 Now new form (InchMm conversion form) is up and when I am done with that form I hit the X button. 现在启动了新表格(InchMm转换表格),当我完成该表格后,我按下了X按钮。 When this is done I want Button2 from StartMenu form to enable. 完成此操作后,我希望从StartMenu窗体启用Button2。 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 InchMm转换表格

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) . InchMm_Conversion()构造函数修改为InchMm_Conversion(StartMenuForm) So you can pass the 'parent' dialog to InchMm_Conversion in construction. 因此,您可以在构造中将“父”对话框传递给InchMm_Conversion。 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) ReferenceToStartMenuForm.enableB() )(我知道有更优雅的解决方案,但这很容易实现)

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: 因为您在子窗体中使用了IsMdiContainerMDIParent (奇怪的是您在单击按钮时设置了MDI容器),所以您可以像这样访问父窗体:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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