Listview - Problems with with listview.selectedindexchanged

  • Thread starter Thread starter Kepano_DK
  • Start date Start date
K

Kepano_DK

Guest
I have a listview (named lvMaterials), i populate with data from a XML-file. Currently I have one column, and I have set multiselect=false (as the listview is used to select the needed material for a workpiece. the selection triggers a second listview to be filled with data).

When the form loads, and the data is added to the listview, I want my code to select (and highlight) the item that fits a specific value (the current material assigned to the workpiece).

A second listview (lvDimensions) is filled with data based on the selection from lvMaterials. In this, I also want to select and highlight a specific value.

The code works only in the scenario where the userform is loaded. When I try to select a new value in lvMaterials, I get an error:

"System.NullReferenceException: 'Objektreferencen er ikke indstillet til en forekomst af et objekt.'

System.Windows.Forms.ListView.FocusedItem.get returned Nothing."

The error must relate to

LvMaterials.FocusedItem.Text

under the itemselectchanged, but I can't figure out how to solve it.


Code that selects items in the listviews:

Private Sub LoadMaterial()
' On Error Resume Next
'--- Declare Custom iProperties
Dim customPropSet As PropertySet = SharedVariables.oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim TypeCat As String = customPropSet.Item("Type_Category").Value
TypeCat = TypeCat.Replace(" ", "")


'---------------------------- Load XML file ------------------------------------'
'Filename with materials: xmlMatLibrary
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("D:\Autodesk\Robot Kinematics\XML\xmlMatLibrary.xml")
Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/materiallibrary/material")

'Fill data into lvMaterials:
For Each node As XmlNode In nodes
If node.Attributes("template").InnerText = TypeCat Then
If LvMaterials.FindItemWithText(node.SelectSingleNode("inventormaterial").InnerText) Is Nothing Then
LvMaterials.Items.Add(node.SelectSingleNode("inventormaterial").InnerText)
End If
End If
Next

Dim ProfType As String = customPropSet.Item("Profile_Type").Value
Dim matName As String = SharedVariables.oDoc.ComponentDefinition.Material.Name
Dim searchMat As ListViewItem = LvMaterials.FindItemWithText(matName)

If Not searchMat Is Nothing Then
searchMat.Focused = True
searchMat.Selected = True
searchMat.EnsureVisible()
LvMaterials.Select()
End If

If Not LvDimensions.FindItemWithText(ProfType) Is Nothing Then
Dim searchDim As ListViewItem = LvDimensions.FindItemWithText(ProfType)
searchDim.Focused = True
searchDim.Selected = True
searchDim.EnsureVisible()
LvDimensions.Select()
End If



Private Sub LvMaterials_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles LvMaterials.ItemSelectionChanged
'This code triggers, when a new index is selected
' On Error Resume Next

' LvMaterials.SelectedItems.Clear()

Dim xmlDoc As New XmlDocument()
xmlDoc.Load("D:\Autodesk\Robot Kinematics\XML\xmlMatLibrary.xml")
Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/materiallibrary/material")

LvDimensions.Items.Clear()

For Each node As XmlNode In nodes

If node.SelectSingleNode("inventormaterial").InnerText = LvMaterials.FocusedItem.Text And node.Attributes("template").InnerText = "RectangularMassive" Then
Dim newType As New ListViewItem(node.SelectSingleNode("type").InnerText)
newType.SubItems.Add(node.SelectSingleNode("pricecategory").InnerText)
LvDimensions.Items.Add(newType)
End If
Next

' ListView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
End Sub



The fix to get my code running is to clear the selection when the lvMaterials.itemselectedChanged (or lvMaterials.selectedindexchanged) with this line:

LvMaterials.SelectedItems.Clear()


The problem with this tho, is that I can't highlight the items in the listviews:

I hope you can see where I make the error, as the highlights for the selected materials are important for the users.


I would have posted, pictures, but I need validation.

Continue reading...
 

Similar threads

D
Replies
0
Views
138
dianas28
D
P
Replies
0
Views
112
Paulywog0667_Laptop
P
M
Replies
0
Views
149
muhammadanzar
M
Back
Top