VB Migration Partner

KNOWLEDGE BASE - Forms and controls

Previous | Index | Next 

[PRB] Assignments to the ListView.SelectedItem property are ignored if the control isn’t visible

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)  ' <== this causes an exception
        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.

Previous | Index | Next 

Follow Francesco Balena on VB6 migration’s group on


Read Microsoft Corp’s official case study of a VB6 conversion using VB Migration Partner.

Code Architects and its partners offers remote and onsite migration services.

More details

Subscribe to our free newsletter for useful VB6 migration tips and techniques.


To learn more about your VB6 applications, run VB6 Analyzer on your source code and send us the generated text file. You will receive a detailed report about your VB6 applications and how VB Migration Partner can help you to quickly and effectively migrate it to .NET.

Get free advice

A fully-working, time-limited Trial Edition of VB Migration Partner allows you to test it against your actual code

Get the Trial

The price of VB Migration Partner depends on the size of the VB6 application, the type of license, and other factors

Request a quote

Migrating a VB6 application in 10 easy steps

Comparing VB Migration Partner with Upgrade Wizard

Migration tools: Feature Comparison Table

All whitepapers