Ignacio Gómez
Ignacio Gómez

Reputation: 1637

Can't Close form from event

My main Form, named Form1 has a class with an event which indicates that Form1 should be Closed.

When I receive the event and try to Close I get the excepcion that a control.invoke must be used. Fine, I make the call with this code and I'm still getting the same exception:

void Prox_OkEvent(object sender, EventArgs e)
{
    if (this.InvokeRequired)
    {
        callControlhandler c = new callControlhandler(CloseForm);
        c.Invoke();
    }
    else Close();
}

private void CloseForm()
{
    try { this.Close(); }
    catch (Exception e) { MessageBox.Show(e.Message); }
}

Where is the problem? thanks for any help

Upvotes: 0

Views: 82

Answers (1)

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44295

Invoke should be called against the Form itself. In this case it is called against c.

Try...

        if (this.InvokeRequired)
        {
            this.Invoke(new Action(CerrarForm));
        }

Upvotes: 4

Related Questions