Hi,
I just want my user to to select a node and then use a button (up or down) to move in the treeview. I have seen example but they related to ASP.Net. I would like to do the same in WinForm.
I also tried using the radListbox and achived the desired functionality of moving Up and Down. However with radListbox I have different problem. The problem is, I want users to check and uncheck an item in the list. I can not figure out how I make radListbox items to appear with check box.
Any help in above to problem will be highly appreciated.
4 Answers, 1 is accepted
Here is the code that I used to move the selected node up and down with two buttons:
private void btnUp_Click(object sender, EventArgs e) | |
{ | |
RadTreeNode selectedNode = this.radTreeView1.SelectedNode; | |
if (selectedNode == null) | |
{ | |
this.radTreeView1.SelectedNode = this.radTreeView1.LastVisibleNode; | |
return; | |
} | |
RadTreeNode prevVisible = selectedNode.PrevVisibleNode; | |
if (prevVisible != null) | |
{ | |
this.radTreeView1.SelectedNode = prevVisible; | |
int treeViewTop = this.radTreeView1.ClientRectangle.Y - this.radTreeView1.ScrollPosition.Y; | |
if (prevVisible.Bounds.Top <= treeViewTop) | |
{ | |
this.radTreeView1.ScrollBy(1); | |
} | |
} | |
} | |
private void btnDown_Click(object sender, EventArgs e) | |
{ | |
RadTreeNode selectedNode = this.radTreeView1.SelectedNode; | |
if (selectedNode == null) | |
{ | |
this.radTreeView1.SelectedNode = this.radTreeView1.FirstVisibleNode; | |
return; | |
} | |
RadTreeNode nextVisible = selectedNode.NextVisibleNode; | |
if (nextVisible != null) | |
{ | |
this.radTreeView1.SelectedNode = nextVisible; | |
int treeViewBottom = this.radTreeView1.ClientRectangle.Y - | |
this.radTreeView1.ScrollPosition.Y + this.radTreeView1.ClientRectangle.Height; | |
if (nextVisible.Bounds.Bottom > treeViewBottom) | |
{ | |
this.radTreeView1.ScrollBy(-1); | |
} | |
} | |
} |
Regarding your other question you can take a look at this KB article that describes how to have check boxes in a RadComboBox. This should work for RadListBox with little changes.
If you have any additional questions, please contact me.
Kind regards,
Jordan
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Hi Telerik,
I have expanded on this example because I needed cyclical navigation in the treeview, but I’m running into issues.
If the user is at the top of the tree and they click btnup they should move to the bottom (last node) in the tree, and if the user is at the bottom of the tree and click btndown they should move to the top of the tree.
Moving to the top of the tree works fine. I just get the root node of the tree and select it, but for some reason I cannot get the last node of the tree.
Here is the code I added…
In btnup_click add this else condition to the last if statement.
else
{
this.tvwrDocVariables.SelectedNode = selectedNode.RootNode;
AppRadTreeViewScrollHelper.ScrollToNode(this.tvwrDocVariables, selectedNode.RootNode);
}
In the btndown_click add this else condition to the last if statement.
else
{
this.tvwrDocVariables.SelectedNode = this.tvwrDocVariables.LastVisibleNode;
AppRadTreeViewScrollHelper.ScrollToNode(this.tvwrDocVariables, this.tvwrDocVariables.LastVisibleNode);
}
Oh… I also implimented your scrollhelper class from a previous post.
The LastVisibleNode call in the btndown returns a null value for the node. What am I doing wrong?
Many thanks,
~Mike
Thank you for writing.
There was an issue in the LastVisibleNode property that caused it to return null in certain cases - for example if you set a node that has no children to be expanded. The issue has now been addressed and will not be present in our next release.
For now you can use the following method to find the last visible node in a RadTreeView:
private RadTreeNode GetLastVisibleNode(RadTreeNodeCollection nodes) |
{ |
if (nodes != null) |
{ |
RadTreeNode node = null; |
for (int i = nodes.Count - 1; i >= 0; i--) |
{ |
node = nodes[i]; |
if (node.Visible) |
{ |
if (!node.Expanded || node.Nodes.VisibleCount == 0) |
{ |
return node; |
} |
return RadTreeNode.GetLastVisibleNode(node.Nodes); |
} |
} |
} |
return null; |
} |
You invoke it as shown below:
RadTreeNode lastVisibleNode = this.GetLastVisibleNode(this.radTreeView1.Nodes);
If you have additional questions, please contact me.
Sincerely yours,
Jordan
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Please note that in Q1 2011 we have introduced a major upgrade of RadTreeView control, which is now virtualized and fully customizable. Feel free to download the latest release and try it out. For more information on our latest release refer to this blog post.
Kind regards,
Stefan
the Telerik team