vb.net listview equivalent to VB6 listindex
I am trying to get the following VB6 listindex to work within my vb.net code:
setTheR CStr(payReq.ItemData(payReq.ListIndex))
But if i copy and paste that into VB.net it wont accept it.
This is what VB.net did with the converting of the VB6 to .net code:
strContract = payReq.Items.Item(payReq.FocusedItem.Index).Text
However, checking that value it returns the name instead of the index. While the VB6 code returns the value of 2311 (which is what it needs to return)
When i add items to the listview i do this:
Item = payReq.Items.Add(rsPayRequests.Fields("userid").Value)
Item.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, VB6.Format(rsPayRequests.Fields("reqdatetime").Value, "mm/dd/yyyy")))
But i noticed it does this as well:
payReq.Items.Add(New VB6.ListBox开发者_如何学CItem(Item, rsPayRequests.Fields("requestNum").Value))
But that does not work with my listview in .net since that above is a listbox and not a listview. Is there an equivalent in .net for the listbox to have a custom index?
Any help would be great!
David
Try strContract = lstPayRequest.FocusedItem.Index
The way you are using it is returning the item at that index
Edit:
To answer your question you can add subitems to a listviewitem or you could use the tag property of the ListViewItem for your custom index.
Dim lv As New ListViewItem
lv.Text = "Item1"
lv.Tag = 1001
lv.SubItems.Add("SubItem1")
lv.SubItems.Add("SubItem2")
lstPayRequests.Items.Add(lv)
Assuming I've read this right, you're doing what many a VB6 programmer did. You're storing a relevant value in the ItemData field that isn't the index but is related to the item. Very common practice in VB6.
Unfortunately, that practice is not directly supported in VB.NET. The VB.NET list box does not have the concept of ItemData thus a direct conversion of the VB6 code is not possible. The only solution I've encountered for this is to create a class based on the ListViewItem class. It could have a display name and an item data property. Then when you add items to the list, you create your custom item class, populate the properties and add that instance to the list. Then you can retrieve the item data value by casting the selected item into your custom item class.
It's a lot of work to replicate built-in VB6 functionality but it's the only option I've seen. Hopefully someone has a better answer to this question and I'll learn too.
精彩评论