Reference Listview subitem by name

All,
I have a listview in detail view with several subitems. I give each a name and a value add them to my listview item and add my item to the list. All works well. However, When a use clicks on the item (full row selected) i want to get the value of one of these subitems using a reference to its name rather than its index value. I was assuming this was going to be a simple coding task by replacing the subitem index value (lvi.SubItems(3).Text) with the subitem name (lvi.SubItems("MySubItem").Text)...i was wrong...can anyone please help

Cheers

[566 byte] By [shax] at [2007-12-28]
# 1

The subitem can be referenced by key (name) or by index see the following as a sample

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim s As String

Dim lvi As ListViewItem = Me.ListView2.Items(0)

s = lvi.Text

MessageBox.Show(s)

lvi.SubItems.Clear()

Dim lvis As New ListViewItem.ListViewSubItem

lvis.Name = "S1"

lvis.Text = "Sub1"

lvis.Tag = "Sub1"

lvi.SubItems.Add(lvis)

s = lvi.SubItems("S1").Text()

MessageBox.Show(s)

End Sub

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Hi,

(lvi.SubItems("MySubItem").Text)

Dim MySubItem As OBJECT

MySubitem = lvi.SubItem(3)

would become>> (lvi.SubItems(byName(MySubItem).Text ) )

This works for a textbox so try it in a listview and the sub-items you are referring to.>>

Dim myobj As Object = TextBox1

TextBox1.Text="1"

' This next line gives the message>> TextBox1

MsgBox(byName(myobj)) ' Note the Function byName returns a String value so you don't have to convert it. :-)

Dim myInt As Integer

' This next line gets the item VALUE.

myInt = CInt(myobj.text)

' This next line gives the message>> TextBox1 has the value of 1

MsgBox("Object " & byName(myobj) & " has the value of " & CStr(myInt))

Function byName(ByVal myObj As Object) As String

Dim returnVal As String

returnVal = myObj.name

Return returnVal

End Function

Regards,

S_DS

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...