hankthetank Posted June 30, 2004 Posted June 30, 2004 ListBox.ItemData in .Net Can someone please tell me how to override a ListBox's ToString method?? All data is being pulled from the XML OK and I can , it is just that the List Box displays 'project1.frmMain+Catalogue' for each list item. I assume this is being caused by the ToString method that I need to override.... I have the following... Public Class Catalogue Public ID As String Public Name As String Public Sub New(ByVal sID As String, ByVal sName As String) ID = sID Name = sName End Sub End Class Private Sub LoadCatalogue() Dim xmlDoc As New XmlDocument Dim xmlElem As XmlElement Dim xmlNode As XmlNode, xmlItem As XmlNode xmlDoc.Load(sXMLFile) xmlElem = xmlDoc.DocumentElement xmlNode = xmlElem.SelectSingleNode("//document") With lstCatalogue .Items.Clear() .ValueMember = "ID" .DisplayMember = "Name" For Each xmlItem In xmlNode.ChildNodes .Items.Add(New Catalogue(GetXMLAttribute(xmlItem, "id"), GetXMLAttribute(xmlItem, "name"))) Next End With Exit_Sub: xmlNode = Nothing xmlItem = Nothing xmlElem = Nothing xmlDoc = Nothing Exit Sub End Sub Quote
Administrators PlausiblyDamp Posted June 30, 2004 Administrators Posted June 30, 2004 You would override the Catalogue class' ToString... Public Class Catalogue Public ID As String Public Name As String Public Sub New(ByVal sID As String, ByVal sName As String) ID = sID Name = sName End Sub public overrides Function ToString() as string return Name 'Or whatever you want to display in the listbox... end function End Class (Not near VS at the moment so that may not be perfect but should give you the idea) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
hankthetank Posted June 30, 2004 Author Posted June 30, 2004 That did the trick!! Perfect. Thanks PD. :D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.