Jump to content
  • Advertisement
Sign in to follow this  
Kaan Aksoy

Simple ListView Inventory Question

This topic is 2179 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

Hi, for my game in XNA, I decided to use a Windows Form as the inventory interface. On the Form, I added a ListView item. So far it is working well, but now that I want to add quantities of items, it is not going to work. How can I set it so that the list of items in the ListView are my own custom objects (I have an Item class). If I could find a way to implement this, I think it would be much easier. All help would be appreciated :)

Share this post


Link to post
Share on other sites
Advertisement
There is a "Tag" member in the ListViewItem class, which is used to associate the listview item with user data. That's probably what you need. Set the Tag property to the GameItem object. Of course, with Tag being of type "object", you'll need to cast back to GameItem when retrieving the object from the listview item. A small example:

[source lang="C#"]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]public[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]struct[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]GameItem[/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"] public[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]string[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Name;[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"] public[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Energy;[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"] public[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Damage;[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"] public[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Value;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]};[/font][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]public void AddGameItemToListView(ListView lv,GameItem gameItem)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"] ListViewItem lv_item=new ListViewItem();[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"] lv_item.Tag = gameItem;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"] lv_item.Text = gameItem.Name;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"] lv.Items.Add(lv_item);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]...[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"] GameItem itemToRetrieve = (GameItem)lv_item.Tag;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]...[/font][/font]
[/source]

Share this post


Link to post
Share on other sites
Just to piggyback on mikeman's code a bit, to add multiple items you could do something like this:


public struct GameItem
{
public static string Name;
public int Energy;
public int Damage;
public int Value;
};
...
lvi.Tag = new List<GameItem>();
lvi.Text = GameItem.Name;
lv.Items.Add(lvi);
...
List<GameItem> itemList = (List<GameItem>)lvi.Tag;
int quantity = itemList.Count;

foreach( GameItem item in itemList )
{
// Do thing with individual item
}
...
Edited by SeiryuEnder

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!