tags:

views:

196

answers:

3

I'm trying to open a new form from within an event handler in the main form in a C# program. The event handler gets called properly and it opens the new form, but the new form is frozen and doesn't even initially populate.

I can create a button on the main form and have the new form created after the button is clicked, but it is not working properly when done from the event handler.

The event handler doesn't need to know the results of anything done on the form it creates - it just needs to create it and get out of the way.

What do I need to do? The new form needs to operate independently of the main form.

Here's where I define the event handler:

ibclient.RealTimeBar += new EventHandler<RealTimeBarEventArgs>(ibclient_RealTimeBar);

Here's the event handler code:

void ibclient_RealTimeBar(object sender, RealTimeBarEventArgs e)
{
    FancyForm a_fancy_form = new FancyForm();
    a_fancy_form.Show();
}

Creating a new form via a button click works fine:

private void button7_Click(object sender, EventArgs e)
{
    FancyForm a_fancy_form = new FancyForm();
    a_fancy_form.Show();
}
+2  A: 

Can you post the event handler code?.. Also is the Event being raised in a seperate thread then the main ui?

Edit:
Not sure what the realtime bar does, but try checking for an invokerequired on your form so you can create the secondary form on the same thread as the main UI..

void ibclient_RealTimeBar(object sender, RealTimeBarEventArgs e)
{
    if(this.InvokeRequired)
    {
        this.Invoke(((Action)() => ShowFancyForm()));
    }

    ShowFancyForm();
}

FancyForm a_fancy_form;
private void ShowFancyForm()
{
    if(null != a_fancy_form)return;

    a_fancy_form = new FancyForm();
    a_fancy_form.Show();
}

Of course this uses some dirty shortcuts and assumes 3.5 but you can modify to your needs.

Also I moved the FancyForm decleration outside of the scope of the method.. again adjust to your needs.

Quintin Robinson
Nice one, Quintin
Tor Haugen
A: 

Do you know which thread is running in the event handler? I believe it might have to be the main GUI thread to work.

Tor Haugen
There is no might. It has to be.
Quibblesome
A: 

Hi there!!

i had the exactly same problem, i used Quintin's code and it works fine now....

I made some changes in order to work with the framework 2.0, here is what i did: First, i created a delegate, pointing at the method that opens the form:

public delegate void pantallazo();
pantallazo obj=new pantallazo(this.ShowFancyForm);

The method that opens the form is the same one provided by Quintin:

smatiCliente a_fancy_form;   //smatiCliente  is the name of my new form class...
    private void ShowFancyForm()
    {
        if (null != a_fancy_form) return;
        a_fancy_form = new smatiCliente();
        this.Hide();
        a_fancy_form.Show();
    }

And inside my programm's event handler, y made some simple changes:

 if(this.InvokeRequired)
                    {
                        this.Invoke(obj);
                    }
                    ShowFancyForm();

And thats it, it works great now. The Invoke method executes the appropiate delegate, so the form is now created under the main UI.

Hope it works, and thanks a lot Quintin!