代码之家  ›  专栏  ›  技术社区  ›  Fabricio Leinat

在C中使用委托从其他usercontrol更新treeview#

  •  0
  • Fabricio Leinat  · 技术社区  · 7 年前

    在主窗体中,我有一个treeview控件,其中每个节点在主窗体中的面板中调用不同的用户控件。我的问题是,其中一个用户控件负责在主窗体中添加/删除/更新treeview的节点。

    我试着用代表来做这件事,但它不起作用。

    在主要形式中,我有这样一种方法:

    public void UpDateTreeView()
    {
        //load xml file that contain my nodes
        //do something
    
        myTreeView.Refresh();
    }
    

    在用户控件中,我正在尝试:

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

    我从未使用过代理,而且它不起作用。我正在使用windows窗体应用程序。

    1 回复  |  直到 7 年前
        1
  •  0
  •   محمد النعيمي    7 年前

    当您可以获得Uzur控件的主要形式时,解决方案就开始了。 我们使用以下方法:

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

    然后我们做下一个投影,得到 主窗体 属于 例子 .

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

    UpDateTreeView()是MainForm中的公共方法。