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.InvalidOperationExc eption 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?
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)
tvwPreAlert.SelectedNode = tvwNodeChild ' exception here
End If
EXCEPTION IS:
System.InvalidOperationExc
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
Wayne