-
Oct 14th, 2024, 09:42 AM
#1
Thread Starter
Lively Member
[RESOLVED] Add Property to Listview items ?
With following Code (class) i add a Property to a ListView itself.
How to add that property to all ListView items ?
Code:
Public Class MyListView
Inherits ListView
Private _Tag1 As Object
Public Property Tag1() As Object
Get
Return _Tag1
End Get
Set(ByVal value As Object)
_Tag1 = value
End Set
End Property
End Class
Many ways exist to Babylon.
Only the shortest is the Best.
-
Oct 14th, 2024, 01:28 PM
#2
Re: Add Property to Listview items ?
It'll be the same value for all of your items, that way
Can you show your adding items code?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 14th, 2024, 01:30 PM
#3
Re: Add Property to Listview items ?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 14th, 2024, 01:45 PM
#4
Thread Starter
Lively Member
Re: Add Property to Listview items ?
Originally Posted by .paul.
It'll be the same value for all of your items, that way
Can you show your adding items code?
I wont to store in Tag1 another information (like in the second line.
Code:
lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag = "Pheasant*"
lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag1 = "D:\Pics\image1""
Many ways exist to Babylon.
Only the shortest is the Best.
-
Oct 14th, 2024, 02:07 PM
#5
Re: Add Property to Listview items ?
An Objects Tag Property is Type Object. You can put anything you choose in there...
For example, a Bitmap. You need to cast the Tag to the appropriate Type when you retrieve it...
Code:
lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag = New Bitmap("D:\Pics\image1.png")
For simple strings, try this...
Code:
lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag = New List(Of String)(New String() {"Pheasant*", "D:\Pics\image1"})
Code:
Private Sub lstAnimal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstAnimal.SelectedIndexChanged
MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, List(Of String))(0))
MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, List(Of String))(1))
'add another
DirectCast(lstAnimal.FocusedItem.Tag, List(Of String)).Add("Third string")
MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, List(Of String))(2))
End Sub
Last edited by .paul.; Oct 14th, 2024 at 02:12 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 14th, 2024, 02:22 PM
#6
Thread Starter
Lively Member
Re: Add Property to Listview items ?
Thanks for the Code , i will try this out.(this is another way)
You mean the Add Property its not Possible for ListView items ?
Many ways exist to Babylon.
Only the shortest is the Best.
-
Oct 14th, 2024, 02:49 PM
#7
Re: Add Property to Listview items ?
Originally Posted by marsias
...You mean the Add Property its not Possible for ListView items ?
It's possible, but not really necessary, as the tag can hold anything you need it to...
Code:
Public Class TagExtenderClass
Public Tag1 As String
Public Tag2 As String
Public Tag3 As String
End Class
Code:
lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag = New TagExtenderClass With {.Tag1="First String", .Tag2="Second String" , .Tag3="Third String"}
Code:
Private Sub lstAnimal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstAnimal.SelectedIndexChanged
MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, TagExtenderClass ).Tag1)
MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, TagExtenderClass ).Tag2)
MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, TagExtenderClass ).Tag3)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 14th, 2024, 03:01 PM
#8
Re: Add Property to Listview items ?
Check the link in post #3 if you want to add properties
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2024, 07:56 AM
#9
Re: Add Property to Listview items ?
The problem with the code in post #1 is that you extended the ListView ... not ListViewItem ...
Post 4 uses the default .Add method which would use the default ListViewItem ... not your custom one. You'd have to inherit ListView and create a .Add2 that returns your custom ListViewItem with the Tag2 property, then you'd be able to use it. But then you'd have to replace all your list view with your custom one .... you can see how this is getting out of hand quickly.
All that said... the solution .Paul posted in post #7 is likely the best solution. The .Tag property is just an Object type, so you can put anything you want into it, so why not make that your complex object? That's far more elegant than trying to inherit and override the base controls and so on...
-tg
-
Oct 15th, 2024, 09:13 AM
#10
Thread Starter
Lively Member
Re: Add Property to Listview items ?
I see yet that the Pauls solution is a simply one and working Solution (already applied).
I always search for a simply Solution (1-2 lines of Code) and not to write a bunches of Code to resolve the same Problem(like in external link from Paul).
As i see, these steps can also applied to all Controls which have a Collection of items(Combobox, listBox, etc)
Can this steps also applied to other Properties like Forecolor to have a disabled Forecolor?
Many thanks again to Paul
Many ways exist to Babylon.
Only the shortest is the Best.
-
Oct 15th, 2024, 09:33 AM
#11
Re: Add Property to Listview items ?
Unfortunately, you're not quite right about ComboBox and ListBox items. For those, you'd use a slightly different method...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2024, 09:36 AM
#12
Re: Add Property to Listview items ?
Originally Posted by marsias
...Can this steps also applied to other Properties like Forecolor to have a disabled Forecolor?
I'm not sure what you mean there. There are ways to stop the forecolor being editable, but that's not related to the topic of this thread.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|