|
-
Aug 13th, 2011, 11:10 PM
#1
Thread Starter
New Member
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:
Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
If ListView2.FocusedItem.Selected = True Then
Dim ListView2Selected As ListViewItem = ListView2.SelectedItems(0)
MsgBox(ListView2Selected.ToString)
ElseIf ListView1.FocusedItem.Selected = True Then
Dim ListView1Selected As ListViewItem = ListView1.SelectedItems(0)
[INDENT][/INDENT]Msgbox(ListView1Selected.ToString)
Else
MsgBox("notting selected")
End If
End Sub
-
Aug 14th, 2011, 12:22 AM
#2
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:
Public Class Form1
Dim selectedListview As ListView
Private Sub ListViews_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Enter, ListView2.Enter
selectedListview = DirectCast(sender, ListView)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If selectedListview.SelectedItems.Count > 0 Then
MsgBox(selectedListview.SelectedItems(0).Text)
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2011, 01:48 AM
#3
Thread Starter
New Member
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.
-
Aug 14th, 2011, 01:55 AM
#4
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:
ListView1.Tag = "folderName"
or:
vb Code:
ListView2.Tag = "folderName"
then:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If selectedListview.SelectedItems.Count > 0 Then
MsgBox(io.path.combine(selectedListview.tag.tostring, selectedListview.SelectedItems(0).Text))
End If
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|