Results 1 to 5 of 5

Thread: adding objects to listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2020
    Posts
    14

    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.
    Code:
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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:
    vb.net Code:
    1. Dim weapon1 As New Shuriken With {.Name = "Black Shuriken", .Cost = 300}
    2.  
    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.
    vb.net Code:
    1. Public Class Shuriken
    2.  
    3.     Public Name As String
    4.     Public Cost As Integer
    5.  
    6.     ''' <inheritdoc />
    7.     Public Overrides Function ToString() As String
    8.         Return $"{Name} ({Cost})"
    9.     End Function
    10.  
    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:
    vb.net Code:
    1. Public Class Shuriken
    2.  
    3.     Public Property Name As String
    4.     Public Property Cost As Integer
    5.  
    6. End Class
    You can then do this:
    vb.net Code:
    1. Dim weapon1 As New Shuriken With {.Name = "Black Shuriken", .Cost = 300}
    2.  
    3. ListBox1.DisplayMember = "Name"
    4.  
    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.

  3. #3
    New Member
    Join Date
    Nov 2021
    Posts
    1

    Re: adding objects to listbox

    jmcilhinney
    And if i want to doubleclick on the Listbox to display the properties of the weapon in Textboxes. How can i do that?


    Code:
        Private Sub lsb_Weapons_DoubleClick(sender As Object, e As EventArgs) Handles lsb_Weapons.DoubleClick
    
            If lsb_Weapons.SelectedIndex <> -1 Then
    
                Dim W as New Weapon
                W = lsb_Weapons.SelectedItem
    
                txt_Cost.Text = Format(W.Cost, "c")
                txt_Name.Text = W.Name
    
            Else
                Exit Sub
            End If
    
            lsb_Weapons.ClearSelected()
    I think thats the code, but im having issues with the SelectdItem... For example, if i add two weapons, wich one with a diferent cost and name, and then if i doubleclick the first one, for some reason the SelectdItem appears with the last add Weapon. I dont know why this is happening. Could somebody help me?

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2020
    Posts
    14

    Re: adding objects to listbox

    I think you're going to need a for-loop to accomplish that. I'll try tinkering with my listbox to see if I can find the code that works for you.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: adding objects to listbox

    You need to use the SelectedIndexChanged event.
    Also, this...

    Code:
    Dim W as New Weapon
    W = lsb_Weapons.SelectedItem
    Should be...

    Code:
    Dim W as Weapon = DirectCast(lsb_Weapons.SelectedItem, Weapon)

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