Hello,

I have being trying to learn VB.net in my own time.
I am at this small project now for months and it is driving me up the walls.

Please bear with me.

In the project I have two ListBox's.

I want to click a button 'cmd1' and have the hightlight item name in one of the two ListBox's be passed to a MsgBox.

Hopefully I can explain it better though pseudocode:

IF ListView1 Focused = True DO
VAR ListView1Selected = ListView1 Focused Item Selected
OUTPUT ListView1Selected

ELSEIF ListView2 Focused = True DO
VAR ListView2Selected = ListView2 Focused Item Selected
OUTPUT ListView2Selected

ELSE
OUTPUT "Notting"]
The problem I am having is this:

When I have a item selected in ListView2 and click the button, I get this error: "NullReferenceException was unhandled".
It doesn't matter if i swap the code around like below, I still get the same error.


IF ListView2 Focused = True DO
VAR ListView2Selected = ListView2 Focused Item Selected
OUTPUT ListView2Selected

ELSEIF ListView1 Focused = True DO
VAR ListView1Selected = ListView1 Focused Item Selected
OUTPUT ListView1Selected

ELSE
OUTPUT "Notting"]

Here is the full sub to look at. See what you can make of it:


Full Code Code:
  1. Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
  2.  
  3.             If ListView2.FocusedItem.Selected = True Then
  4.  
  5.                 Dim ListView2Selected As ListViewItem = ListView2.SelectedItems(0)
  6.  
  7.               MsgBox(ListView2Selected.ToString)
  8.  
  9.             ElseIf ListView1.FocusedItem.Selected = True Then
  10.                 Dim ListView1Selected As ListViewItem = ListView1.SelectedItems(0)
  11.                  
  12. [INDENT][/INDENT]Msgbox(ListView1Selected.ToString)
  13.                
  14.             Else
  15.                 MsgBox("notting selected")
  16.             End If
  17. End Sub