• Advertisement
Sign in to follow this  

[C#] Getting Custom Objects from a ComboBox

This topic is 2325 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font=arial, verdana, tahoma, sans-serif][size=2]I'm building an editor using Windows Forms in C# for my game, and I'm stuck on how collections work. I have a custom class "Character" which I add to my combo box's "Items" collection like so:

Character character = (Character)CharacterComboBox.Items[index];

[/font]

What I've done is put a bunch of Character-type objects in my Combo Box's "Items" collection. It stores the data, and now I want to be able to get that data out and display its data in my Window so that I can modify it. Any idea on how to do that?

Also, I've subclassed my Character class with "object", and overridden its "ToString()" method with my own that displays the name of the character which works.

Share this post


Link to post
Share on other sites
Advertisement

That code doesn't add anything, it tries to get something out. Can you post the actual code you're having trouble with?


My mistake, I'm trying to actually get objects from a ComboBox's "Items" collection. I'll post more code.

Here is my Character class:

public class Character : object
{
public Attribute baseAttributes;
public Attribute growthAttributes;
public Stats baseStats;
public Stats growthStats;


public string name;
public string modelFilename;
public string initScriptFilename;
public string updateScriptFilename;


public Faction faction = Faction.FACTION_NONE;
public Resistance fireRes = Resistance.RESISTANCE_NORMAL;
public Resistance coldRes = Resistance.RESISTANCE_NORMAL;
public Resistance lightningRes = Resistance.RESISTANCE_NORMAL;


public short commonDropItem = 0;
public short rareDropItem = 0;
public SByte commonDropRate = 0;
public SByte rareDropRate = 0;

public Character()
{
name = "New Character ~";
}

public Character(int index)
{
name = "New Character " + index.ToString();
}

public override string ToString()
{ return name; }
}


Here's the methods in my Windows Form to add AND remove a character to my ComboBox:

private void addCharacterToolStripMenuItem_Click(object sender, EventArgs e)
{
// make sure there aren't too many characters in the database
if (CharacterComboBox.Items.Count > 1023)
{
MessageBox.Show("Too many characters created. Please remove a character from the database to add more.", "ERROR", MessageBoxButtons.OK);
return;
}

CharacterComboBox.Items.Add(new Character(characterIndex).ToString());
CalcluateCapacity();
characterIndex++;
}


private void removeCharacterToolStripMenuItem_Click(object sender, EventArgs e)
{
// make sure there's at least 1 character profile that exists
if (CharacterComboBox.Items.Count < 2)
{
MessageBox.Show("There must be at least 1 character profile in existence.", "ERROR", MessageBoxButtons.OK);
return;
}

CharacterComboBox.Items.RemoveAt(CharacterComboBox.SelectedIndex);
CalcluateCapacity();
CharacterComboBox.SelectedIndex = 0;
}


Now, what I need to do is get the an item at it's current index as a "Character" object so that I can extract it's character data into text fields, and other controls so the user can edit it the data, and apply the edits to that combo box item.

Share this post


Link to post
Share on other sites
Well, the line of code you posted originally tries to do exactly what you describe. What happens when you put that code into your program and run it?


NB: I know what's going on here, I just want to help you go through the process of learning to think out these sorts of things by yourself :-)

Share this post


Link to post
Share on other sites

Well, the line of code you posted originally tries to do exactly what you describe. What happens when you put that code into your program and run it?


NB: I know what's going on here, I just want to help you go through the process of learning to think out these sorts of things by yourself :-)


I appreciate it. I've done quite a bit of googling this weekend when I've had the time. When I try accessing it using that array notation from my first post, everything works fine until I try to select an element greater than zero. Selecting anything other than the first element results in a runtime exception, and cryptic JIT debugging output. ComboBox.Items isn't actually just a collection, it's a subclass of IList, ICollection and IDictionary, I believe.

I'm not really a C# programmer at all. Is there any way to just grab a specific element from the items collection at a given index or do I need to iterate through it using foreach() until I get to the desired index in the list? I tried this code, but I get runtime exceptions:

foreach(object charObj in CharacterComboBox.Items)
{
Character character = (Character)charObj; // exception occurs!
}

Share this post


Link to post
Share on other sites
foreach(object charObj in CharacterComboBox.Items)
{
Character character = (Character)charObj; // exception occurs!
}

Is the Items collection storing an actual Character object?

CharacterComboBox.Items.Add(new Character(characterIndex).ToString());

Take a deeper look at that line.

Share this post


Link to post
Share on other sites
As AverageMidget says, you aren't storing a character in the combo box. You are storing the character ToString in the combo box. Assuming you have:

- a list of characters "List<Character> charList"
- a combo box "ComboBox cBox"

And the list items are in the same order as the combo box items. Then you can do this:



// init combo box like this
cBox.Items.Clear();
foreach(Character c in charList)
{
cBox.Items.Add(c.ToString();
}

// select a character like this
// by modifiying the combo box SelectedIndexChanged event
if(cBox.selectedIndex >= 0)
{
Character c = charList[cBox.selectedIndex];
// then do something with the character...
}

Share this post


Link to post
Share on other sites

foreach(object charObj in CharacterComboBox.Items)
{
Character character = (Character)charObj; // exception occurs!
}

Is the Items collection storing an actual Character object?

CharacterComboBox.Items.Add(new Character(characterIndex).ToString());

Take a deeper look at that line.


That's a good point^

This is actually a mistake in my code. I posted the current version where I just store the character's string in the combo box and the rest of the data in a list. It works, and I may just keep doing that in the future.

However, during my first post, I was adding the character itself to the Combo Box's Items collection like so:
CharacterComboBox.Items.Add(new Character(characterIndex));

Accessing a specific item using the foreach loop, array notation, and trying to cast the collection item to a character has given me runtime exceptions.

Share this post


Link to post
Share on other sites
However, during my first post, I was adding the character itself to the Combo Box's Items collection like so:
CharacterComboBox.Items.Add(new Character(characterIndex));

Accessing a specific item using the foreach loop, array notation, and trying to cast the collection item to a character has given me runtime exceptions.


...which you never actually posted that code in your first post.

I guess the first thing should be to let us know how you're going to store your characters. Storing their string names in a combobox, while storing their data elsewhere, will be a different answer than storing the characters, themself, in the combobox.

If you're going to store only the string, then you'll need to search through the container holding your character objects and compare the combobox string to the string returned by your overloaded ToString() method. Or, if you can guarantee the index of the combobox and the container match, you could access them that way.

If you're going to store the character object, itself, in the combobox, and you were looking to access the selected character, you could handle the SelectedIndexChanged() method, like so
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
Character selectedCharacter = (Character)cb.SelectedItem;

// access your characters data now
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement