简体   繁体   English

使用C#中的委托从其他用户控件更新Treeview

[英]Updating a treeview from other usercontrol using delegate in C#

In a main form I have a treeview control where each node calls a different user control in a panel present in the main form. 在主窗体中,我有一个treeview控件,其中每个节点在主窗体中的面板中调用一个不同的用户控件。 My problem is that one of these user controls is responsible to add/delete/update nodes of the treeview in the main form. 我的问题是这些用户控件之一负责在主窗体中添加/删除/更新树视图的节点。

I have tried to do it using delegates it is not working. 我试图使用它无法正常工作的代表来做到这一点。

In the main form I have a method like this: 在主要形式中,我有一个像这样的方法:

public void UpDateTreeView()
{
    //load xml file that contain my nodes
    //do something

    myTreeView.Refresh();
}

In the User Control I'm trying it: 在用户控件中,我正在尝试:

delegate void UpDatingMyTreeView();

private void buttonUpDate_Click(object sender, EventArgs e)
{
    //update the xml file with a new node
    //do something...

    frm_MainForm frm = new frm_MainForm frm();
    UpDatingMyTreeView updTrv = new UpDatingMyTreeView(frm.UpDateTreeView);
    updTrv();
}

I have never used delegates and it's not working. 我从来没有使用过代表,它没有用。 I'm using windows form application. 我正在使用Windows窗体应用程序。

The solution starts when you can get the main form of the Uzur Control. 当您可以获得Uzur控件的主要形式时,解决方案便开始了。 We use the following method: 我们使用以下方法:

yourUserControl.FindForm(); or this.FindForm();

Then we do the next projection to get the Main form of Instance . 然后,我们进行下一个投影以获得InstanceMain形式

private void buttonUpDate_Click(object sender, EventArgs e)
    {
      ((MainForm)this.FindForm()).UpDateTreeView();
    }

UpDateTreeView() is public Method in MainForm . UpDateTreeView()是MainForm中的公共方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM