Can anyone tell me what the dotnet equivalent is to the following code:
Dim strListItem As String
strListItem = VB6.GetItemString(Me.lstCall, Me.lstCall.Items.Count -
1)
strListItem = strListItem & "."
VB6.SetItemString(Me.lstCall, Me.lstCall.Items.Count - 1, strListItem)
I want to remove the Microsoft.VisualBasic.Compatibility reference and make
my app a true .net app.
Thanks,
Well, there isn't one exactly.... Those functions are there because
the .NET ListBox does not have an ItemData property like it did in
VB6. The reson for this is that ListItems do not need to be simple
strings. The most common use of the ItemData was to associate an
Integer value with an entry in the listbox (often a record id) - but
that's not needed in .NET. For example:
class Person
private _id as integer
private _firstName as string
private _lastName as string
public property ID as integer
get
retrun id
end get
set (byval value as integer)
_id = value
end set
end property
' properties for first and last name
' full name property
public readonly property string FullName
get
return _lastName & ", " & _firstName
end get
end property
end class
Now, you can fill a list box with a bunch of these objects:
listbox1.DisplayMember = "FullName"
dim p as new person()
p.id = 1
p.firstname = "tom"
p.lastname = "shelton"
listbox1.items.add(p)
Now you would see:
shelton, tom
in your ListBox. Now, obviously in this case you would probably put a
whole bunch of objects in there from some source - but this is just a
silly example
No, if you needed the id of the selected item, you would do somthing
like:
if not listbox1.selecteditem is nothing then
dim p as person = directcast(listbox1.selecteditem, person)
id = p.id
end if
Anyway, the thing to take from this is that the listbox in .NET
doesn't need an itemdata property, because you can stick arbitrary
objects in it which can have as much or little data associated with
them as you want.... You might want to become familiar with the
DisplayMember and ValueMember properties of the ListBox.