Changing opacity of one form through another

3

I need to change the opacity of the main form every time the user clicks the close button. When it clicks on this button the opacity stays in this.Opacity = .75; and opens a new form asking if it wants to close the program.

Close button on main form:

private void btFechar_Click(object sender, EventArgs e)
    {
        this.Opacity = .75;
        fechar f = new fechar();
        f.ShowDialog();
        f.Dispose();
    }

The problem is that when the user clicks to close the system in the form "close" the opacity of the main form does not return to this.Opacity = 1;

Do not close the program:

public void btFecharNAO_Click(object sender, EventArgs e)
    {
        principal p = new principal();
        p.Opacity = 1;
        this.Close();
    }
    
asked by anonymous 03.01.2017 / 14:07

2 answers

3

Pass tela principal as a parameter to form fechar , in the builder call. Something similar to this:

public partial class fechar : Form
{
    private Form mFormParent = null;
    public fechar(Form frmParent){
          this.mFormParent  = frmParent;
    }

    //seu código

    public void btFecharNAO_Click(object sender, EventArgs e)
    {
       if (mFormParent != null)
           mFormParent.Opacity = 1;
       this.Close();
    }
}

And in the call just pass the form itself as a parameter:

private void btFechar_Click(object sender, EventArgs e)
{
    this.Opacity = .75;
    fechar f = new fechar(this); //passe this
    f.ShowDialog();
    f.Dispose();
}
    
03.01.2017 / 14:23
5

The purpose of a Dialog is to get a response from the user. The user responds by choosing one of the available buttons.

The ShowDialog () method returns a value of type DialogResult whose purpose is to indicate the response (button).

The value returned is that of the DialogResult of dialog .

Use the value returned to know which button was clicked and, if it is DialogResult.No , put the opacity to 1.

Parent Form:

private void btFechar_Click(object sender, EventArgs e)
{
    this.Opacity = .75;
    fechar f = new fechar();
    if(f.ShowDialog(this) == DialogResult.No)
    {
        this.Opacity = 1;
    }
    f.Dispose();
}

Dialog:

public void btFecharNAO_Click(object sender, EventArgs e)
{
    DialogResult = DialogResult.No;
    this.Close();
}

The btFecharNAO_Click() method can, in this situation, be avoided if you use the DialogResult button.

When a button has its DialogResult property with a value other than DialogResult.None and belongs to a Form opened by the ShowDialog() method, when clicked it will close the Form without needing to use its event, at the same time the DialogResult of the Form property gets the value assigned to the DialogResult property of the button.

See the documentation for DialogResult Enumeration a> other values can assign to DialogResult .

    
03.01.2017 / 14:52