I am working with Windows Form project. I am using VS2013 and .net version is 4.5. i do not have very high knowledge in .Net.
I want to up down any node of my tree view which was possible from this post https://stackoverflow.com/a/2204091/14326209
Here is the code
/// <summary>
/// Move Selected Node Up in TreeNode Collection
/// </summary>
/// <param name="node"></param>
public static void MoveUp(this TreeNode node)
{
TreeNode parent = node.Parent;
TreeView view = node.TreeView;
if (parent != null)
{
int index = parent.Nodes.IndexOf(node);
if (index > 0)
{
parent.Nodes.RemoveAt(index);
parent.Nodes.Insert(index - 1, node);
node.Text = node.Text;
}
}
else if (node.TreeView.Nodes.Contains(node)) //root node
{
int index = view.Nodes.IndexOf(node);
if (index > 0)
{
view.Nodes.RemoveAt(index);
view.Nodes.Insert(index - 1, node);
node.Text = node.Text;
}
}
//view.Focus();
//view.SelectedNode = node;
//node.EnsureVisible();
}
/// <summary>
/// Move Selected Node Down in TreeNode Collection
/// </summary>
/// <param name="node"></param>
public static void MoveDown(this TreeNode node)
{
TreeNode parent = node.Parent;
TreeView view = node.TreeView;
if (parent != null)
{
int index = parent.Nodes.IndexOf(node);
if (index < parent.Nodes.Count - 1)
{
parent.Nodes.RemoveAt(index);
parent.Nodes.Insert(index + 1, node);
node.Text = node.Text;
}
}
else if (view != null && view.Nodes.Contains(node)) //root node
{
int index = view.Nodes.IndexOf(node);
if (index < view.Nodes.Count - 1)
{
view.Nodes.RemoveAt(index);
view.Nodes.Insert(index + 1, node);
node.Text = node.Text;
}
}
//view.Focus();
//view.SelectedNode = node;
//node.EnsureVisible();
}
private void tvCsmTuner_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
tvCsmTuner.SelectedNode.MoveUp();
}
else if (e.KeyCode == Keys.Down)
{
tvCsmTuner.SelectedNode.MoveDown();
}
label3.Focus();
}
when i call Node.MoveDown() and Node.MoveUp() then Node is getting perfectly moving but selection from Selected node going to different node after node moved. i want selection should persist on selected node after moving the Node.
So please guide me what changes need to add in the above code as a result selection on selected node should not go to any node.
I tried this to persist node selection but did not work.
view.SelectedNode = node;
Looking for suggestion. thanks
Related
I created Check Box dynamically inside a wrap panel in wpf. How to find the checked check box in code behind.
If you want a more compact not reusable and specialized version..
WrapPanel wp = new WrapPanel();
for (int i = 0; i < 10; i++)
{
CheckBox chb = new CheckBox();
chb.Name = string.Format("Id{0}", i);
wp.Children.Add(chb);
}
foreach (CheckBox el in wp.Children)
{
if (el.Name == "Id3")
{
return el;
}
}
/// <summary>
/// Finds a Child of a given item in the visual tree.
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter.
/// If not matching item can be found,
/// a null parent is being returned.</returns>
public static T FindChild<T>(DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
If you are looking for the text of the selected checkbox do somthing like thes:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
string a =checkBox1.Text;
}
I want to traverse through a treeview & set the value property to be its position in the tree as shown below
A[val:1]->A1[val:11]
l l--->A2[val:12]
l----->A3[val:13]
B[val:2]->B1[val:21]
l l--->B2[val:22]
C[val:3]->C1[val:31]
l l--->C2[val:32]
I have written a recursive which returns me all the nodes but i am unable to assign the desired position to its nodes.
private void TraverseTreeNode(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
TraverseTreeNode(node.ChildNodes);
}
}
Considering that TreeNode.Value is of type string, this will, starting at level = 1:
private static void TraverseTreeNode(TreeNodeCollection nodes, int parentNumber)
{
var childNumber = 1;
foreach (TreeNode node in nodes)
{
node.Value = string.Format("{0}{1}", parentNumber, childNumber ).Substring(0,node.Depth+1);
TraverseTreeNode(node.ChildNodes, parentNumber);
childNumber++;
if (node.Depth == 0) { parentNumber++; }
}
}
Only works for two levels but is easily extendable by adding additional parameters to TraverseTreeNode.
UPDATE
The following will work for any depth in hierarchy:
private static void TraverseTreeNode(TreeNodeCollection nodes, int parentNumber)
{
var childNumber = 1;
foreach (TreeNode node in nodes)
{
node.Value = node.Parent != null && node.Parent.Value != null
? string.Format("{0}{1}", node.Parent.Value, childNumber)
: string.Format("{0}{1}", parentNumber, childNumber).Substring(0, node.Depth + 1);
TraverseTreeNode(node.ChildNodes, parentNumber);
childNumber++;
if (node.Depth == 0) { parentNumber++; }
}
}
As you need recursive method, try this
private void Caller()
{
TraverseTreeNode(treeView1.Nodes);
}
private void TraverseTreeNode(TreeNodeCollection nodes)
{
int index = 1;
foreach (TreeNode node in nodes)
{
node.Text = (node.Parent != null ? node.Parent.Text : string.Empty) + index++;
TraverseTreeNode(node.Nodes);
}
}
I have an asp.net treeview (acting as a remote file and folder browser). When a node is selected all child nodes are automatically selected. This works fine (c# code below).
When any child is checked/unchecked I want all related parents to be checked/unchecked as well. I cannot figure this out. I want to use c# to do this.
-item1
------child1
------child2
--child2.1
--child2.2
------child3
Example 1 - if child 2.2 had its checkbox checked then child 2 and item1 will be checked automatically using c# code behind
Example 2 - if item1, child 2 , child 2.1 and child 2.2 were checked and if the user were to uncheck child 2.2 then item1, child 2 would remain checked as child 2.1 is still checked
thanks
Damo
below is my code that checks all children of a checked item and works fine.
/// <summary>
/// Checks or unchecks child nodes when a parent node is checked or unchecked.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnTreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
// Determine if checked Node is a root node.
if (e.Node.ChildNodes.Count > 0)
{
// Check or uncheck all of the child nodes based on status of parent node.
if (e.Node.Checked)
ChangeChecked(e.Node, true);
else
ChangeChecked(e.Node, false);
}
}
/// <summary>
/// Recursively checks or unchecks all child nodes for a given TreeNode.
/// </summary>
/// <param name="node">TreeNode to check or uncheck.</param>
/// <param name="check">Desired value of TreeNode.Checked.</param>
private void ChangeChecked(TreeNode node, bool check)
{
// "Queue" up child nodes to be checked or unchecked.
if (node.ChildNodes.Count > 0)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
ChangeChecked(node.ChildNodes[i], check);
}
node.Checked = check;
}
I figured this out
I have 2 functions as follows . The first checks all the children and the second checks all the parents.
Hope this helps someone.
Damo
ChangeCheckedChildren(e.Node, e.Node.Checked);
ChangeCheckedParents(e.Node, e.Node.Checked);
/// <summary>
/// Recursively checks or unchecks all child nodes for a given TreeNode.
/// </summary>
/// <param name="node">TreeNode to check or uncheck.</param>
/// <param name="check">Desired value of TreeNode.Checked.</param>
private void ChangeCheckedChildren(TreeNode node, bool check)
{
try
{
// "Queue" up child nodes to be checked or unchecked.
if (node.ChildNodes.Count > 0)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
ChangeCheckedChildren(node.ChildNodes[i], check);
}
node.Checked = check;
}
catch (Exception ex)
{
}
}
/// <summary>
/// Recursively checks or unchecks all parent nodes for a given TreeNode.
/// </summary>
/// <param name="node">TreeNode to check or uncheck.</param>
/// <param name="check">Desired value of TreeNode.Checked.</param>
private void ChangeCheckedParents(TreeNode node, bool check)
{
try
{
if (node.Parent == null) // if we are at the root node
{
if (node.ChildNodes.Count > 0)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Checked == true)
{
node.Checked = true;
return;
}
}
node.Checked = false;
}
else
{
node.Checked = check;
}
}
else
{
// Check all parents if the user is checking
if (check == true)
{
node.Checked = check;
ChangeCheckedParents(node.Parent, check);
}
else
{
// Do not uncheck a parent if any of its other children or their children are checked
if (node.ChildNodes.Count > 0)
{
// Default to not check and check if required
node.Checked = false;
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Checked == true)
{
node.Checked = true;
}
}
ChangeCheckedParents(node.Parent, check);
}
else
{
node.Checked = check;
ChangeCheckedParents(node.Parent, check);
}
}
}
}
catch (Exception ex)
{
}
}
I would like to change the order of System.Windows.Forms.TreeNodes on the same level.
any suggestions on how this might be done in .net-2.0.
You need to manipulate the TreeView's Nodes collection. See TreeNodeCollection.
If you have three tree nodes and you want to move the last one to the front, for example: (Note: not tested code, but shows the idea)
var lastNode = MyTreeView.Nodes[2];
MyTreeView.Nodes.Remove(lastNode);
MyTreeView.Nodes.Insert(0, lastNode);
void MoveNodeUp(TreeNode node)
{
TreeNode parentNode = node.Parent;
int originalIndex = node.Index;
if (node.Index == 0) return;
TreeNode ClonedNode = (TreeNode)node.Clone();
node.Remove();
parentNode.Nodes.Insert(originalIndex - 1, ClonedNode);
parentNode.TreeView.SelectedNode = ClonedNode;
}
That's what I've written:
public void MoveNode(TreeView tv, TreeNode node, bool up)
{
if ((node.PrevNode == null) && up) {
return;
}
if ((node.NextNode == null) && !up) {
return;
}
int newIndex = up ? node.Index - 1 : node.Index + 1;
var nodes = tv.Nodes;
if (node.Parent != null) {
nodes = node.Parent.Nodes;
}
nodes.Remove(node);
nodes.Insert(newIndex, node);
}
I wrote this code which does not require any cloning.
For my case it moves up one position in the sibling nodes but can be adapted
TreeNode selectedNode = treeViewChain.SelectedNode;
if (selectedNode != null && selectedNode.Index > 0)
{
TreeNode parent = selectedNode.Parent;
int selectedIndex = selectedNode.Index;
selectedNode.Remove();
parent.Nodes.Insert(selectedIndex - 1, selectedNode);
treeViewChain.SelectedNode = selectedNode;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
i have one treeview and 2 button
i want when click on UP_button, above Node selected if i am click on DOWN_button then selection should be go down
There are some nice built in properties of the node that you can use, so for up you would use:
TreeView.SelectedNode = TreeView.SelectedNode.PrevNode;
and for down:
TreeView.SelectedNode = TreeView.SelectedNode.NextNode;
From MSDN Documentation:
private void SelectNode(TreeNode node)
{
if(node.IsSelected)
{
// Determine which TreeNode to select.
switch(myComboBox.Text)
{
case "Previous":
node.TreeView.SelectedNode = node.PrevNode;
break;
case "PreviousVisible":
node.TreeView.SelectedNode = node.PrevVisibleNode;
break;
case "Next":
node.TreeView.SelectedNode = node.NextNode;
break;
case "NextVisible":
node.TreeView.SelectedNode = node.NextVisibleNode;
break;
case "First":
node.TreeView.SelectedNode = node.FirstNode;
break;
case "Last":
node.TreeView.SelectedNode = node.LastNode;
break;
}
}
node.TreeView.Focus();
}
EDIT
You'd of course use the cases for "Previous" and "Next" or "PreviousVisible" and "NextVisible" in your button click handlers. This code assumes that you select the "action" from a combo box and push a button.
EDIT 2
Just in case you are trying to move the nodes up and down (not the selection), you can use something like the following:
TreeNode sourceNode = treeView.SelectedNode;
if (sourceNode.Parent == null)
{
return;
}
treeView.Nodes.Remove(sourceNode);
treeView.Nodes.Insert(sourceNode.Index + 1, sourceNode);
This would move the current node one down. Please note that you have to write some more code to handle some special cases (e.g. what happens with the first node? Does it work on all levels of the tree?), but basically that's what I'd try.
try this for UP:
private void btnUP_Click(object sender, EventArgs e)
{
var tn = tv.SelectedNode;
if(tn==null) return;
var idx = tn.Index;
txt.Text = "Node: " + idx;
var par = tn.Parent;
if(par==null) return;
par.Nodes.Remove(tn);
if (idx > 0)
{
par.Nodes.Insert(tn.Index - 1, tn);
return;
}
//if you want to move int its parent's parent [grand parent :) ]
if (par.Parent!=null)
par.Parent.Nodes.Add(tn);
}
For ASP.NET Treeview:
/// <summary>
/// MoveComponentUpLinkButton_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void MoveComponentUpLinkButton_Click(object sender, EventArgs e)
{
// Get the selected node
TreeNode sourceNode = this.MyTreeview.SelectedNode;
if (sourceNode != null)
{
// Get the selected node's parent
TreeNode parentNode = this.MyTreeview.SelectedNode.Parent;
if (parentNode != null)
{
int index = -1;
// For each node in selected nodes parent
for (int j = 0; j < parentNode.ChildNodes.Count; j++)
{
// If we found the selected node
if (sourceNode == parentNode.ChildNodes[j])
{
// save index
index = j;
break;
}
}
// If node is not already at top of list
if (index > 0)
{
// Move it up
parentNode.ChildNodes.RemoveAt(index);
parentNode.ChildNodes.AddAt(index - 1, sourceNode);
sourceNode.Selected = true;
}
}
}
}
/// <summary>
/// MoveComponentDownLinkButton_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void MoveComponentDownLinkButton_Click(object sender, EventArgs e)
{
// Get the selected node
TreeNode sourceNode = this.MyTreeview.SelectedNode;
if (sourceNode != null)
{
// Get the selected node's parent
TreeNode parentNode = this.MyTreeview.SelectedNode.Parent;
if (parentNode != null)
{
int index = -1;
// For each node in selected nodes parent
for (int j = 0; j < parentNode.ChildNodes.Count; j++)
{
// If we found the selected node
if (sourceNode == parentNode.ChildNodes[j])
{
// save index
index = j;
break;
}
}
// If node is not already at botton of list
if (index < parentNode.ChildNodes.Count - 1)
{
// Move it down
parentNode.ChildNodes.RemoveAt(index);
parentNode.ChildNodes.AddAt(index + 1, sourceNode);
sourceNode.Selected = true;
}
}
}
}
my problem solved.
thanx for answers
private void button1_Click(object sender, EventArgs e)
{
TreeNode node = new TreeNode();
node = treeView1.SelectedNode;
treeView1.SelectedNode = node.NextVisibleNode;
node.TreeView.Focus();
}