473,569 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Modify the order of a treeview control during runtime?

10 New Member
I have a treeview control on a custom task pane in Excel. I've enable the ability to use Drag & Drop (by following this how-to) on the treeview to change the order of the nodes. The problem though is if I close the application and then go back in, the nodes are back in the order they were before I had reordered them. Is there a way that I can modify my DragDrop event to permanently change the index of the nodes?

Here's my code if you're interested. It's almost literally a copy and paste job from the site that I linked to above:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ManningFiltersTreeView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ManningFiltersTreeView.DragDrop
  2.  
  3.         'Check that there is a TreeNode being dragged
  4.         If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
  5.               True) = False Then Exit Sub
  6.  
  7.         'Get the TreeView raising the event (incase multiple on form)
  8.         Dim selectedTreeview As TreeView = CType(sender, TreeView)
  9.  
  10.         'Get the TreeNode being dragged
  11.         Dim dropNode As TreeNode = _
  12.               CType(e.Data.GetData("System.Windows.Forms.TreeNode"),  _
  13.               TreeNode)
  14.  
  15.         'The target node should be selected from the DragOver event
  16.         Dim targetNode As TreeNode = selectedTreeview.SelectedNode
  17.  
  18.         'Remove the drop node from its current location
  19.         dropNode.Remove()
  20.  
  21.         'If there is no targetNode add dropNode to the bottom of
  22.         'the TreeView root nodes, otherwise add it to the end of
  23.         'the dropNode child nodes
  24.         If targetNode Is Nothing Then
  25.             selectedTreeview.Nodes.Add(dropNode)
  26.         Else
  27.             targetNode.Nodes.Add(dropNode)
  28.         End If
  29.  
  30.         'Ensure the newly created node is visible to
  31.         'the user and select it
  32.         dropNode.EnsureVisible()
  33.         selectedTreeview.SelectedNode = dropNode
  34.  
  35.     End Sub
  36.  
  37.     Private Sub ManningFiltersTreeView_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ManningFiltersTreeView.DragEnter
  38.  
  39.         'See if there is a TreeNode being dragged
  40.         If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
  41.             True) Then
  42.             'TreeNode found allow move effect
  43.             e.Effect = DragDropEffects.Move
  44.         Else
  45.             'No TreeNode found, prevent move
  46.             e.Effect = DragDropEffects.None
  47.         End If
  48.  
  49.     End Sub
  50.  
  51.     Private Sub ManningFiltersTreeView_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ManningFiltersTreeView.DragOver
  52.  
  53.         'Check that there is a TreeNode being dragged 
  54.         If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
  55.                True) = False Then Exit Sub
  56.  
  57.         'Get the TreeView raising the event (incase multiple on form)
  58.         Dim selectedTreeview As TreeView = CType(sender, TreeView)
  59.  
  60.         'As the mouse moves over nodes, provide feedback to 
  61.         'the user by highlighting the node that is the 
  62.         'current drop target
  63.         Dim pt As Point = _
  64.             CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
  65.         Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt)
  66.  
  67.         'See if the targetNode is currently selected, 
  68.         'if so no need to validate again
  69.         If Not (selectedTreeview.SelectedNode Is targetNode) Then
  70.             'Select the    node currently under the cursor
  71.             selectedTreeview.SelectedNode = targetNode
  72.  
  73.             'Check that the selected node is not the dropNode and
  74.             'also that it is not a child of the dropNode and 
  75.             'therefore an invalid target
  76.             Dim dropNode As TreeNode = _
  77.                 CType(e.Data.GetData("System.Windows.Forms.TreeNode"),  _
  78.                 TreeNode)
  79.  
  80.             Do Until targetNode Is Nothing
  81.                 If targetNode Is dropNode Then
  82.                     e.Effect = DragDropEffects.None
  83.                     Exit Sub
  84.                 End If
  85.                 targetNode = targetNode.Parent
  86.             Loop
  87.         End If
  88.  
  89.         'Currently selected node is a suitable target
  90.         e.Effect = DragDropEffects.Move
  91.         'End If
  92.  
  93.     End Sub
  94.  
  95.     Private Sub ManningFiltersTreeView_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ManningFiltersTreeView.ItemDrag
  96.  
  97.         'Set the drag node and initiate the DragDrop 
  98.         DoDragDrop(e.Item, DragDropEffects.Move)
  99.  
  100.     End Sub
Oct 19 '08 #1
3 4696
dutsnekcirf
10 New Member
I received a response in a different forum. I wanted to include it here because I'm hoping someone might be able to help me implement the solution. The response was:

You'd need to save the status when the control is removed from memory, then restore it in the code that's initializing the control (when the task pane is being displayed the first time).

The better place to ask this kind of question would be a forum that specializes in working with Windows Forms and their controls. The specialists there should be able to tell you the most efficient way to save the state of the control, then restore it at a later point.
So if I understand this correctly, I need to save the status of the treeview control by writing it to a file (or another spreadsheet? (yes, this is in Excel using Visual Studio Tools for Office) before closing the workbook and then when the workbook is reopened it needs to read that data back into the control to display it's last state? Could this be done by databinding the treeview control to an Excel table? or would it be better to use some sort of XML file to better represent the heirarchy of the treeview control?

Does anyone have any idea how to do this?
Oct 20 '08 #2
dutsnekcirf
10 New Member
Okay, here's an idea everyone. What if on the thisworkbook_sh utdown event, I looped through the tree and extracted the text and fullpath properties of each node and added them to a Text and Path array and then wrote both arrays to a blank worksheet.

And then as part of the thisworkbook_st artup event, I read through the range on my blank worksheet and added a node to the treeview and assigned each new node the fullpath property.

So far I've got it working to write the status of my treeview to the spreadsheet. Here's the code that I'm using up to this point:


Expand|Select|Wrap|Line Numbers
  1.         Dim Text As New List(Of String)
  2.         Dim Path As New List(Of String)
  3.  
  4.         'Call the ExtractTextFromNodes function.  This finds all the text and fullpath properties of each node in the treeview and adds them to
  5.         'a an array.
  6.         ExtractTextFromNodes(APPredator.ManningFiltersTreeView.Nodes, Text, Path)
  7.  
  8.  
  9.         Dim int As Integer = 0
  10.  
  11.         For i = 1 To Text.Count
  12.             CType(Globals.ThisWorkbook.Sheets("Manning Config"), Excel.Worksheet).Range("BV" & i).Value2 = Text(int)
  13.             int = int + 1
  14.         Next
  15.  
  16.         int = 0
  17.         For i = 1 To Path.Count
  18.             CType(Globals.ThisWorkbook.Sheets("Manning Config"), Excel.Worksheet).Range("BW" & i).Value2 = Path(int)
  19.             int = int + 1
  20.         Next
  21.  
  22. Private Shared Sub ExtractTextFromNodes(ByVal nodeList As TreeNodeCollection, ByVal Text As List(Of String), ByVal Path As List(Of String))
  23.  
  24.         Dim n As TreeNode
  25.  
  26.         For Each n In nodeList
  27.  
  28.  
  29.             Text.Add(n.Text)
  30.             Path.Add(n.FullPath)
  31.             ExtractTextFromNodes(n.Nodes, Text, Path)
  32.  
  33.  
  34.         Next
  35.  
  36.     End Sub
Next step is to read the data from my spreadsheet and build a treeview from it. If I've gone off track with this idea, let me know. If there's an easier way to do it, let me know too. :)
Oct 20 '08 #3
dutsnekcirf
10 New Member
I've got this working now. The solution is found here .
Oct 30 '08 #4

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

Similar topics

2
2760
by: Peter Flickinger | last post by:
I have been using a label with the TreeView control - to simulate the ghost image of a node during drag/drop (so that a ghost of the node appears to be floating next to the cursor on a drag/drop). This works peachy when I had a form that used the TreeView control (from Microsoft). The time came that I needed to change the TreeView - so I...
1
2958
by: Alexandru Nedelcu | last post by:
Hi!!! I'm trying to add a UserControl created using the Page.LoadCotnrol("test.ascx") method to a Page's Control collection and the exception I get is ( the complete stack trace): System.Web.UI.ControlCollection.Add(Control child) Akela.WebFramework.Design.TreeView.TreeNode.LoadUserControl() in...
14
6798
by: Evan Kontos | last post by:
I am trying to implement a Treeview w/an XML file and I even copied and pasted examples from MSDN but can't get them to work. Any suggestions welcome. XML File <TREENODES> <TREENODE TEXT="Document-1"> <TREENODE TEXT="Folder-1" EXPANDED="true"> <TREENODE TEXT="Document-2" />
16
2433
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? -> array2 is assigned to array1 ....???? the reason being it is
1
6337
by: mongphong28 | last post by:
Hi, I'm using a treeview as a menu, and when the user clicks on a node I want the focus to set to a control (ie textbox) in a panel to the right. The problem I'm having is the focus will not stay on the control, but instead jumps back to the treeview. I'm setting the focus in the AfterSelect event of the treeview, and if I step through...
1
693
by: kvicky | last post by:
I am trying to load child nodes to a TreeNode in a TreeView in a ASP.net web application. The Treeview with parent nodes are loaded on a Page_load while doing if( ! ISPostback ) and then in the Treeview event I am dynamically trying to load the child nodes to the exisisting TreeView. The problem is since the TreeView is being loaded on not...
0
2471
by: apenly | last post by:
Hi all- I'm trying to Render a TreeView in a custom control, but I'm receiving a NullReferenceException at runtime. If I put the TreeView on the page it works fine, but as soon as I try to render it within my control it crashes and burns. I've even tried placing it within another control (a PlaceHolder) and tried to render the PlaceHolder...
3
5856
by: roucha | last post by:
Hey guys, i'm still working on that chat program, and i was wondering if there was a way to modify the name of already created nodes during runtime. i know that you can create them but i don't know how to edit the name of created ones. thanks.
2
2274
by: letobale1982 | last post by:
Hello I would like to get some ideas on how to modify a Combobox during runtime and save the changes so next time the form is ran changes are available. Here is the plan. A form displays a combobox that get populated with several items. Next to the combobox I will add three buttons: Add, Delete and Modify. My idea is that the combobox will...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.