Results 1 to 7 of 7

Thread: [RESOLVED] Changing object name in Combo Box.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    6

    Resolved [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.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. Public Class Form1
    2.     Public Sub New()
    3.         InitializeComponent()
    4.         Dim ws As New WarSoldier()
    5.         ws.Name = "John"
    6.         ws.SomeOtherProperty = "Some other value"
    7.         Me.ComboBox1.Items.Add(ws)
    8.         Me.ComboBox1.DisplayMember = "Name" 'Property name to show
    9.     End Sub
    10.  
    11. End Class
    12.  
    13. Public Class WarSoldier
    14.     Private _Name As String
    15.     Property Name() As String
    16.         Get
    17.             Return Me._Name
    18.         End Get
    19.         Set(ByVal value As String)
    20.             Me._Name = value
    21.         End Set
    22.     End Property
    23.  
    24.     Private _SomeOtherProperty As String
    25.     Public Property SomeOtherProperty() As String
    26.         Get
    27.             Return _SomeOtherProperty
    28.         End Get
    29.         Set(ByVal value As String)
    30.             _SomeOtherProperty = value
    31.         End Set
    32.     End Property
    33. End Class

    The second way is to override the ToString() method:

    vb.net Code:
    1. Public Class Form1
    2.     Public Sub New()
    3.         InitializeComponent()
    4.         Dim ws As New WarSoldier()
    5.         ws.Name = "John"
    6.         ws.SomeOtherProperty = "Some other value"
    7.         Me.ComboBox1.Items.Add(ws)
    8.         'notice you don't have to assign the display member here
    9.     End Sub
    10.  
    11. End Class
    12.  
    13. Public Class WarSoldier
    14.     Private _Name As String
    15.     Property Name() As String
    16.         Get
    17.             Return Me._Name
    18.         End Get
    19.         Set(ByVal value As String)
    20.             Me._Name = value
    21.         End Set
    22.     End Property
    23.  
    24.     Private _SomeOtherProperty As String
    25.     Public Property SomeOtherProperty() As String
    26.         Get
    27.             Return _SomeOtherProperty
    28.         End Get
    29.         Set(ByVal value As String)
    30.             _SomeOtherProperty = value
    31.         End Set
    32.     End Property
    33.  
    34.     Public Overrides Function ToString() As String
    35.         Return Name
    36.     End Function
    37.  
    38. End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    6

    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.

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. ComboBox1.DisplayMember = Name

    Should be this:

    vb.net Code:
    1. ComboBox1.DisplayMember = "Name"

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    6

    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!

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [RESOLVED] Changing object name in Combo Box.

    The font you have for Recruit looks like an Age Of Empires font or something...

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    6

    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
  •  



Click Here to Expand Forum to Full Width