Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i was trying it using treeview.select() or treeview.Focus() command.
but using this cursor focusing on by default first node in treeview.
but i want to focus on newly created node in treeview.
Posted
Updated 30-Mar-21 5:44am

You use the selected node property of the treeview, for example if your treeview is called treeView1:

C#
treeView1.SelectedNode = treeNode;


You can also call treeNode.EnsureVisible() to make sure that the node is visible in the treeview.

Edit:

Here is your revised solution using the code from your comment:

C#
TreeNode newNode = treeViewDept.Nodes.Add(textBoxNewParent.Text);
btnSave.Enabled = true;
treeViewDept.SelectedNode = newNode;
newNode.EnsureVisible();


Notice how I save the new node when it is created? This is so it can be referenced again later when I set the SelectedNode and use EnsureVisible.
 
Share this answer
 
v2
Comments
Gauri Bharde 19-Dec-13 1:23am    
treeViewDept.Nodes.Add(textBoxNewParent.Text);
btnSave.Enabled = true;
btnSave.Focus();
treeViewDept.Focus();


actually this is my code.I want to create new node through textbox.
when click on create new node button,new node added into treeview.but i want to focus on that node bydefault without selecting that node.
i am trying to treeview1.selectednode= textBoxNewParent.Text;
but there is error occurs.how to solve this problem?
Ron Beyer 19-Dec-13 9:57am    
I've updated my answer to include your code, modified to work.
To focus on the TreeView and hide any selection while visually indicating to the user that a TreeNode has just been created try something like this:
private TreeNode tempTreeNode;

private Color nodeBaseColor = Color.White;
private Color justCreatedNodeColor = Color.Yellow;

private void btnMakeNewTreeNode_Click(object sender, EventArgs e)
{
    // reset the background color of the last created node
    if (tempTreeNode != null) tempTreeNode.BackColor = nodeBaseColor;

    // create the new node, add it to the TreeView
    // set its background color
    tempTreeNode = new TreeNode(textBox1.Text);
    treeView1.Nodes.Add(tempTreeNode);
    tempTreeNode.BackColor = justCreatedColor;

    // focus on the TreeView, select the new node
    // then nullify the selection
    treeView1.Focus();
    treeView1.SelectedNode = tempTreeNode;
    treeView1.SelectedNode = null;
}
 
Share this answer
 
Comments
Gauri Bharde 19-Dec-13 3:51am    
thanks!!
Consider
MyTreeView.Select()

Reason:
When a button is clicked, the TreeView loss focus since the user clicked elsewhere.
When the treeview is not in focus, the highlighted item is not seen.
It can be mistaken as no item is selected, or "the selected node is lost".
Putting the TreeView back in focus restores the highlight.
This can be done in the button's click event handler.
So after clicking the button, the focus to the treeview is restored programatically.
 
Share this answer
 
v4
Comments
Tony Hill 30-Mar-21 11:55am    
Only 7 years late with an answer which does not answer the OP's question, the OP actually wants to focus (highlight) a newly created node in the treeview not focus a control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900