从另一个表单添加子节点 [英] add child node from another form

查看:72
本文介绍了从另一个表单添加子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一种形式将子节点添加到treeview的父节点。

我尝试了这段代码,这是执行但没有添加子节点。

tree视图是在窗体1中。我想在窗体2中添加子节点,在按钮ckick事件中添加文本框值。我确实喜欢这个

  foreach (表格f  in  Application.OpenForms)
{
if (f.Name == form1
f.controls.contains(treeview1.Nodes [ 市场] Nodes.Add(Textbox1.text));
}







你可以告诉我这个问题吗

解决方案

尝试类似下面的内容。如果每个打开的表单上只有一个treeView,这段代码将正常工作。



我的逻辑不难猜测。我正在迭代属于子窗体的所有控件,如果我找到TreeView控件,我正在更新市场节点。







  foreach (表格f 应用程序中。 OpenForms)
{
if (f.Name == form1
{
foreach (Control ct in f.Controls)
{
TreeView treeViewToUpdate = ct as TreeView;
if (treeViewToUpdate!= null
treeViewToUpdate.Nodes [ 市场]。Nodes.Add(Textbox1.text));
}
}

}


OP正在创建form1的新实例原始版本永远不会更新。



使用 Application.OpenForms


例如

  foreach (表格f   Application.OpenForms)
{
if (f.Name == form1
// 做点什么
}


I want to add child node to parent node of treeview from another form.
I tried this code, this is executing but not adding a child node.
tree view is in form 1. I want to add child node from form 2, textbox value in button ckick event. I did like this

foreach (Form f in Application.OpenForms)
{
    if (f.Name == "form1")
f.controls.contains(treeview1.Nodes["Market"].Nodes.Add(Textbox1.text));
}




can u let me know the problem

解决方案

Try something like below. This code will work properly if there is no more than one treeView located on each open form.

My logic is not difficult to guess. I'm iterating all controls belonging to child form and if I find TreeView control I'm updating "Market" node.



foreach (Form f in Application.OpenForms)
  {
      if (f.Name == "form1")
      {
          foreach (Control ct in f.Controls)
          {
              TreeView treeViewToUpdate = ct as TreeView;
              if (treeViewToUpdate !=null)
                  treeViewToUpdate.Nodes["Market"].Nodes.Add(Textbox1.text));
          }
      }

  }


OP is creating a new instance of form1 so the original never gets updated.

Find the current instance that you have loaded by using Application.OpenForms

E.g.

foreach (Form f in Application.OpenForms)
{
    if (f.Name == "form1")
        //Do something
}


这篇关于从另一个表单添加子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆