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(); }
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..
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.