2009-02-25 47 views
0

我想从C#程序中的主窗体中的事件处理程序中打开一个新窗体。事件处理程序被正确调用,并打开新窗体,但新窗体被冻结,甚至不会最初填充。从主窗体中的事件处理程序中打开新窗体挂起应用程序

我可以在主窗体上创建一个按钮,并在按钮被点击后创建新窗体,但是当它从事件处理程序完成时它不能正常工作。

事件处理程序不需要知道在它创建的窗体上完成的任何事情的结果 - 它只需要创建它并避开它。

我需要做什么?新表格需要独立于主表格进行操作。

这里就是我定义的事件处理程序:

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

这里是事件处理代码:

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

创建通过点击一个按钮的新形式工作正常:

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

请在您创建并显示新表单的位置添加您的代码。 – VBNight 2009-02-25 15:56:34

回答

2

你可以发布事件处理程序代码吗?也是事件在单独的线程中引发,然后是主要的UI?

编辑:
不知道实时的酒吧做什么,但尝试检查一个invokerequired表单上的,所以你可以创建同一个线程的主界面上的辅助形式..

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

中当然这使用一些脏快捷方式,并假设3.5,但你可以修改你的需要。

另外我将FancyForm删除移到了方法范围之外..再次根据您的需求进行调整。

+0

不错的一个,Quintin – 2009-02-27 11:56:52

0

你知道哪个线程在事件处理程序中运行吗?我相信它可能必须是主要的GUI线程才能工作。

+0

没有可能。它一定要是。 – Quibblesome 2009-02-25 16:07:04

2

我有完全相同的问题,我用金廷的代码和现在的工作罚款....

我做了一些改变,以便与框架2.0的工作,这里是我做过什么: 首先,我创建委托,在打开的形式的方法指向:

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

打开形式的方法是相同的一个由提供廷:

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

而在我的程序的事件处理程序中,y做了一些简单的修改:

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

而且它现在效果很好。 Invoke方法执行适当的委托,所以现在在主UI下创建表单。

希望它的作品,并感谢很多Quintin!

相关问题