Solved

VB.NET TreeView exception

Posted on 2010-11-08
4
517 Views
Last Modified: 2012-05-10
I have started getting an exception thrown when trying to select a child node.

The Treeview is fillled with a company and child nodes are entered, when a specific child node is being entered it is selected with:

If CLng(prealert1.PreAlertID) = idprealert Then
  tvwPreAlert.SelectedNode = tvwNodeChild ' exception here
End If

EXCEPTION IS:
System.InvalidOperationException was unhandled
  Message=Collection was modified; enumeration operation may not execute.
 
A first guess is my logic is wrong in filling the child nodes and also selcting one in the list. If that is so then the other way to do it would be to finish filling the treeview then loop through companies/child nodes to select the required one.

If I had to loop through the treeview what would be the syntax to do that correctly?
Dim company2 As Company

			Dim tvwNodeCompany As TreeNode

			Dim tvwNodeChild As TreeNode



			For Each company2 In companyArray



				tvwNodeCompany = tvwPreAlert.Nodes.Add(company2.CompanyLedgerType + " ~ " + company2.CompanyName)



				tvwNodeCompany.Tag = company2.CompanyID.ToString()

				tvwNodeCompany.ForeColor = Color.Blue



				Dim prealert1 As PreAlertNumber

				For Each prealert1 In company2.CompanyPreAlerts

					tvwNodeChild = tvwPreAlert.Nodes(companyArray.IndexOf(company2)).Nodes.Add("Pre-alert " + prealert1.PreAlertID)

					tvwNodeChild.Tag = prealert1.PreAlertID

					If CLng(prealert1.PreAlertID) = idprealert Then

						tvwPreAlert.SelectedNode = tvwNodeChild

					End If

				Next prealert1



			Next company2

Open in new window

0
Comment
Question by:TeDeSm
  • 3
4 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 34083573
What routine are you running this code from? If it's one of the Treeview's Select events, then that code is going to be called again. What you may need to do is have a class level boolean variable and set it to True prior to setting the SelectedNode. Then in the event, check the value of that boolean variable and if True, Exit Sub.

Wayne
0
 

Author Comment

by:TeDeSm
ID: 34085998
I am selecting a value from a combobox, see code below.
Private Sub cboPreAlert_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPreAlert.SelectedIndexChanged

		Dim x As Long = cboPreAlert.SelectedValue



		' Clear the TreeView

		tvwPreAlert.BeginUpdate()

		tvwPreAlert.Nodes.Clear()

		tvwPreAlert.EndUpdate()

		FilterMyTreeViewAlertNumber(x)



	End Sub

Open in new window

0
 

Accepted Solution

by:
TeDeSm earned 0 total points
ID: 34091927
Found a solution to the exception, remove the erroneous code;
If CLng(prealert1.PreAlertID) = idprealert Then
  tvwPreAlert.SelectedNode = tvwNodeChild
End If

Then use the following AFTER filling the treeview that uses a function to select the required child node.

See this link:
http://www.xtremedotnettalk.com/showthread.php?t=71346



' Give treeview focus

Me.tvwPreAlert.Focus()

'look for a node with a text property value of Pre-alert + idprealert



Me.tvwPreAlert.SelectedNode = GetNode("Pre-alert " + idprealert.ToString, Me.tvwPreAlert.Nodes())

'ensure the focus set on the node

Me.tvwPreAlert.Select()

' Set the ListView's 'HideSelection' property to False

' to show selected node

Me.tvwPreAlert.HideSelection = False





Private Function GetNode(ByVal text As String, ByVal parentCollection As TreeNodeCollection) As TreeNode

Dim ret As TreeNode

Dim child As TreeNode



For Each child In parentCollection 'step through the parentcollection           

  If child.Text = text Then

    ret = child

  ElseIf child.GetNodeCount(False) > 0 Then	' if there is child items then call this function recursively

    ret = GetNode(text, child.Nodes)

  End If



  If Not ret Is Nothing Then Exit For 'if something was found, exit out of the for loop       

Next

Return ret

End Function

Open in new window

0
 

Author Closing Comment

by:TeDeSm
ID: 34130136
My previous code failed due to the fact the tree was still being filled when I was trying to select a node. The exception was correct.

Waiting until the tree had finished being filled the selecting a node worked.

Remember that you are passing a string to the function which is used to check the Text value of the child node at child.Text = text.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Clone Visual studio 2013 solution 5 24
help with issues with ReportViewer in VS2015 5 37
Populating an array of locale_time_info 8 37
Visual Studio 2013 Shortcut (VB) 4 34
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now