Link to home
Start Free TrialLog in
Avatar of TeDeSm
TeDeSm

asked on

VB.NET TreeView exception

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

Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

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
Avatar of TeDeSm
TeDeSm

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of TeDeSm
TeDeSm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TeDeSm

ASKER

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.