简体   繁体   中英

how to determine between 2 mouse clicks of treeviews

I have two treeviews and a contexmenucontainer with the adding node function cmnuAddNode is the add tab of my contexmenuestrip I'm a newbie working with events

I want to add the node to treeview 1 if clicked and add the node to treeview2 if clicked I just want to know what I should write in my if condition my code is as below:

 private void cmnuAddNode_Click(object sender, EventArgs e)
    {

        NewNode n = new NewNode();
        n.ShowDialog();
        TreeNode nod = new TreeNode();
        nod.Name = n.NewNodeName.ToString();
        nod.Text = n.NewNodeText.ToString();

        n.Close();
      if (treeView1.SelectedNode!=null)
        {
            treeView1.SelectedNode.Nodes.Add(nod);
            treeView1.SelectedNode.ExpandAll();
        }
        if (treeView2.SelectedNode!= null)
        {
            treeView2.SelectedNode.Nodes.Add(nod);
            treeView2.SelectedNode.ExpandAll();
        }
          }

this code has exception when I click the treeview2

private void cmnuAddNode_Click(object sender, EventArgs e,TreeViewEventArgs e1)
{

    NewNode n = new NewNode();
    n.ShowDialog();
    TreeNode nod = new TreeNode();
    nod.Name = n.NewNodeName.ToString();
    nod.Text = n.NewNodeText.ToString();

    n.Close();
    if(e1.Node.TreeView == treeView1)
   {
    treeView1.SelectedNode.Nodes.Add(nod);
    treeView1.SelectedNode.ExpandAll();
    }
     if(e1.Node.TreeView == treeView2)
   {
    treeView2.SelectedNode.Nodes.Add(nod);
    treeView2.SelectedNode.ExpandAll();
     }
}

first add a mouse click event for each TreeView secondly select the correct node using the MouseEventArgs. for treeview1 (do the same for treeview2)

    void treeView1MouseUp(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Right)
        {
            // Select the clicked node
            treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

            if(treeView1.SelectedNode != null)
            {
             cmnuAddNode.Show(treeView1, e.Location);
            }
        }
    }

//it will work for the two treeview getting Parent control of cmnuAddNode
private void cmnuAddNode_Click(object sender, EventArgs e,TreeViewEventArgs e1)
    {
        TreeView yourtreeView = (TreeView)cmnuAddNode.Parent;
        NewNode n = new NewNode();
        n.ShowDialog();
        TreeNode nod = new TreeNode();
        nod.Name = n.NewNodeName.ToString();
        nod.Text = n.NewNodeText.ToString();

        n.Close();

        yourtreeView.SelectedNode.Nodes.Add(nod);
        yourtreeView.SelectedNode.ExpandAll();

    }

simple I got an index for my treeviews when it goes in treeview1 in event mouseclick of it I set it to 1 and when goes to 2 I set it to 2 so in my function add I wrote conditions this was simple and fast and easy

private void cmnuAddNode_Click(object sender, EventArgs e)
    {

        NewNode n = new NewNode();
        n.ShowDialog();
        TreeNode nod = new TreeNode();
        nod.Name = n.NewNodeName.ToString();
        nod.Text = n.NewNodeText.ToString();

        n.Close();

        if (treeviewindex== 1)
        {
            treeView1.SelectedNode.Nodes.Add(nod);
            treeView1.SelectedNode.ExpandAll();

        }
        if (treeviewindex==2)
        {
            treeView2.SelectedNode.Nodes.Add(nod);
            treeView2.SelectedNode.ExpandAll();
        }
        if (treeviewindex == 3)
        {
            treeView3.SelectedNode.Nodes.Add(nod);
            treeView3.SelectedNode.ExpandAll();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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