简体   繁体   中英

vb.6.0 lvwtag to vb.net 2010

Im new at Vb.net 2010. im using vb 6.0. Can anyone help me convert this code to vb.net 2010?

Dim SkeyBracket as ingeter
Dim xList As ListItem
If sKeyBracket <> 0 Then
    Set xList = lvBracket.FindItem(CStr(sKeyBracket), lvwTag)
Else
    Set xList = lvBracket.ListItems(1)
End If

According to this MSDN Link ( It discusses VBA, but VB6 is very similar ) and in looking at the documentation for the VB6 ListView.FindItem and .net ListViewFindItemWithText Method you will need to look more closely at the context of your VB6 code in order to determine the best fit.

Set Keyword. In VBA, the Set keyword is necessary to distinguish between assignment of an object and assignment of the default property of the object. Since default properties are not supported in Visual Basic .NET, the Set keyword is not needed and is no longer supported .

So in your case I would do something like:

Dim SkeyBracket as integer
Dim xList As ListViewItem

If sKeyBracket <> 0 Then
    xList = lvBracket.FindItem(CStr(sKeyBracket), lvwTag)
Else
    xList = lvBracket.ListItems(1)
End If

You are out of luck, listviews in .Net do not have an equivalent method. The good news is that it is relatively trivial to write your own. If you are only using it in one place, you could use LINQ to do so without having to declare your own method, otoh if you are using it a lot, then an extension method would allow you to "add" it to the listview class.

While the ListView class does not contain a FindItem method or equivalent method that searches by a ListItem's Tag property, it is easy to do the equivalent using LINQ (Cast is used to make the list item collection an ienumerable)

 lvBracket.Items.Cast(Of ListItem).FirstOrDefault(
               Function(li) Object.Equals(li.Tag, CStr(sKeyBracket))

Or using VB's null coalesence operator (the one called with 2 arguments):

 xList = If(lvBracket.Items.Cast(Of ListItem).FirstOrDefault(
               Function(li) Object.Equals(li.Tag, CStr(sKeyBracket)),
            lvBracket.Items(1))

Although I really suspect you meant lvBracket.Items(0) ...

If you want it as a method, then an extension method is easy enough to write:

Public Function FindByTag(this as ListView, tagStr as String) As ListItem
    Return this.Items.Cast(Of ListItem).FirstOrDefault(
               Function(li) CStr(li.Tag) = tagStr)
End Function

And used as lvBracket.FindByTag(CStr(sKeyBracket))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM