Changing opacity of one form through another

Asked

Viewed 227 times

3

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

Close button on the 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 not close the system in the form "close" the opacity of the main form does not return to this.Opacity = 1;

Button to not close the program:

public void btFecharNAO_Click(object sender, EventArgs e)
    {
        principal p = new principal();
        p.Opacity = 1;
        this.Close();
    }

2 answers

5

The purpose of a Dialog is to get an answer from the user. User responds by choosing one of the buttons available on it.

The method Showdialog() returns a value of type Dialogresult the purpose of which is to indicate the answer (button) chosen.

The value returned is the property Dialogresult of dialog.

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

Main 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 method btFecharNAO_Click() can, in this situation, be avoided by using the property Dialogresult button.

When a button has its property DialogResult with a value other than DialogResult.None and belongs to a Form method-opened ShowDialog(), when clicked will close the Form no need to use the respective event, at the same time the property DialogResult of Form receives the value assigned to the property DialogResult button.

See in the documentation of Dialogresult Enumeration what other values you can assign to DialogResult.

3


Pass to tela principal as a parameter for the 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 parameter:

private void btFechar_Click(object sender, EventArgs e)
{
    this.Opacity = .75;
    fechar f = new fechar(this); //passe this
    f.ShowDialog();
    f.Dispose();
}
  • Worked perfectly!

  • If it worked, mark it as the right answer. :)

  • 1

    In the close constructor you can set frmParent.Opacity = .75, so automatically when you call the fechar, will change the screen. If you have a project with multiple screens this will be useful.

  • 1

    The @ramaral response makes more sense.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.