Results 1 to 34 of 34

Thread: VB6 = ListView.Listitems.Item("Woof") What is .NET equivalent?

Hybrid View

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    VB6 = ListView.Listitems.Item("Woof") What is .NET equivalent?

    OK, in VB6 I would do:
    VB Code:
    1. Dim lvwItem   As ListItem
    2.    Set lvwItem = ListView1.ListItems.Item("Woof")
    in .NET I want to do:
    VB Code:
    1. Dim lvwItem  As ListViewItem
    2.    lvwItem  = Listview1. ?????????
    I feel really daft for asking this

    Woof

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Is it something like this !! What do you want to do exactly (if that didn't answer your question) ?
    VB Code:
    1. Me.ListView1.Items.Item(0).BackColor = Color.Black

  3. #3

  4. #4
    Lively Member nokia8210's Avatar
    Join Date
    Dec 2002
    Location
    Coventry
    Posts
    92
    i think what you want is just like pirate say's but put


    Me.ListView1.Items.Item(0).item("Woof")

    you also need to dim the list view item as new

    you will need to put it in side a loop if you wish to populate the listview with many items this is an example of my code:

    Dim drRow as datarow
    Dim dsData as new dataset
    Dim lstV as listviewitem

    dsData.Tables.Add("Table")

    sqlData.Fill(dsData.Tables("Table"))

    For each drRow in dsData.Tables("Table").rows
    dim lstV = new listviewitem

    lstV.text =drRow.Item("Woof" or "your column name if using dataset")
    lstV.SubItems.Add("Woof Woof" or drRow.Item("Woof" or "your column name if using dataset"))

    listView1.Items.Add(lstV)


    Next


    Hope this helps

  5. #5
    Lively Member nokia8210's Avatar
    Join Date
    Dec 2002
    Location
    Coventry
    Posts
    92
    if you set a interger as woof like this


    dim Woof as int32

    then if you pass a value to the int


    woof=1

    then you could put

    listView1.items.item(woof).item("woof")


    does this help have you got the code you want to use that you could post and i'll see if i can help.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Do you want to set different text to the listviewitem.Text property ?

  7. #7

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    What?!
    That makes no sense what so ever.

    I can't explain this any simpler.

    In VB6 I can get a listitem by it's INDEX by using:
    VB Code:
    1. Msgbox Listview1.ListItems.Item(23).Text
    That will MsgBox the text for the listitem with an index of 23. Does that make sense?
    Now in VB6 you can add a KEY to a listitem so instead of using the Index value, in this case 23, I can use a key ie:
    VB Code:
    1. Msgbox Listview1.ListItems.Item("WOOF").Text
    Now if I do:
    VB Code:
    1. Dim lvwItem1   As ListItem
    2. Dim lvwItem2   As ListItem
    3.    Set lvwItem1 = Listview1.ListItems.Item("WOOF")
    4.    Set lvwItem2 = Listview1.ListIndex.Item(lvwItem1.Index)
    Then lvwItem1 and lvwItem2 are the SAME object.
    I can get at this object using the KEY, in this case "WOOF".

    VB .NET doesn't seem to allow me to use a KEY...I can ONLY use the index, 23...I don't want to do this. I want to assign a Key, "WOOF", to a listitem.

    Does that make sense?

    Woka

  8. #8

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Originally posted by Pirate
    Do you want to set different text to the listviewitem.Text property ?
    Arrrrrghhhh!!!!!!!!!!!!!!!!!!!!!

    NO.

    I want to ACCESS a Listitem using a KEY and NOT the INDEX.

    Arrrrrghghghghhhhh. My head hurts like a very naughty fish

    Woof

  9. #9

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Originally posted by nokia8210
    listView1.items.item(woof).item("woof")
    I am VERY confused.
    A listitem in .NET DOES NOT have an Item property (highlighted in bold)

    Woka

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Wokawidget
    I am VERY confused.
    A listitem in .NET DOES NOT have an Item property (highlighted in bold)
    Yes , no such thing .

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Unfortunately you can no longer reference the ListViewItems by key. As for the Items they just changed the name its now SubItems. The initial Item is a ListViewItem which contains a collection of SubItems of type ListViewItemSubItem.

  12. #12
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Don't know for certain, but there's no mention of a key in "Mastering Visual Basic .Net", by Petroutsos.

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Hopefully , this is what you're looking after . I'm working in a buffymode so sorry if this doen't help

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal
    2. e As System.EventArgs) Handles Button1.Click
    3.         Dim listvItem As New ListViewItem("something")
    4.  
    5.         If Me.ListView1.Items.IndexOf(listvItem) Then
    6.             'It's there and do your stuff
    7.  
    8.         Else
    9.             'It's not existed and do something else
    10.  
    11.         End If
    12.     End Sub

  14. #14

  15. #15
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Afraid not Woka, now that the listview is made of objects (well in this case just a string) its a basic array (IList actually) and you can only access it by its Index.

    You can however add the items to another collection that does support key values, and add the listitem to the collection with the key.

    This is mostly from my head so be warned, but you should get the idea.

    Code:
     'At form header
     Private DocumentList As New Collection()
     
     Friend Sub procAddItem(MyClassInfo as MyClass)
    
      'Create the listitem
      Dim lsvCurrItem As New ListViewItem()
      'Put the data inside it
      lsvCurrItem.Text = MyClassInfo.ToString
    
      'Add it to the listview control
      lsvDocumentList.Items.Add(lsvCurrItem)
    
      'Add the listviewitem to the collection as well
      DocumentList.Add(lsvCurrItem, "MyNewKey)
    
      'Lose local ref (just for test)
      lsvCurrItem = Nothing
    
      'Rather then get the listview outta the control, get it outta the collection (its byref, the same object)
      lsvCurrItem = DocumentList.Item("MyNewKey")
     
      'Change some more values, they will get updated in the listviewcontrol
      lsvCurrItem.Text = "The changed values"
     End Sub
    You may think, why the hell ?? Like I did . Its a pain in the arse for when you want a simple list, but is great when you go more complex. You can have a list of all the items that could be in the collection, and only show the ones that you need (filtered) in the actual control.


    Just so you know, when you go to use the listbox, you just add the whole object instead of just the text and it will use the .ToString function of the object to display as the text. Its quite funky, and a bit OT but it was the same wall I was hitting when I first started.

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    To explicitly find an item by text yes. Depending on what it is you want to do there may be other shortcuts as well. For instance any selected items can also be found in the ListView1.SelectedItems collection. Also maintaining a reference to the actual item object may be the way to go depending on what you are doing.

  17. #17

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    OK, here's a small project of what I have been trying to do.
    It works fine.
    Is this correct .NET programming, or am I using some crappy methods and techneques???

    Comments much appreciated.



    Woka
    Attached Files Attached Files

  18. #18
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    One comment about Item property : You overloaded it though they have the same signature and return type which is against overloading rules . I know it works but doesn't follow the general rule of overloading .

    The other thing , You used member const 'vbNullString' which is for compatibility with VB6 converted proj . If MS will not support this namespace (Microsoft.VisualBasic) in the future , then you have to rewrite that code in pure .NET way like this :

    VB Code:
    1. If pstrKey = String.Empty Then
    2.         tvwData.SelectedNode = Nothing
    3.  Else
    4.  
    5.  
    6. 'or simply
    7.  
    8. If pstrKey = "" Then
    9.             tvwData.SelectedNode = Nothing
    10. Else

    Other than that , I really like the way you code classes .

  19. #19
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Look up the function DirectCast rather then Ctype

  20. #20

  21. #21

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Originally posted by Pirate
    One comment about Item property : You overloaded it though they have the same signature and return type which is against overloading rules . I know it works but doesn't follow the general rule of overloading .
    OK...but I want it to return the same type of object. Same signiture??? Explain.
    I am really new to .NET

    Woka

  22. #22
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Method signature is the paramter list of a method . Do both of the Item properites return same obj and same value is that what you mean ?

  23. #23

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    I need to be able to access the Item bu either it's numberical index in the collection AND by it's Key, ie "WOOF".
    Yes...both item props MUST return the same type of object.
    My params are different for both properties, or are you on about the naming of the passed in param, ie shoulkd it be:
    VB Code:
    1. Public Overloads ReadOnly Property Item(ByVal KeyIndex As String) As clsKeyIndex
    2.         Get
    3.             Dim objKeyIndex As clsKeyIndex
    4.             For Each objKeyIndex In list
    5.                 If objKeyIndex.Key = KeyIndex Then
    6.                     Return objKeyIndex
    7.                 End If
    8.             Next
    9.             Return DirectCast(list.Item(list.Count + 1), clsKeyIndex)
    10.         End Get
    11.     End Property
    12.  
    13.     Public Overloads ReadOnly Property Item(ByVal NumericIndex As Integer) As clsKeyIndex
    14.         Get
    15.             Return DirectCast(list.Item(NumericIndex), clsKeyIndex)
    16.         End Get
    17.     End Property
    is that correct?

    Woka

  24. #24
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It seems you're messing up with some versions of your program . I've quite different code . This is what I'm referring to .

    VB Code:
    1. Public Overloads ReadOnly Property Item(ByVal Index As String) As clsKeyIndex
    2.         Get
    3.             Dim objKeyIndex As clsKeyIndex
    4.             For Each objKeyIndex In list
    5.                 If objKeyIndex.Key = Index Then
    6.                     Return objKeyIndex
    7.                 End If
    8.             Next
    9.             Return CType(list.Item(list.Count + 1), clsKeyIndex)
    10.         End Get
    11.     End Property
    12.  
    13.     Public Overloads ReadOnly Property Item(ByVal Index As Integer) As clsKeyIndex
    14.         Get
    15.             Return CType(list.Item(Index), clsKeyIndex)
    16.         End Get
    17.     End Property

    errrr ,
    f-ucking edit [ I must be blind] I didn't see the other Item's param as Integer . I thought it's string . I'm working on two projs at the same time . I'm so sorry Wokawidget . . You're right . Your code is just perfect .

  25. #25

  26. #26
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Wokawidget
    Hahahaha That's OK.
    So is it OK to use INDEX as the varible name for BOTH the parameters?
    Perfectly legal .

  27. #27

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Would this be viable:
    VB Code:
    1. Public Overloads Function Add(ByVal Username As String) As User
    2.         'blah blah blah
    3.     End Function
    4.  
    5.     Public Overloads Function Add(ByVal Username As String, ByVal Email As String, ByVal Telephone As String) As User
    6.         'blah blah blah
    7.     End Function
    Or should I use:
    VB Code:
    1. Public Function Add(ByVal Username As String, Optional ByVal Email As String, Optional ByVal Telephone As String) As User
    2.        'blah blah blah
    3.     End Function
    ???

    WOka

  28. #28
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's what overloading was created for . I can't say which is which good in your situation . It really depends on the functionality you're implementing . Suppose you used the first one with only one parameter (Username) , then will you need to set/get any other data like email or telephone . What I mean is do they serve the same purpose and do the same job but at the same time they work in a polymophically manner . I might be missing your point though (as usual ) . Is this what you're asking about !!

  29. #29

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    OK...I have added to classes. User and Users.
    Have I used .NET code correctly so far?
    Or am I following VB6 methods?

    Am really greatful for your help.

    Woka
    Attached Files Attached Files

  30. #30
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Reflecting back to your previous example here is one with two alternate ways of doing the samething. One is basically the same except it stores the relationships between the node and listviewitems in a hashtable instead of dealing directly with the index. The other stores the item in the tag of the nodes/items and loop selects the matching control. These ways aren't any better they just show different ways .NET lets you do it.

  31. #31
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Re: VB6 = ListView.Listitems.Item("Woof") What is .NET equivalent?

    Originally posted by Wokawidget
    OK, in VB6 I would do:
    VB Code:
    1. Dim lvwItem   As ListItem
    2.    Set lvwItem = ListView1.ListItems.Item("Woof")
    in .NET I want to do:
    VB Code:
    1. Dim lvwItem  As ListViewItem
    2.    lvwItem  = Listview1. ?????????
    I feel really daft for asking this

    Woof
    Shame on you all, you all laugh at me for asking daft questions when i was a n00b, how do you feel now

  32. #32

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    I need a new captain. From now on, you'll be known as 'Wiggles'

    I feel great
    I have learnt loads of .NET in a day and can now write full apps that almost have the same functionality as my old VB6 apps did

    In regards to Db applications that use large class structures anyways.

    What do you mean laugh at you when you were a nOOb? We all still laugh at you now Hahahahaha WOOF

    Edneeis, cheers. Will look at that at work tomorrow. Knew about the object in tag thing. Not to sure when I would use that as I prefer the method I have now...although, I could add the node into the tag of the listview...Ooooo...now there's a thought...and the listitem as the tag of the node...Hmmmm...wouldn't that create a circular loop?

    hmmm...will check it out tomorrow.

    Thanks again and fish fingers, chips (fries) and beans to you all.

    Wiggles

  33. #33

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    I like the hash tables.
    You example of the lopping method also strengthened my knowledge on Overloads. Cheers.

    No code for splitter bar
    People are too lazy now-a-days

    Woka

  34. #34
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    i was afraid of people laughing at me

    now i need a new project, any ideas?

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