使用树视图,在树视图节点上单击事件

发布于 2024-10-27 20:00:51 字数 1226 浏览 0 评论 0原文

我试图在单击树视图的子节点或父节点时打开表单:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    TreeNode head = new TreeNode("HEAD");

    TreeNode member = new TreeNode("MEMBER ");

    TreeNode submember = new TreeNode("SUB-MEMBER");

    private void Form1_Load(object sender, EventArgs e)
    {
        head.Nodes.Add(member);
        member.Nodes.Add(submember);

        treeView1.Nodes.Add(head);
        treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);

    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
           if (treeView1.SelectedNode == member)
              {
                  MemberForm mf = new MemberForm();
                  mf.ShowDialog(); 
              }

           if (treeView1.SelectedNode == head)
              {
                  HeadForm hf = new HeadForm();
                  hf.ShowDialog(); 
              }

           if (treeView1.SelectedNode == submember)
              {
                  SubMemberForm sf = new SubMemberForm();  //is this way of checking that which node is clicked efficient???
                  sf.ShowDialog(); 
              }
    }

}

I am trying to open a Form when the child node or parent node of a treeview is clicked :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    TreeNode head = new TreeNode("HEAD");

    TreeNode member = new TreeNode("MEMBER ");

    TreeNode submember = new TreeNode("SUB-MEMBER");

    private void Form1_Load(object sender, EventArgs e)
    {
        head.Nodes.Add(member);
        member.Nodes.Add(submember);

        treeView1.Nodes.Add(head);
        treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);

    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
           if (treeView1.SelectedNode == member)
              {
                  MemberForm mf = new MemberForm();
                  mf.ShowDialog(); 
              }

           if (treeView1.SelectedNode == head)
              {
                  HeadForm hf = new HeadForm();
                  hf.ShowDialog(); 
              }

           if (treeView1.SelectedNode == submember)
              {
                  SubMemberForm sf = new SubMemberForm();  //is this way of checking that which node is clicked efficient???
                  sf.ShowDialog(); 
              }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕头说它不想醒 2024-11-03 20:00:51

只要树视图中只有三个节点,这可能会很有效。但是,这需要您为添加的每个新节点编写额外的 if 语句。如果您尝试区分节点深度,最好使用 Level 属性。

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
       if (treeView1.SelectedNode.Level == 0)
          {
              HeadForm hf = new HeadForm();
              hf.ShowDialog(); 
          }
       else if (treeView1.SelectedNode.Level == 1)
          {
              MemberForm mf = new MemberForm();
              mf.ShowDialog(); 
          }

       else if (treeView1.SelectedNode.Level == 2)
          {
              SubMemberForm sf = new SubMemberForm();
              sf.ShowDialog(); 
          }
}

As long as you have only three nodes in your treeview, this might be efficient. However this would require you to write an extra if statement for each new node you add. If you're trying to differentiate on node depth you are better off using the Level property.

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
       if (treeView1.SelectedNode.Level == 0)
          {
              HeadForm hf = new HeadForm();
              hf.ShowDialog(); 
          }
       else if (treeView1.SelectedNode.Level == 1)
          {
              MemberForm mf = new MemberForm();
              mf.ShowDialog(); 
          }

       else if (treeView1.SelectedNode.Level == 2)
          {
              SubMemberForm sf = new SubMemberForm();
              sf.ShowDialog(); 
          }
}
梅倚清风 2024-11-03 20:00:51

这可能有点黑客和滥用标签功能,但你可以这样做:

TreeNode Head = new TreeNode("Head");
Head.Tag = typeof(HeadForm);

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
  Form toOpen = Activator.CreateInstance((Type)treeView1.SelectedNode.Tag) as Form;

  if(toOpen != null)
    toOpen.ShowDialog();
}

This may be slightly hackish and abusive of the tag feature but you could do something like this:

TreeNode Head = new TreeNode("Head");
Head.Tag = typeof(HeadForm);

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
  Form toOpen = Activator.CreateInstance((Type)treeView1.SelectedNode.Tag) as Form;

  if(toOpen != null)
    toOpen.ShowDialog();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文