Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Administrators
Posted

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)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...