The .NET ListView control differs from the VB6 ListView control for an important detail: assignments to the ListView.SelectedItem property are ignored if the control isn’t visible. For this reason, assignments to this property that are performed before the form becomes visible – for example, inside the Form_Load event – have no effect and leave the property set to Nothing:
Private Sub Form_Load() Handles MyBase.Load
ListView1.SelectedItem = ListView1.Items(1)
End Sub
This behavior often causes a NullReference exception later in the execution, and it isn’t always easy to understand what the real cause of the exception is.
The ListView control in VB Migration Partner’s support library inherits this behavior from the .NET control. However, to help developers to correctly diagnose the cause of spurious NullReference exceptions, the ListView.SelectedItem property throw an exception if the control is currently invisible. The exception message is:
Can’t set the SelectedItem property at this time.
The simplest solution to this problem is moving the code that assign the SelectedItem to an event handler that runs when the form (and the control) is visible, for example in the Form_Activate event hander.