|
-
Jul 19th, 2009, 03:00 PM
#1
Thread Starter
New Member
[RESOLVED] Changing object name in Combo Box.
I was just starting to learn how to use classes today and I think I might have a vague handle on them.
Anyways, I was using a class to generate troops (as objects, since I needed various statistics tied to each individual one) in an army for sort of a basic medieval-type game I was creating and adding them to a combo box.
It all works perfect and the index holds my object and I can call data from it, etc., my only issue is with the aesthetics of my result.
All of the troops I add to the combo box as objects appear in the dropdown list as "WarProject.Soldier", the name of the object. This is obviously not something an end-user would understand or want to see. I have no idea how to change what an object appears as in the combo box.
Anyone know how to do this?
I don't think helping with this requires looking at my code at all (since all I need is for someone to help me figure out how to change what my objects are appearing in the combo box as), but let me know if you do need to see it for some reason.
Help appreciated!
Last edited by CaptainDingo; Jul 19th, 2009 at 03:07 PM.
-
Jul 19th, 2009, 03:38 PM
#2
Re: Changing object name in Combo Box.
There's a couple ways you can do this. The first way is to assign the property name of what you want to show in the combobox to the ComboBox's DisplayMember property:
vb.net Code:
Public Class Form1 Public Sub New() InitializeComponent() Dim ws As New WarSoldier() ws.Name = "John" ws.SomeOtherProperty = "Some other value" Me.ComboBox1.Items.Add(ws) Me.ComboBox1.DisplayMember = "Name" 'Property name to show End Sub End Class Public Class WarSoldier Private _Name As String Property Name() As String Get Return Me._Name End Get Set(ByVal value As String) Me._Name = value End Set End Property Private _SomeOtherProperty As String Public Property SomeOtherProperty() As String Get Return _SomeOtherProperty End Get Set(ByVal value As String) _SomeOtherProperty = value End Set End Property End Class
The second way is to override the ToString() method:
vb.net Code:
Public Class Form1 Public Sub New() InitializeComponent() Dim ws As New WarSoldier() ws.Name = "John" ws.SomeOtherProperty = "Some other value" Me.ComboBox1.Items.Add(ws) 'notice you don't have to assign the display member here End Sub End Class Public Class WarSoldier Private _Name As String Property Name() As String Get Return Me._Name End Get Set(ByVal value As String) Me._Name = value End Set End Property Private _SomeOtherProperty As String Public Property SomeOtherProperty() As String Get Return _SomeOtherProperty End Get Set(ByVal value As String) _SomeOtherProperty = value End Set End Property Public Overrides Function ToString() As String Return Name End Function End Class
-
Jul 19th, 2009, 05:22 PM
#3
Thread Starter
New Member
Re: Changing object name in Combo Box.
Hrm. I tried both those but I just can't get it to work.
The item in the list is always blank now. And if I take out InitializeComponent(), it just does what it always did.
I'm kind of frustrated.
I'll just post the entire block of code I have that's adding the troop, and my troop class. Maybe you can help me plug all this stuff in correctly, I'm not even sure what most of that code was doing.
Troop Class:
Code:
Public Class Soldier
Public Soldier_ID As Integer
Public Soldier_Name As String
Public Soldier_LastName As String
Public Soldier_Age As Integer
Public Soldier_Power As Integer
Public Soldier_Defense As Integer
Public Soldier_Rank As String
Private _Name As String
Property Name() As String
Get
Return Me._Name
End Get
Set(ByVal value As String)
Me._Name = value
End Set
End Property
Private _SomeOtherProperty As String
Public Property SomeOtherProperty() As String
Get
Return _SomeOtherProperty
End Get
Set(ByVal value As String)
_SomeOtherProperty = value
End Set
End Property
End Class
Code:
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
InitializeComponent()
Dim objSoldier As Soldier
objSoldier = New Soldier
randindex = randomnumber.Next(0, 30)
objSoldier.Soldier_Name = ArmyFirstNames(randindex)
randindex = randomnumber.Next(0, 40)
objSoldier.Soldier_LastName = ArmyLastNames(randindex)
objSoldier.Soldier_ID = "1"
objSoldier.Soldier_Age = Rand(100, 500)
objSoldier.Soldier_Power = Rand(1, 2)
objSoldier.Soldier_Defense = Rand(1, 2)
objSoldier.Soldier_Rank = "New Recruit"
objSoldier.Name = "Testing"
ComboBox1.Items.Add(objSoldier)
ComboBox1.DisplayMember = Name
End Sub
Some of the extra stuff here if it confuses you... I was randomizing first and last names using two arrays I built, so when I recruit a new troop his name is random, and he's got a bunch of other properties I was setting. You can probably ignore those.
I'm sure I'm doing something horribly wrong with the code you gave me. :P I'm really not very good at VB right now and don't understand all the set/get stuff and why it's not linked right or not working with the combo box, etc.
-
Jul 19th, 2009, 05:24 PM
#4
Re: Changing object name in Combo Box.
1) Remove InitializeComponent from the button click event. That call is only performed in the New sub.
2) If you look at what I posted, you have to put the name of the property as a string. Meaning this:
vb.net Code:
ComboBox1.DisplayMember = Name
Should be this:
vb.net Code:
ComboBox1.DisplayMember = "Name"
-
Jul 19th, 2009, 05:30 PM
#5
Thread Starter
New Member
Re: Changing object name in Combo Box.
Oh, jeez. Epic. It works perfectly now, thanks.
Here, you can see the fruits of your tutelage. 
http://i26.photobucket.com/albums/c1...go/Success.jpg
Thanks again!
-
Jul 19th, 2009, 06:13 PM
#6
Re: [RESOLVED] Changing object name in Combo Box.
The font you have for Recruit looks like an Age Of Empires font or something...
-
Jul 19th, 2009, 08:07 PM
#7
Thread Starter
New Member
Re: [RESOLVED] Changing object name in Combo Box.
Haha. It's AvQuest, the font most commonly known as the font used in the Diablo series.
Unfortunately it sizes down very very poorly so I'll be switching to a different one.
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
|