Skip to main content
 首页 » 编程设计

c#之委托(delegate)和事件

2024年02月20日14tuyile006

我有 2 个表格。

表格 1:

public partial class Panel1 
    { 
        public void ShowExport(object sender, EventArgs e) 
        { 
           ....... 
        } 
    } 

表格 2:

public partial class Panel2 
{ 
        public delegate void ShowExportReport(object sender, EventArgs e); 
        public event ShowExportReport ShowExportClicked; 
 
private void buttonExport_Click(object sender, RoutedEventArgs e) 
        {          
           if (ShowExportClicked != null) 
            { 
                ShowExportClicked(sender, new EventArgs()); 
            }                      
 
        } 
} 

当我点击按钮时——

button.Click = buttonExport_Click 

如何从 Panel2.buttonExport_Click 调用 Panel1.ShowExport()?

请您参考如下方法:

在 Panel1 中你必须订阅事件:

pnl2.ShowExportClicked += new ShowExportReport(ShowExport);