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"))
In VB6 I can get a listitem by it's INDEX by using:
VB Code:
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:
Msgbox Listview1.ListItems.Item("WOOF").Text
Now if I do:
VB Code:
Dim lvwItem1 As ListItem
Dim lvwItem2 As ListItem
Set lvwItem1 = Listview1.ListItems.Item("WOOF")
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.
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.
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.
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.
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???
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:
If pstrKey = String.Empty Then
tvwData.SelectedNode = Nothing
Else
'or simply
If pstrKey = "" Then
tvwData.SelectedNode = Nothing
Else
Other than that , I really like the way you code classes .
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
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:
Public Overloads ReadOnly Property Item(ByVal KeyIndex As String) As clsKeyIndex
Public Overloads ReadOnly Property Item(ByVal Index As Integer) As clsKeyIndex
Get
Return CType(list.Item(Index), clsKeyIndex)
End Get
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 .
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 !!
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.
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.