469,623 Members | 1,683 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Moving Nodes

Sl1ver
196 100+
I've got a problem, i got the nodes to move but
1. they copy nodes(if you drag it to 3 different places it will actually have 3 of the same nodes)
2. i want to make the nodes, if moved update the new nodes locations

This is my coding
Expand|Select|Wrap|Line Numbers
  1.  private void tvDescriptions_MouseDown(object sender, MouseEventArgs e)
  2.         {
  3.             TreeView tree = (TreeView)sender;
  4.  
  5.             // Get the node underneath the mouse.
  6.             TreeNode node = tree.GetNodeAt(e.X, e.Y);
  7.  
  8.             // Start the drag-and-drop operation with a cloned copy of the node.
  9.             if (node != null)
  10.             {
  11.                 tree.DoDragDrop(node.Clone(), DragDropEffects.Copy);
  12.             }
  13.         }
  14.  
  15.         private void tvDescriptions_DragOver(object sender, DragEventArgs e)
  16.         {
  17.             // Get the tree.
  18.             TreeView tree = (TreeView)sender;
  19.  
  20.             // Drag and drop denied by default.
  21.             e.Effect = DragDropEffects.None;
  22.  
  23.             // Is it a valid format?
  24.             if (e.Data.GetData(typeof(TreeNode)) != null)
  25.             {
  26.                 // Get the screen point.
  27.                 Point pt = new Point(e.X, e.Y);
  28.  
  29.                 // Convert to a point in the TreeView's coordinate system.
  30.                 pt = tree.PointToClient(pt);
  31.  
  32.                 // Is the mouse over a valid node?
  33.                 TreeNode node = tree.GetNodeAt(pt);
  34.                 if (node != null)
  35.                 {
  36.                     e.Effect = DragDropEffects.Copy;
  37.                     tree.SelectedNode = node;
  38.                 }
  39.             }
  40.         }
  41.  
  42.         private void tvDescriptions_DragDrop(object sender, DragEventArgs e)
  43.         {
  44.             // Get the tree.
  45.             TreeView tree = (TreeView)sender;
  46.  
  47.             // Get the screen point.
  48.             Point pt = new Point(e.X, e.Y);
  49.  
  50.             // Convert to a point in the TreeView's coordinate system.
  51.             pt = tree.PointToClient(pt);
  52.  
  53.             // Get the node underneath the mouse.
  54.             TreeNode node = tree.GetNodeAt(pt);
  55.  
  56.             // Add a child node.
  57.             node.Nodes.Add((TreeNode)e.Data.GetData(typeof(TreeNode)));
  58.  
  59.             // Show the newly added node if it is not already visible.
  60.             node.Expand();
  61.         }
  62.  
  63.         private void tvDescriptions_ItemDrag(object sender, ItemDragEventArgs e)
  64.         {
  65.  
  66.         }
  67.  
Apr 9 '09 #1
6 3772
tlhintoq
3,525 Expert 2GB
So you want a 'move' not a copy? Am I reading that right?
Do the copy, then at the node where you moved it from perform a .Remove

At line 6 you learn which node is being copied (naming it 'node')
between lines 11 and 12 add... tree.Nodes.Remove(node);
Apr 11 '09 #2
Sl1ver
196 100+
Thanx, you really know your stuff. How or where will i post a event to update the database with the new information?
Apr 14 '09 #3
tlhintoq
3,525 Expert 2GB
where will i post a event to update the database with the new information?
I don't know your program as well as you do, so I can only give you a generic answer... I would raise and event to perform an update after I'm done with all my changes. Where that fits into what you've engineered, only you know. This treeview could be on its own user control, or it could be on a floating pallet or it could be in the middle of your one and only form. I can't say. But that's the beauty of events... As long as you have another class 'listening' (subscribed) to the event it will still work.
Apr 14 '09 #4
Sl1ver
196 100+
Hi,

this is my coding. I get a error that states "Must use a updatable query" and if you double click on the nodes the whole program freezes. any help?

Expand|Select|Wrap|Line Numbers
  1.  private void tvLocations_MouseDown(object sender, MouseEventArgs e)
  2.         {
  3.             TreeView tree = (TreeView)sender;
  4.  
  5.             // Get the node underneath the mouse.
  6.             TreeNode node = tree.GetNodeAt(e.X, e.Y);
  7.  
  8.             // Start the drag-and-drop operation with a cloned copy of the node.
  9.             if (node != null)
  10.             {
  11.                 tree.DoDragDrop(node, DragDropEffects.Copy);
  12.                 //node.Remove();
  13.  
  14.             }
  15.         }
  16.  
  17.         private void tvLocations_DragOver(object sender, DragEventArgs e)
  18.         {
  19.             // Get the tree.
  20.             TreeView tree = (TreeView)sender;
  21.  
  22.  
  23.             // Drag and drop denied by default.
  24.             e.Effect = DragDropEffects.None;
  25.  
  26.             // Is it a valid format?
  27.             if (e.Data.GetData(typeof(TreeNode)) != null)
  28.             {
  29.                 // Get the screen point.
  30.                 Point pt = new Point(e.X, e.Y);
  31.  
  32.                 // Convert to a point in the TreeView's coordinate system.
  33.                 pt = tree.PointToClient(pt);
  34.  
  35.                 // Is the mouse over a valid node?
  36.                 TreeNode node = tree.GetNodeAt(pt);
  37.                 if (node != null)
  38.                 {
  39.                     e.Effect = DragDropEffects.Copy;
  40.                     tree.SelectedNode = node;
  41.  
  42.                 }
  43.             }
  44.         }
  45.  
  46.         private void tvLocations_DragDrop(object sender, DragEventArgs e)
  47.         {
  48.             try
  49.             {
  50.                 // Get the tree.
  51.                 TreeView tree = (TreeView)sender;
  52.  
  53.                 tree.BeginUpdate();
  54.  
  55.                 // Get the screen point.
  56.                 Point pt = new Point(e.X, e.Y);
  57.  
  58.                 // Convert to a point in the TreeView's coordinate system.
  59.                 pt = tree.PointToClient(pt);
  60.  
  61.                 // Get the node underneath the mouse.
  62.                 TreeNode node = tree.GetNodeAt(pt);
  63.  
  64.                 TreeNode childNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
  65.  
  66.                 // Remoce this node
  67.                 tree.Nodes.Remove(childNode);
  68.  
  69.                 // Add the node to the new parent
  70.                 node.Nodes.Add(childNode);
  71.  
  72.                 // Show the newly added node if it is not already visible.
  73.                 node.Expand();
  74.  
  75.                 tree.EndUpdate();
  76.  
  77.                 UpdateDBStructure(childNode.Tag, node.Tag);
  78.             }
  79.             catch (Exception ex)
  80.             {
  81.                 MessageBox.Show("Error: " + ex.Message);
  82.             }
  83.         }
  84.  
  85.         private void tvLocations_ItemDrag(object sender, ItemDragEventArgs e)
  86.         {
  87.  
  88.         }
  89.  
  90.         private void UpdateDBStructure(object Id, object parentId)
  91.         {                        
  92.             OleDbConnection mConn = new OleDbConnection(strCon);
  93.  
  94.             mConn.Open();            
  95.  
  96.             string UpdateStr = @"UPDATE qx_AssetLocation SET 
  97.                                  alc__acl_id = " + parentId + 
  98.                                " where alc__ID = " + Id;
  99.  
  100.             OleDbCommand myCmd = mConn.CreateCommand();
  101.             myCmd.CommandText = UpdateStr;
  102.             try
  103.             {
  104.                 myCmd.ExecuteNonQuery();
  105.                 //MessageBox.Show("Data added to database");
  106.  
  107.             }
  108.             catch (Exception ed)
  109.             {
  110.                 MessageBox.Show("Error: " + ed.Message, "Error");
  111.             }
  112.             finally
  113.             {
  114.                 myCmd.Dispose();
  115.                 mConn.Close();
  116.                 mConn.Dispose();
  117.                 tvLocations.Nodes.Clear();
  118.                 //PopulateTree();
  119.             }
  120.  
  121.  
  122.         }
Apr 15 '09 #5
tlhintoq
3,525 Expert 2GB
Put a breakpoint in your UpdateDbStructure (line 92 or so) and walk through it one step at a time with F-10. See if that helps you find the exact location and nature of the issue.
Apr 15 '09 #6
Sl1ver
196 100+
thanx man for the help, things are working
Apr 16 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by alanrn | last post: by
4 posts views Thread by ewosch | last post: by
1 post views Thread by Victor | last post: by
1 post views Thread by db2udbgirl | last post: by
2 posts views Thread by Bart Steur | last post: by
reply views Thread by Malcolm Dew-Jones | last post: by
1 post views Thread by barantamer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.