Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listview 1

Status
Not open for further replies.

brews

Technical User
Dec 12, 2007
194
US
Have loaded a listview and want to return the record number from the selected index. Have tried this but it returns only a 0
Code:
Private Sub lvwDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwDisplay.Click
        MsgBox(CInt(lvwDisplay.SelectedItems(0).Text))
End Sub

Any help is great. thank you.
 
Something like this:

Code:
	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		'Load the ListView
		Dim lvi As ListViewItem = Nothing
		For a As Integer = 0 To 9
			lvi = New ListViewItem("Item " + a.ToString)
			lvi.SubItems.AddRange(New String() {"Sub Item 1", "Sub Item 2", "Sub Item 3"})
			ListView1.Items.Add(lvi)
		Next
		ListView1.MultiSelect = False

	End Sub

	Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click

		[b]Dim index As Integer = ListView1.SelectedIndices(0)[/b]
		Dim sb As New System.Text.StringBuilder[b]("Item at index: " + index.ToString + Environment.NewLine)
		With ListView1
			sb.Append(ControlChars.Tab + .Items(index).Text + Environment.NewLine)[/b]
			sb.Append(ControlChars.Tab + ControlChars.Tab + .Items(index).SubItems(1).Text + Environment.NewLine)
			sb.Append(ControlChars.Tab + ControlChars.Tab + .Items(index).SubItems(2).Text + Environment.NewLine)
			sb.Append(ControlChars.Tab + ControlChars.Tab + .Items(index).SubItems(3).Text)
		End With

		MessageBox.Show(sb.ToString)

	End Sub

I've highlighted the parts I think you need.

Hope this helps.

[vampire][bat]
 
Are you populating the listview with records from a database?

If so, just store the RecordID in the .Tag property of the ListViewItem.

'Populate ListView

Code:
Dim lvwItem as new ListViewItem
lvwItem.Text = myDataReader("Field1")
lvwItem.Tag = myDataReader("RecordID")
lvwMain.Items.Add(lvwItem)

'Getting Selected RecordID
Code:
    Private Sub lvwMain_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwMain.SelectedIndexChanged
         Msgbox(lvwMain.SelectedItems.Item(0).Tag)
    End Sub
 
Thanks for the help. in one case I used .text and it works but not in this case. Used .tag and it works.

Earth and fire, thanks for the effort. I was not complete enuf in my cry for help.
 
Maybe it works and maybe .... The problem is this:
Code:
    Private Sub lvwDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwDisplay.Click, lvwDisplay.SelectedIndexChanged

        Try
            iID = CInt(lvwDisplay.SelectedItems(0).Tag)
           ' get data here using ID and fill txtboxes
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
The first time through it works but each succeeding time it runs the proc twice with the first time showing an error of invalid argument=value of '0' where iID = 0 the first time through. Clicking ok and it performs as it should where iID picks up the correct tag number.

 
Having had similar problems using SelectedItems, that was why I suggested using SelectedIndices - I've found it more realiable, although I have no idea why.


Hope this helps.

[vampire][bat]
 
I've never had that problem. If I deal with a single select instance at any point I make sure the multi select is false. Brews may have already done so, but that is my only suggestion.

It could also be due to the click event. If I remember there is a selection change event. I've found, when available, that is the better event for selected items.

-I hate Microsoft!
-Forever and always forward.
 
I tried selectedindices and it did not return the correct record. Also used the change event and same results. Seems that for now the only solution is to use a try/catch and leave the catch blank?

Thanks for the help.
 
If you did that and it is still happening it is likely you have something else going on. Try:

Code:
Try
            Dim CurrentLVI As ListViewItem
            CurrentLVI = lvwDisplay.SelectedItems(0)
            iID = CurrentLVI.Tag
           ' get data here using ID and fill txtboxes
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Do a break on iID = CurrentLIV.Tag and check the info in CurrentLVI. Does the text of the ListViewItem returned match the text of the record it should be assigned to?

-I hate Microsoft!
-Forever and always forward.
 
Sorwen,

Put the stop on iID = Currentlvi.tag
CurrentLVI = lvwDisplay.SelectedItems(0) says {text=""}
CurrentLVI.tag in the following line ="35"{String}
Next line then iID = currentlvi.tag

Is this correct?
 
Did you try it a couple times and now it is always coming back with the correct record number in .Tag? You have listview items with no text? Is this done on purpose or could additional items some how have been added? If this is on purpose what are you actually using the listview for because really there should always be some text or how do visually differentiate one item from another so users know which they want to select?

-I hate Microsoft!
-Forever and always forward.
 
When loading the listview I was formatting the first column:
Code:
For Each CR In c
            sKey = Format(CR.ID, "")
            mItem = New ListViewItem
            mItem.Text = CStr(sKey)
            mItem.SubItems.Add(CR.Name)
            lvwDisplay.Items.Add(mItem)
Next CR
which caused the skey not to load but the name loaded. Upon removing the format so that sKey = CR.ID, CurrentLVI = the skey but there is nothing ("") in (CurrentLVI.Tag). Although both columns load.
 
Ok. Lets back up a bit. You will need to show and explain more about your code. Where does c (in the above) come from and what type (object, collection, dataset, etc) is it? Where is the line and sorounding code for the initial setting of the tag? If for some reason you can't show the actual code (it happens) still need as close as possible because there is too much missing at this point to tell you for sure where your problem is.

-I hate Microsoft!
-Forever and always forward.
 
c is a collection used to populate the listview:
Code:
        With lvwDisplay
            .Show()
            .Items.Clear()
            .Columns.Add("ID", 15)
            .Columns.Add("Click on Name", 150)
            .Refresh()
            .View = View.Details
        End With
        LoadItems()
    End Sub

    Public Sub LoadItems()
        Dim c As New Collection
        lvwDisplay.Items.Clear()
        Dim sKey As String
        dataMgr.DisplayAll(c)
        Dim mItem As ListViewItem
        For Each CR In c
            sKey = CR.ID
            mItem = New ListViewItem
            mItem.Text = CStr(sKey)
            mItem.SubItems.Add(CR.Name)
            lvwDisplay.Items.Add(mItem)
        Next CR
    End Sub
When the listview is clicked:
Code:
Try
            Dim CurrentLVI As ListViewItem
            CurrentLVI = lvwDisplay.SelectedItems(0)
            iID = CInt(CurrentLVI.Tag)
            dataMgr.FindName(iID, CR)
            FillBoxes()
Catch ex As Exception
            MessageBox.Show(ex.Message)
End Try
 
Well first you don't seem to have any place where you set the tag so I don't see how it ever works.

Change to this.
Code:
Public Sub LoadItems()
        Dim c As New Collection
        lvwDisplay.Items.Clear()
        Dim sKey As String
        dataMgr.DisplayAll(c)
        
        For Each CR In c
            Dim mItem as New ListViewItem
            sKey = CR.ID
            mItem = New ListViewItem
            mItem.Tag = sKey
            mItem.Text = sKey
            mItem.SubItems.Add(CR.Name)
            lvwDisplay.Items.Add(mItem)
        Next CR
    End Sub
The with sKey already a string you don't need to CStr() it. If you put something like that any where you would want to put it were sKey is first used.

If that isn't the fix then more code. For instance I assume CR is declared globally because it is used in both sets of code? Or are you reusing the variable and not showing the type assignment? Or worse yet you don't have Explicit set to On? If it is off turn it on.

-I hate Microsoft!
-Forever and always forward.
 
Code:
mItem = New ListViewItem
Sorry, I missed read that part was even there. So you would only want to add:
Code:
mItem.Tag = sKey
to your code.


-I hate Microsoft!
-Forever and always forward.
 
CR is part of a class. After all of this and not being able to get it work, a listbox is used instead of a listview. And for this purpose it does the job.

Many Thanks to all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top