[英]How to call a class method from a different class
我正在創建Windows窗體應用程序,其主窗體包含一個面板。 單擊按鈕將在該面板上加載不同的用戶控件。
namespace LearnCSharp
{
public partial class MainForm : Form
{
private void configButton_Click(object sender, EventArgs e)
{
var uControllerDashboard = new Controllers.Dashboard();
panel.Controls.Add(uControllerDashboard);
updateNotification("active");
}
private void updateNotification(string state)
{
switch (state)
{
case "active" :
//Do something here
break;
}
}
}
}
當單擊配置按鈕時 ,它將Dashboard用戶控制器加載到Panel中, Dashboard userControl中還有另一個Apply按鈕。 當我單擊該按鈕時,我需要在MainForm Class中調用updatNotification方法。
namespace LearnCSharp.Controllers
{
public partial class Dashboard : UserControl
{
private void btnApply_Click(object sender, EventArgs e)
{
// Need to call updateNotification method here.
}
}
}
我怎樣才能達到我的要求。 感謝您的幫助。 謝謝。
為此使用事件。
與其在UserControl中調用MainForm,不如在UserControl中創建一個事件並使MainForm訂閱該事件。 在UserControl內部,您只需要觸發事件即可。
網絡上有許多關於此的示例。 看看這個:
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.