8 Replies - 4730 Views - Last Post: 25 October 2009 - 10:08 AM Rate Topic: -----

#1 masashiorika  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 06-July 09

ListBox and Multiple Items

Post icon  Posted 24 October 2009 - 09:07 PM

Hey,

So what I'm looking into doing is be able to add items into a listbox (which is easy), and when the user selects an item, depending on the item, it'll update labels which are on the same form as the listbox(not sure how to do), and then the user can "use" the item they've selected, and it's removed from the listbox, UNLESS there are multiple of them.

An analogy to make this easier to visualize would be this:

There are two forms, frmMain and frmInventory (where the list box and labels are located).

In frmMain, the user gains a potion (in this case, let's say three potions, kill two birds with one stone).

Now we can add it to the list box by using:
frmInventory.wepList.Items.Add("Small Potion (3)")


and so now in the list box, all there is is just "Small Potion (3)".
When the user selects the item, on the right of the list box are two labels, lblName and lblEffect.
It'll update lblName to "Small Potion" and lblEffect to "HP Recovery + 50".

Below these two labels is a button, which has the text "Use Item" on it.

When that button is clicked, it will execute a command associated with the item (little confused on how to do this part) along with change the name in the list box to "Small Potion (2)".

I could change it so it just says "Small Potion" in the list box, and have a label to indicate numbers, and have a If/Else command. For example:

If lblNumber = 0 Then
frmInventory.wepList.Items.Remove("Small Potion")
Else
lblNumber -= 1
End If



So, in the end, this is what I am asking help for.

How do I make it so when the user selects a item, it'll update execute code for that item, how can I store a number associated with a item in the list box (along with any values associated with that item).

When user selects an item in the list box, how do I activate an event/code that is associated for that item?
How do I store values for a specific item in the list box?

Thank for the help, and if you have any questions, please ask them!

As for source code, there is none written yet to be of any value for this, sorry!

Is This A Good Question/Topic? 0
  • +

Replies To: ListBox and Multiple Items

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: ListBox and Multiple Items

Posted 24 October 2009 - 10:01 PM

Keep in mind that listboxes in VB.NET don't just hold string data. They can hold objects! What you see in the listbox will be the data you return in a toString method of that object. So consider the following...

Public Class item
	Private name As String
	Private description As String

	Public Sub New(ByVal thename As String, ByVal thedescription As String)
		name = thename
		description = thedescription
	End Sub

	Public Overrides Function toString() As String
		Return "Our item is: " & name
	End Function

End Class



Here we have a simple class where we pass it a name and description. Can you see something special about this? Yup, you can store your count here too!

Now here is how we add it to the listbox...

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
	' Create instance of our item
	Dim ourItem As New item("Small potion", "HP + 50")

	' Add item to listbox.
	ListBox1.Items.Add(ourItem)
End Sub



What you will see in the listbox is the value returned by the toString method of our class. So in the listbox you will see "Our item is: Small potion".

Ok, so we have it in the listbox, but what about getting it out? Well, listboxes that store objects like this store them as "Object". That is because it is the base class of all objects in .NET. So in order to get it back out, we have to read the listbox item, then cast it back to an instance of the item class...

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
		' If the item was selected from the listbox, convert it back to an item
		If Not ListBox1.SelectedItem Is Nothing Then
			Dim ourItemAgain As item = CType(ListBox1.SelectedItem, item)

			' Now we can treat "ourItemAgain" as an instance of our class item.
			MessageBox.Show(ourItemAgain.toString())
		End If
End Sub



As you can see, we casted it right back to an item class instance. From here we can use it like any instance of our class... like populating labels with properties from the class etc. Removing it is as simple as calling ListBox1.Items.Remove(ListBox1.SelectedItem) after our population of the labels and click of the button to use it.

:)
Was This Post Helpful? 1
  • +
  • -

#3 masashiorika  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 06-July 09

Re: ListBox and Multiple Items

Posted 24 October 2009 - 10:13 PM

Okay, I think I understand!

I've only glanced over creating classes and such xD

So for the class, if I change this:

	Public Overrides Function toString() As String
		Return "Our item is: " & name
	End Function


to this:

	Public Overrides Function toString() As String
		Return name
	End Function


It will only show "Small Potion" in the list box instead of "Our item is: Small Potion", correct?

And I get the Add part of it, it's pretty straight forward, thank you very much for that!


To display it in the labels when selected, how would it be?
You have it set as a MsgBox, and so in that message box, would it come up as "Small Potion, HP + 50"?

If it does, how are we able to separate it, so we could get the name in one label, and then the description in another?


You said we could add counters in the class, and I think I understand how.

	Private name As String
	Private description As String
Private number As Integer

	Public Sub New(ByVal thename As String, ByVal thedescription As String, ByVal thenumber As Integer)
		name = thename
		description = thedescription
		number = thenumber
	End Sub


And then when adding:

Dim ourItem As New item("Small potion", "HP + 50", 3)


and then I am still confused about how to display it, and then how would I read the counter, edit it, then if it's 0, remove it?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: ListBox and Multiple Items

Posted 24 October 2009 - 10:21 PM

Well add functions to the class which returns the various pieces of the item. We did this with the toString method to return the name (and yes, just using return name will return just the name). But you can have a property/method to return the description, the count etc. Then all you have to do is set the labels...

' After adding a method called "GetDescription" which returns the description "HP + 50"
' We set it to the label.
lblEffects.text = ourItemAgain.GetDescription()



All your labels will be set to different properties of the class. :)
Was This Post Helpful? 1
  • +
  • -

#5 masashiorika  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 06-July 09

Re: ListBox and Multiple Items

Posted 24 October 2009 - 10:28 PM

Okay, I got it!

Thank you very much, I've never understood classes at all until now, thank you very much again!

You've been of tremendous help!
Was This Post Helpful? 0
  • +
  • -

#6 masashiorika  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 06-July 09

Re: ListBox and Multiple Items

Posted 24 October 2009 - 10:46 PM

Actually, I have one more question.

If I use an item, and there is still one more left (So from 2 small potions to 1 small potion) how do I change it in the system?

Right now I have this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim Amount As Integer
		Dim Use As Integer
		Dim Effect As Integer
		If Not wepList.SelectedItem Is Nothing Then
			Dim ourItemAgain As item = CType(wepList.SelectedItem, item)

			Amount = ourItemAgain.GetNumber()
			Use = ourItemAgain.GetUse()
			Effect = ourItemAgain.GetEffect()

			If Use = 1 Then
				frmMain.lblPHP.Text += Effect
				Amount -= 1
				If Amount <= 0 Then
					wepList.Items.Remove(wepList.SelectedItem)
				Else
					'HERE IS WHERE I WANT TO LOWER THE AMOUNT BY 1
				End If
			End If
		End If
	End Sub


How would I go about doing that?

I have it being added (in the Main form) as:

Dim SPotion As New item("Small Potion", "HP + 50", 2, 1, 50)


The last two numbers (1 & 50) are used for something else, and will be static, but I need the 2 to be lowered to 1 in the system, and then stored.
Was This Post Helpful? 0
  • +
  • -

#7 masashiorika  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 06-July 09

Re: ListBox and Multiple Items

Posted 25 October 2009 - 09:29 AM

Okay, I figured it out!

I added this to the class:

	Public Function GetNewNumber() As Integer
		number -= 1

		If number <= 0 Then
			Return -1
		Else
			Return number
		End If
	End Function


and then when the button was clicked, this was activated:

		Dim Amount As Integer
		Dim Use As Integer
		Dim Effect As Integer
		Dim Number As String
		If Not wepList.SelectedItem Is Nothing Then
			Dim ourItemAgain As item = CType(wepList.SelectedItem, item)

			Amount = ourItemAgain.GetNumber()
			Use = ourItemAgain.GetUse()
			Effect = ourItemAgain.GetEffect()

			If Use = 1 Then
				frmMain.lblPHP.Text += Effect
				Number = ourItemAgain.GetNewNumber()
				If Number = -1 Then
					Amount = 0
					wepList.Items.Remove(wepList.SelectedItem)
				Else
					wepAmount.Text = Number
					Amount = Number
				End If
			End If
		End If

This post has been edited by masashiorika: 25 October 2009 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#8 masashiorika  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 06-July 09

Re: ListBox and Multiple Items

Posted 25 October 2009 - 09:46 AM

Actually, I have ended up with another questions!

Sometimes, a item in the wepList will be updates (let's say the number is increased by 1), how would I do this, from frmMain while the list in in frmInventory?

I've tried looking it over, but I am stuck currently, but while waiting for a reply, I'll keep trying to figure it out!
Was This Post Helpful? 0
  • +
  • -

#9 masashiorika  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 06-July 09

Re: ListBox and Multiple Items

Posted 25 October 2009 - 10:08 AM

Okay, surprisingly I have figured it out!

Sorry for the 4 posts in a row!
I hope this topic helps someone else so I'll post what I did.

I just declared the items:
 Dim SPotion As New item("Small Potion", "HP + 50", 2, 1, 50)


Right under the Public Class, but above everything else, so they can be called from anywhere.

And to do anything with it, where we used ourItemAgain, in the other form, we can now just use the name (SPotion)

For example:

SPotion.GetNumber()


is the same as:

ourItemAgain.GetNumber()


on the other form, which required the item to be selected, where as if we call it by the name directly in the main form, we don't need to have it selected, and we can call multiple items at once (SPotion, MPotion, and LPotion for example)

I hope this helps for other people!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1