How to get the current node selected in a TreeView control?

When I use the event MouseClickof a treeview control, the first node in the tree is always selected. What to do to get the current node selected by the user? I am using C #.

This is the code I'm currently using:

private void TVRecorder_MouseClick(object sender, MouseEventArgs e)
{
    TreeNode selectedNode = TVRecorder.HitTest(e.Location).Node;
    if (selectedNode != null)
    {
        if (selectedNode.SelectedImageKey == "Test_Space")
        {
            frmRepository rep = new frmRepository();
            string ssql = string.Empty;
            rep.label1.Text = "Scenario-RepositoryDetails";
            rep.LoadAppSettings();
            SqlConnection con4 = new SqlConnection();

            con4.ConnectionString = "Data Source=" + rep.ScnServer + ";" + "initial catalog=" + rep.ScnDbName + ";" + "User Id=" + rep.ScnUserName + ";" + "Password=" + rep.ScnPwd;
            try
            {
                con4.Open();
            }
            catch
            {
                MessageBox.Show("Connection Failed");
            }
            ssql = "scn_select_testplan_sp";
            SqlCommand cmd = new SqlCommand(ssql, con4);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@testspace_key", SqlDbType.Int);
            cmd.Parameters["@testspace_key"].Value = testspace_key;
            SqlDataReader _datareader = cmd.ExecuteReader();
            try
            {
                while (_datareader.Read())
                {
                    testplan_key = (int)_datareader["testplan_key"];
                    testplan_desc = (string)_datareader["testplan_desc"];
                    //selectedNode.Nodes[0].Nodes.Add(Convert.ToString(testplan_key), testplan_desc, "P", "Test_Plan");
                    TVRecorder.Nodes[0].Nodes.Add(Convert.ToString(testplan_key), testplan_desc, "P", "Test_Plan");
                    TVRecorder.Visible = true;
                    TVRecorder.HideSelection = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

Thanks in advance...

+3
source share
1 answer

You can use the method displayed by the control in combination with the location of the mouse cursor when the event ( ) occurred , to determine which node was pressed (and therefore they will be selected at the end of the event). For instance: HitTestTreeViewMouseClick e.Location

private void TreeView_MouseClick(object sender, MouseEventArgs e)
{
    // Get the node that was clicked
    TreeNode selectedNode = myTreeView.HitTest(e.Location).Node;

    if (selectedNode != null)
    {
        // ...
        // Do something with the selected node here...
    }
}

, , selectedNode null, - . , node, selectedNode, HitTest, null.

, , selectedNode TreeView. node , MouseClick - . node, .


:

. :

TVRecorder.Nodes[0].Nodes.Add(Convert.ToString(testplan_key), testplan_desc, "P", "Test_Plan");

node, TreeView. , node. , node (. ), , node, :

selectedNode.Nodes.Add(Convert.ToString(testplan_key), testplan_desc, "P", "Test_Plan");

, node ( TreeNode ) Nodes , TreeNodeCollection. node. MSDN, , , :

Nodes TreeNode. node [s] Nodes, TreeNodeCollection. . FullPath .

+5

Source: https://habr.com/ru/post/1783167/


All Articles