Results 1 to 4 of 4

Thread: If Loop and FocusedItem.Selected

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    6

    Question If Loop and FocusedItem.Selected

    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: If Loop and FocusedItem.Selected

    the problem is the focus changes when you click the button. supposing listview1 is focussed + you click your button. listview1 is deselected + button1 is focussed. try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim selectedListview As ListView
    4.  
    5.     Private Sub ListViews_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Enter, ListView2.Enter
    6.         selectedListview = DirectCast(sender, ListView)
    7.     End Sub
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         If selectedListview.SelectedItems.Count > 0 Then
    11.             MsgBox(selectedListview.SelectedItems(0).Text)
    12.         End If
    13.     End Sub
    14.  
    15. End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    6

    Re: If Loop and FocusedItem.Selected

    Thanks Paul, That is great.

    But the two ListView lists files in different folders.

    ListView1 list files in lets say C:\example\listview1 and ListView2 C:\example\listview2.

    The Folder location is a variable.

    How would I get the Msgbox to display the full location?

    If an item in ListView1 is selected I want to display in a MsgBox C:\example\listview1\ITEMSELECTED.TXT and If an item in ListView2 is selected I want to display in a MsgBox C:\example\listview2\ITEMSELECTED.TXT

    Thanks.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: If Loop and FocusedItem.Selected

    when you load your filenames in to your listviews, put the path in the listview's .tag property:

    vb Code:
    1. ListView1.Tag = "folderName"

    or:

    vb Code:
    1. ListView2.Tag = "folderName"

    then:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     If selectedListview.SelectedItems.Count > 0 Then
    3.         MsgBox(io.path.combine(selectedListview.tag.tostring, selectedListview.SelectedItems(0).Text))
    4.     End If
    5. End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width