-
Jul 29th, 2021, 10:20 PM
#1
Thread Starter
New Member
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?
-
Jul 29th, 2021, 10:55 PM
#2
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:
Dim weapon1 As New Shuriken With {.Name = "Black Shuriken", .Cost = 300}
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:
Public Class Shuriken
Public Name As String
Public Cost As Integer
''' <inheritdoc />
Public Overrides Function ToString() As String
Return $"{Name} ({Cost})"
End Function
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:
Public Class Shuriken
Public Property Name As String
Public Property Cost As Integer
End Class
You can then do this:
vb.net Code:
Dim weapon1 As New Shuriken With {.Name = "Black Shuriken", .Cost = 300}
ListBox1.DisplayMember = "Name"
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.
-
Nov 12th, 2021, 08:42 AM
#3
New Member
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?
-
Nov 12th, 2021, 10:16 AM
#4
Thread Starter
New Member
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.
-
Nov 12th, 2021, 06:13 PM
#5
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|