Results 1 to 2 of 2

Thread: adding objects to listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2020

    adding objects to listbox

    Is it possible to display objects inside of a listbox?
    For example, I have a form with a listbox and a button below it that reads purchase weapon.

    I made a separate form and named it weapons. Inside of weapons, I created a new weapon object.
    Public Class weapons
        Private Sub weapons_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim weapon1 As Shuriken
            weapon1 = New Shuriken
            weapon1.Name = "Black Shuriken"
            weapon1.cost = 300D
        End Sub
        Public Class Shuriken
            Public Name As String
            Public cost As Integer
        End Class
    End Class
    How could I call the Shuriken object from my weapons class and have it display inside of my listbox?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: adding objects to listbox

    Firstly, let's fix the silliness with the code you have. Firstly, why would you have one field called Name and another called cost rather than Cost? Why would you be inconsistent with the casing of your fields? Secondly, why would you declare the cost field as type Integer and the explicitly assign a Decimal value to it? Think about what you're doing. ALWAYS be consistent and ALWAYS us the correct/appropriate data types. In fact, turn Option Strict On in the project properties and that will force you to do better with your data types. You can still do the wrong thing but less often. Turn it On in the VS options too, so it is On by default in future projects. You will write better code sooner as a result.

    As for your question, a ListBox ALWAYS contains and displays objects. Strings and Integers are still objects, if just simpler ones. Any time you add instances of any type to a ListBox, you are adding objects. the ListBox then creates a text representation of that object for display purposes. By default, it will call the ToString method of the object and display the result. By default, the ToString method of an object returns the name of its type. If you do this: Code:
    1. Dim weapon1 As New Shuriken With {.Name = "Black Shuriken", .Cost = 300}
    3. ListBox1.Items.Add(weapon1)
    then, in your ListBox, you would see YourProjectName.Shuriken.

    If you want to change what your object returns from its ToString method then you need to override it, e.g. Code:
    1. Public Class Shuriken
    3.     Public Name As String
    4.     Public Cost As Integer
    6.     ''' <inheritdoc />
    7.     Public Overrides Function ToString() As String
    8.         Return $"{Name} ({Cost})"
    9.     End Function
    11. End Class
    Add the same item to the ListBox would then display Black Shuriken (300). If you just wanted to display the Name value then you can do so by data-binding, but only if you declare properties rather than fields: Code:
    1. Public Class Shuriken
    3.     Public Property Name As String
    4.     Public Property Cost As Integer
    6. End Class
    You can then do this: Code:
    1. Dim weapon1 As New Shuriken With {.Name = "Black Shuriken", .Cost = 300}
    3. ListBox1.DisplayMember = "Name"
    5. ListBox1.Items.Add(weapon1)
    and your ListBox would display Black Shuriken. Of course, you should set the DisplayMember property of the control in the designer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width