OK, in VB6 I would do:
in .NET I want to do:VB Code:
Dim lvwItem As ListItem Set lvwItem = ListView1.ListItems.Item("Woof")
I feel really daft for asking this :(VB Code:
Dim lvwItem As ListViewItem lvwItem = Listview1. ?????????
Woof
OK, in VB6 I would do:
in .NET I want to do:VB Code:
Dim lvwItem As ListItem Set lvwItem = ListView1.ListItems.Item("Woof")
I feel really daft for asking this :(VB Code:
Dim lvwItem As ListViewItem lvwItem = Listview1. ?????????
Woof
Is it something like this !! What do you want to do exactly (if that didn't answer your question) ?
VB Code:
Me.ListView1.Items.Item(0).BackColor = Color.Black
Errr...sort of, but instead of the 0 as the index I want to use a key like "woof"
As far as I can see .NET only allows you to use an index (integer) :(
Woka
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
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.
Do you want to set different text to the listviewitem.Text property ?
What?! :confused:
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:
That will MsgBox the text for the listitem with an index of 23. Does that make sense?VB Code:
Msgbox Listview1.ListItems.Item(23).Text
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:
Now if I do:VB Code:
Msgbox Listview1.ListItems.Item("WOOF").Text
Then lvwItem1 and lvwItem2 are the SAME object.VB Code:
Dim lvwItem1 As ListItem Dim lvwItem2 As ListItem Set lvwItem1 = Listview1.ListItems.Item("WOOF") Set lvwItem2 = Listview1.ListIndex.Item(lvwItem1.Index)
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
Arrrrrghhhh!!!!!!!!!!!!!!!!!!!!! :(Quote:
Originally posted by Pirate
Do you want to set different text to the listviewitem.Text property ?
NO.
I want to ACCESS a Listitem using a KEY and NOT the INDEX.
Arrrrrghghghghhhhh. My head hurts like a very naughty fish :D
Woof
I am VERY confused.Quote:
Originally posted by nokia8210
listView1.items.item(woof).item("woof")
A listitem in .NET DOES NOT have an Item property (highlighted in bold)
Woka
Yes , no such thing .Quote:
Originally posted by Wokawidget
I am VERY confused.
A listitem in .NET DOES NOT have an Item property (highlighted in bold)
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.
Don't know for certain, but there's no mention of a key in "Mastering Visual Basic .Net", by Petroutsos.
Hopefully , this is what you're looking after . I'm working in a buffymode so sorry if this doen't help :(
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim listvItem As New ListViewItem("something") If Me.ListView1.Items.IndexOf(listvItem) Then 'It's there and do your stuff Else 'It's not existed and do something else End If End Sub
Ok. Cheers. So it's not possible. Bad .NET *slap*
So to find an item I would have to scroll through all the items and check the TEXT property?
Woka
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.
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.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
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???
Comments much appreciated.
:D
Woka
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 . :)
Look up the function DirectCast rather then Ctype
why?
Woka
OK...but I want it to return the same type of object. Same signiture??? Explain.Quote:
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 .
I am really new to .NET
Woka
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 ? :confused:
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:
is that correct?VB Code:
Public Overloads ReadOnly Property Item(ByVal KeyIndex As String) As clsKeyIndex Get Dim objKeyIndex As clsKeyIndex For Each objKeyIndex In list If objKeyIndex.Key = KeyIndex Then Return objKeyIndex End If Next Return DirectCast(list.Item(list.Count + 1), clsKeyIndex) End Get End Property Public Overloads ReadOnly Property Item(ByVal NumericIndex As Integer) As clsKeyIndex Get Return DirectCast(list.Item(NumericIndex), clsKeyIndex) End Get End Property
Woka
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:
Public Overloads ReadOnly Property Item(ByVal Index As String) As clsKeyIndex Get Dim objKeyIndex As clsKeyIndex For Each objKeyIndex In list If objKeyIndex.Key = Index Then Return objKeyIndex End If Next Return CType(list.Item(list.Count + 1), clsKeyIndex) End Get End Property Public Overloads ReadOnly Property Item(ByVal Index As Integer) As clsKeyIndex Get Return CType(list.Item(Index), clsKeyIndex) End Get End Property
errrr , :blush: :blush:
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 . :D . You're right . Your code is just perfect .
Hahahaha :D That's OK.
So is it OK to use INDEX as the varible name for BOTH the parameters?
Or should they be named differently?
Woof
Perfectly legal . :)Quote:
Originally posted by Wokawidget
Hahahaha :D That's OK.
So is it OK to use INDEX as the varible name for BOTH the parameters?
Would this be viable:
Or should I use:VB Code:
Public Overloads Function Add(ByVal Username As String) As User 'blah blah blah End Function Public Overloads Function Add(ByVal Username As String, ByVal Email As String, ByVal Telephone As String) As User 'blah blah blah End Function
???VB Code:
Public Function Add(ByVal Username As String, Optional ByVal Email As String, Optional ByVal Telephone As String) As User 'blah blah blah End Function
WOka
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 !!
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
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.
Shame on you all, you all laugh at me for asking daft questions when i was a n00b, how do you feel now:oQuote:
Originally posted by Wokawidget
OK, in VB6 I would do:
in .NET I want to do:VB Code:
Dim lvwItem As ListItem Set lvwItem = ListView1.ListItems.Item("Woof")
I feel really daft for asking this :(VB Code:
Dim lvwItem As ListViewItem lvwItem = Listview1. ?????????
Woof
I feel great :D
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? :confused:
hmmm...will check it out tomorrow.
Thanks again and fish fingers, chips (fries) and beans to you all.
Wiggles
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 :D
Woka
i was afraid of people laughing at me:afrog:
now i need a new project, any ideas?