Solved

Problem adding child nodes in treeview

Posted on 2011-02-14
3
754 Views
Last Modified: 2012-05-11
I am getting an exception thrown when trying to add a child node to a treeview. The data for the Parent node is held in an arraylist, the data for the Child nodes is held in an arraylist that is part of the parent array.

The exception is at ....salesorderIDArray.IndexOf(salesorders2)) .....

and is 'Specified argument was out of the range of valid values.
Parameter name: index'

The Parent node is shown correctly.
Public Class SalesOrders
	Inherits [Object]
	Private idOrd As Long
	Private ordNumber As Long
	Private ordDate As Date
	Private ordCustomerAcc As String = ""
	Private ordCustomerName As String = ""
	Private ordFwdQty As Integer = 0
	Private ordAllocQty As Integer = 0
	Private ordStatus As String = ""
	Friend ordSalesOrders As New ArrayList()

	Public Sub New(ByVal idorder As Long, ByVal ordernumber As Long, ByVal orderdate As Date, _
		ByVal ordercustacc As String, ByVal ordercustname As String, ByVal orderfwdqty As Integer, _
		ByVal orderallocqty As Integer, ByVal orderstatus As String)
		Me.idOrd = idorder
		Me.ordNumber = ordernumber
		Me.ordCustomerAcc = ordercustacc
		Me.ordCustomerName = ordercustname
		Me.ordDate = orderdate
		Me.ordFwdQty = orderfwdqty
		Me.ordStatus = orderstatus
		Me.ordAllocQty = orderallocqty
	End Sub

	Public Property IDSalesOrder() As Long
		Get
			Return Me.idOrd
		End Get
		Set(ByVal Value As Long)
			Me.idOrd = Value
		End Set
	End Property

	Public Property SalesOrderNumber() As Long
		Get
			Return Me.ordNumber
		End Get
		Set(ByVal Value As Long)
			Me.ordNumber = Value
		End Set
	End Property

	Public Property CustomerAccount() As String
		Get
			Return Me.ordCustomerAcc
		End Get
		Set(ByVal Value As String)
			Me.ordCustomerAcc = Value
		End Set
	End Property

	Public Property CustomerName() As String
		Get
			Return Me.ordCustomerName
		End Get
		Set(ByVal Value As String)
			Me.ordCustomerName = Value
		End Set
	End Property

	Public Property SalesOrderDate() As Date
		Get
			Return Me.ordDate
		End Get
		Set(ByVal Value As Date)
			Me.ordDate = Value
		End Set
	End Property

	Public Property SalesOrderFwdQty() As Integer
		Get
			Return Me.ordFwdQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordFwdQty = Value
		End Set
	End Property

	Public Property SalesOrderAllocQty() As Integer
		Get
			Return Me.ordAllocQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordAllocQty = Value
		End Set
	End Property

	Public Property SalesOrderStatus() As String
		Get
			Return Me.ordStatus
		End Get
		Set(ByVal Value As String)
			Me.ordStatus = Value
		End Set
	End Property

	Public ReadOnly Property SalesOrderLines() As ArrayList
		Get
			Return Me.ordSalesOrders
		End Get
	End Property
End Class 'End Company class

Public Class SalesOrderLine
	Inherits [Object]
	Private idOrdLine As Long
	Private ordlineOrderID As Long
	Private ordlinePrtSeq As Integer
	Private ordlineItmCode As String
	Private ordlineItmDesc As String
	Private ordlineLineQty As Integer
	Private ordlineAllocQty As Integer
	Private ordlineDespQty As Integer
	Private ordlineInvQty As Integer
	Private ordlinePromDelDte As Date
	Private ordlineFullFillMethID As Long

	Public Sub New(ByVal orderid As Long, ByVal ordlineid As Long, ByVal orderlineprtseq As Integer, ByVal orderlineitmcode As String, ByVal orderlineitmdesc As String, _
  ByVal orderlineordqty As Integer, ByVal orderlineallocqty As Integer, ByVal orderlinedespqty As Integer, ByVal orderlineinvqty As Integer, _
  ByVal orderlinepromdeldte As Date, ByVal orderlinefullfillmthdid As Long)
		Me.ordlineOrderID = orderid
		Me.idOrdLine = ordlineid
		Me.ordlinePrtSeq = orderlineprtseq
		Me.ordlineItmCode = orderlineitmcode
		Me.ordlineItmDesc = orderlineitmdesc
		Me.ordlineLineQty = orderlineordqty
		Me.ordlineAllocQty = orderlineallocqty
		Me.ordlineDespQty = orderlinedespqty
		Me.ordlineInvQty = orderlineinvqty
		Me.ordlinePromDelDte = orderlinepromdeldte
		Me.ordlineFullFillMethID = orderlinefullfillmthdid
	End Sub 'New

	Public Property SalesOrderID() As Long
		Get
			Return Me.ordlineOrderID
		End Get
		Set(ByVal Value As Long)
			Me.ordlineOrderID = Value
		End Set
	End Property

	Public Property IDOrderLine() As Long
		Get
			Return Me.idOrdLine
		End Get
		Set(ByVal Value As Long)
			Me.idOrdLine = Value
		End Set
	End Property

	Public Property PrintSequence() As Integer
		Get
			Return Me.ordlinePrtSeq
		End Get
		Set(ByVal Value As Integer)
			Me.ordlinePrtSeq = Value
		End Set
	End Property

	Public Property ItemCode() As String
		Get
			Return Me.ordlineItmCode
		End Get
		Set(ByVal Value As String)
			Me.ordlineItmCode = Value
		End Set
	End Property

	Public Property ItemDescription() As String
		Get
			Return Me.ordlineItmDesc
		End Get
		Set(ByVal Value As String)
			Me.ordlineItmDesc = Value
		End Set
	End Property

	Public Property LineQuantity() As Integer
		Get
			Return Me.ordlineLineQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordlineLineQty = Value
		End Set
	End Property

	Public Property AllocatedQuantity() As Integer
		Get
			Return Me.ordlineAllocQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordlineAllocQty = Value
		End Set
	End Property

	Public Property DespatchQuantity() As Integer
		Get
			Return Me.ordlineDespQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordlineDespQty = Value
		End Set
	End Property

	Public Property InvoicedQuantity() As Integer
		Get
			Return Me.ordlineInvQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordlineInvQty = Value
		End Set
	End Property

	Public Property PromisedDeliveryDate() As Date
		Get
			Return Me.ordlinePromDelDte
		End Get
		Set(ByVal Value As Date)
			Me.ordlinePromDelDte = Value
		End Set
	End Property

	Public Property FullFillmentMethod() As Long
		Get
			Return Me.ordlineFullFillMethID
		End Get
		Set(ByVal Value As Long)
			Me.ordlineFullFillMethID = Value
		End Set
	End Property

End Class ' End SalesOrderLines class

'+++++++++++++++++
			If salesorderIDArray.Count = 0 Then
				tvwNodeFwdOrders = Me.tvwForwardOrders.Nodes.Add("No Forward Orders for Item")
				tvwNodeFwdOrders.Tag = "*"
				tvwNodeFwdOrders.ForeColor = Color.Red
			Else

				For Each salesorders2 In salesorderIDArray

					tvwNodeFwdOrders = Me.tvwForwardOrders.Nodes.Add(salesorders2.SalesOrderDate.ToShortDateString + _
					"  [" + salesorders2.CustomerAccount.PadLeft(8, CChar(" ")) + "]  " + salesorders2.CustomerName.PadRight(30, CChar(" ")) + _
					 " ~ Order: " + salesorders2.SalesOrderNumber.ToString.PadLeft(8, CChar("0")) + _
					 "   Alloc Qty: " + salesorders2.SalesOrderAllocQty.ToString + _
					 "   Fwd Qty: " + salesorders2.SalesOrderFwdQty.ToString + "  [" + salesorders2.SalesOrderStatus.ToString + "]")

					tvwNodeFwdOrders.Tag = salesorders2.IDSalesOrder.ToString()
					tvwNodeFwdOrders.ForeColor = Color.Blue
					'Me.tvwForwardOrders.HideSelection = True

					Dim salesorderline1 As SalesOrderLine

					' Loop through the Order Lines array and add child nodes
					For Each salesorderline1 In salesorders2.SalesOrderLines
						tvwNodeChild = tvwNodeFwdOrders.Nodes(salesorderIDArray.IndexOf(salesorders2)).Nodes.Add("Line: " + salesorderline1.PrintSequence.ToString)
						tvwNodeChild.Tag = salesorderline1.IDOrderLine.ToString
					Next salesorderline1

				Next salesorders2

			End If ' companyArray.Count

Open in new window

0
Comment
Question by:TeDeSm
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
wellhole earned 500 total points
ID: 34888450
Notice this line that you have above your error line:

tvwNodeFwdOrders = Me.tvwForwardOrders.Nodes.Add(salesorders2.SalesOrderDate.ToShortDateString + _
                              "  [" + salesorders2.CustomerAccount.PadLeft(8, CChar(" ")) + "]  " + salesorders2.CustomerName.PadRight(30, CChar(" ")) + _
                               " ~ Order: " + salesorders2.SalesOrderNumber.ToString.PadLeft(8, CChar("0")) + _
                               "   Alloc Qty: " + salesorders2.SalesOrderAllocQty.ToString + _
                               "   Fwd Qty: " + salesorders2.SalesOrderFwdQty.ToString + "  [" + salesorders2.SalesOrderStatus.ToString + "]")


It's reassigning tvwNodeFwdOrders as a child node of the original tvwNodeFwdOrders node. So, if you try to access a child node via tvwNodeFwdOrders.Nodes(salesorderIDArray.IndexOf(salesorders2)) you'll get the exception because tvwNodeFwdOrders does not have a child node at that point.
0
 

Author Comment

by:TeDeSm
ID: 34888609
Thanks for your fresh pair of eyes, I had got 'code blind' and totally missed the error. Perhaps a change in variable names would have helped me here in distinguishing what was the TreeView and the 'New Nodes To Add'.

The below code works.
' Add a root TreeNode for each object in the ArrayList.
			Dim salesorders2 As SalesOrders
			Dim tvwNodeFwdOrders As TreeNode
			Dim tvwNodeChild As TreeNode

			If salesOrderArray.Count = 0 Then
				tvwNodeFwdOrders = Me.tvwForwardOrders.Nodes.Add("No Forward Orders for Item")
				tvwNodeFwdOrders.Tag = "*"
				tvwNodeFwdOrders.ForeColor = Color.Red
			Else

				For Each salesorders2 In salesOrderArray

					' Loop through the Order array and add parent nodes
					tvwNodeFwdOrders = tvwForwardOrders.Nodes.Add(salesorders2.SalesOrderDate.ToShortDateString + _
					 "  [" + salesorders2.CustomerAccount.PadLeft(8, CChar(" ")) + "]  " + salesorders2.CustomerName.PadRight(30, CChar(" ")) + _
					  " ~ Order: " + salesorders2.SalesOrderNumber.ToString.PadLeft(8, CChar("0")) + _
					  "   Alloc Qty: " + salesorders2.SalesOrderAllocQty.ToString + _
					  "   Fwd Qty: " + salesorders2.SalesOrderFwdQty.ToString + "  [" + salesorders2.SalesOrderStatus.ToString + "]")

					tvwNodeFwdOrders.Tag = salesorders2.IDSalesOrder
					tvwNodeFwdOrders.ForeColor = Color.Blue
					'Me.tvwForwardOrders.HideSelection = True

					Dim salesorderline1 As SalesOrderLine

					' Loop through the Order Lines array and add child nodes
					For Each salesorderline1 In salesorders2.SalesOrderLines
						tvwNodeChild = tvwForwardOrders.Nodes(salesOrderArray.IndexOf(salesorders2)).Nodes.Add("Line: " + salesorderline1.PrintSequence.ToString)
						tvwNodeChild.Tag = salesorderline1.IDOrderLine
					Next salesorderline1

				Next salesorders2

			End If ' companyArray.Count

Open in new window

0
 

Author Closing Comment

by:TeDeSm
ID: 34888618
Great help, thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

22 Experts available now in Live!

Get 1:1 Help Now