Jump to content

  • Log In with Google      Sign In   
  • Create Account


Simple ListView Inventory Question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Kaandimandimanbopbobop   Members   -  Reputation: 109

Like
0Likes
Like

Posted 03 June 2012 - 06:38 PM

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 :)

Sponsor:

#2 mikeman   Members   -  Reputation: 2176

Like
0Likes
Like

Posted 04 June 2012 - 05:29 AM

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[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]struct[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]GameItem[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"] public[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]string[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Name;[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"] public[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Energy;[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"] public[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Damage;[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"] public[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Value;[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]};[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]public void AddGameItemToListView(ListView lv,GameItem gameItem)[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ListViewItem lv_item=new ListViewItem();[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] lv_item.Tag = gameItem;[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] lv_item.Text = gameItem.Name;[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] lv.Items.Add(lv_item);[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]...[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] GameItem itemToRetrieve = (GameItem)lv_item.Tag;[/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]...[/size][/font][/size][/font][/source]

#3 SeiryuEnder   Members   -  Reputation: 199

Like
0Likes
Like

Posted 04 June 2012 - 08:03 AM

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, 04 June 2012 - 08:07 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS