-
Questions about ListBoxes.
[Originally posted by DiS]
Hello,
Note: This is kinda long.
Let's see if I can explain this good, here it goes.. I have a listbox and I want to use the items, add items, and remove items anywhere in the listbox. This is for a simple game that I'm making. For example. If I add a Herb to the listbox. I want to be able to use it, add it, remove it, any where from the listbox. I have a Public Sub that updates the listbox, it looks like this.
Public Sub UpdateList(LBox As ListBox)
LBox.Clear
LBox.AddItem "Herb˙ ˙ ˙ ˙ ˙ ˙ " & Herb
LBox.AddItem "Heal˙ ˙ ˙ ˙ " & Heal
LBox.AddItem "Potion˙ ˙ ˙ ˙ ˙ ˙ " & Potion
LBox.AddItem "X Potion˙ ˙ ˙ ˙ ˙ ˙ " & XPotion
LBox.AddItem "Mini Ether˙ ˙ ˙ ˙ ˙ ˙ " & MiniEther
LBox.AddItem "Ether˙ ˙ ˙ ˙ ˙ " & Ether
I can't figure out how to use the items where ever the item is in the list box. Any help will be greatly appreciated. I've been trying for days, and I can't find out how on any sites or my **** books! Thanks for reading this.
Sincerely,
˙ DiS
-
Re:Questions about ListBoxes.
[Originally posted by Chris Carta]
what do you mean by 'how to use the items where ever the item is in the list box'?
Are you talking about how to find where an item is in the listbox or how to determine which item in a listbox was selected by the user?
Is the list box sorted?
Chris
-
Re:Questions about ListBoxes.
[Originally posted by DiS]
The ListBox isn't sorted. I would like to find out how to determine which item in the listbox was selected, then be able to use it, and if it becomes 0 then remove it. Does that make any sense at all?
-
Re:Re:Questions about ListBoxes.
[Originally posted by Chris Carta]
Give me a few minutes.
Chris
-
Re:Re:Questions about ListBoxes.
[Originally posted by Chris Carta]
Try this for starters,
˙ ˙ 'find selected item
˙ ˙ Dim strSelItem As String
˙ ˙ strSelItem = lBox.List(lBox.ListIndex)
˙ ˙
˙ ˙ 'delete selected item
˙ ˙ lBox.RemoveItem (lBox.ListIndex)
Chris
-
Re:Questions about ListBoxes.
[Originally posted by DiS]
Ok thank you, I'll play around with it and see if I can get this to work the way I want it to.
-
Re: Questions about ListBoxes.
[Originally posted by DiS]
Well.. this is what I did..
˙ ˙ Dim strSelItem As String
˙ ˙ strSelItem = LBox.List(LBox.ListIndex)
'just like you said, and the way I figure it is I use this like this:
˙ If strSelItem = "Herb" Then Call UseHerb 'uses the herb. But it doesn't work when I select herb from the listbox.. any suggestions or questions?
If it helps any I use a command button to use the items when selected..
-
Re:Re: Questions about ListBoxes.
[Originally posted by Chris Carta]
So, you click on a button, find the item selected, and if it's Herb, call the method UseHerb.
What part doesn't work, i.e., can you step the code and see that you found the item selected?˙ Do you get to where you're going to call the method?˙ Does it break in the method?˙ I take it that you're not getting an error, but rather it's not doing what you want.˙ Correct.
Chris
-
Re: Questions about ListBoxes.
[Originally posted by DiS]
So, you click on a button, find the item selected, and if it's Herb, call the method UseHerb. <-- That's exactly what I want to do. I'm not sure how to set it to where when the item is selected from anywhere in the listbox to run UseHerb, so it uses the item and then takes 1 away from the item.( Well the UseHerb already has the coding for using the item and taking it away..) Any suggestions/questions?
-
Re:Re: Questions about ListBoxes.
[Originally posted by Chris Carta]
Do you mean that you only want to take one away when used, but the subtraction is happening in two places?
If that's the case, I'd subtract in the method.
Chris
-
Re:Re:Re: Questions about ListBoxes.
[Originally posted by DiS]
When the item is selected in the ListBox the code doesn't run. Either nothing happens or I get errors. I have no idea on how to do this. This ListBox stuff is all new to me =/
-
Re:Re:Re:Re: Questions about ListBoxes.
[Originally posted by Chris Carta]
Are you running code from the double-click of the listbox?˙ I thought the user selects from the listbox and then presses a command button to 'use' the selected item?
Can you put the offending code out here?
If not, what are the error codes?
Chris
-
Re:Re:Re:Re:Re: Questions about ListBoxes.
[Originally posted by DiS]
This is in the module to update the listbox
Public Sub UpdateList(LBox As ListBox)
LBox.Clear
LBox.AddItem "Herb˙ ˙ ˙ ˙ ˙ ˙ " & Herb
LBox.AddItem "Heal˙ ˙ ˙ ˙ " & Heal
LBox.AddItem "Potion˙ ˙ ˙ ˙ ˙ ˙ " & Potion
LBox.AddItem "X Potion˙ ˙ ˙ ˙ ˙ ˙ " & XPotion
LBox.AddItem "Mini Ether˙ ˙ ˙ ˙ ˙ ˙ " & MiniEther
LBox.AddItem "Ether˙ ˙ ˙ ˙ ˙ " & Ether
LBox.AddItem "Elixer˙ ˙ ˙ ˙ ˙ " & Elixer
LBox.AddItem "Dagger˙ ˙ ˙ ˙ ˙ ˙ " & Dagger
LBox.AddItem "Short Sword˙ ˙ ˙ ˙ ˙ ˙ " & ShortSword
LBox.AddItem "Scimitar˙ ˙ ˙ ˙ ˙ " & Scimitar
LBox.AddItem "Clothes˙ ˙ ˙ ˙ ˙ ˙ " & Clothes
LBox.AddItem "Chain Mail˙ ˙ ˙ ˙ ˙ ˙ " & ChainMail
LBox.AddItem "Gloves˙ ˙ ˙ ˙ ˙ ˙ " & Gloves
LBox.AddItem "Wooden Shield˙ ˙ ˙ ˙ ˙ ˙ " & WoodenShield
End Sub
According to my coding, Herb's Index is 0, but it won't always be 0 since it'll be added and remove, re-added and so forth. Same with the other items that will be added to and removed from the listbox. The user is suppose to click the item in the ListBox, which will select it, then they hit the command button that has the name "Use Item" which uses the item. My problem is when the item is selected in the ListBox It doesn't want to be used. Since the index of the item always changes since the location will be pretty much always differen in the listbox. Have I lost you yet? :) OK, so basicaly I have the code that updates the listbox, and that code adds the items. I don't know how to write the code that when the certain item is selected in the listbox, that it gets used, and if it becomes 0 it gets removed. So If I have 4 Herbs, I use one of them to heal the player up it becomes 3, and if it's 0, it removes it.. Not sure how else to explain I want I need. Any Suggestions?
-
Re:Re:Re:Re:Re:Re: Questions about ListBoxes.
[Originally posted by Chris Carta]
Ok, so if I got this, you are using the items listbox index to keep track of how many are left.
I would use the listbox.itemdata to hold how many are left of an item.
Another way would be to have an array of a type.
The type would be an object with a NAME and a QUANTITY.˙ For however many unique items you have, it will be an element of the type.
Then, type(x).NAME = "Herb" and type(x).QUANTITY = 4, then 3, then 2, etc.
Am I on the right page?
Chris
-
Re:Re:Re:Re:Re:Re:Re: Questions about ListBoxes.
[Originally posted by DiS]
Um.. you lost me now. I gotta go to bed, I need some sleep! I'll be back though :) Thanks for helping.
Here's my coding. Maybe this will help or something.
In the module:
Under General Declarations...
Public Herb As Integer
Public Heal As Integer
Public Potion As Integer
Public XPotion As Integer
Public MiniEther As Integer
Public Ether As Integer
Public Elixer As Integer
This is also in the module
Public Sub UpdateList(LBox As ListBox)
LBox.Clear
LBox.AddItem "Herb˙ ˙ ˙ ˙ ˙ ˙ " & Herb
LBox.AddItem "Heal˙ ˙ ˙ ˙ " & Heal
LBox.AddItem "Potion˙ ˙ ˙ ˙ ˙ ˙ " & Potion
LBox.AddItem "X Potion˙ ˙ ˙ ˙ ˙ ˙ " & XPotion
LBox.AddItem "Mini Ether˙ ˙ ˙ ˙ ˙ ˙ " & MiniEther
LBox.AddItem "Ether˙ ˙ ˙ ˙ ˙ " & Ether
LBox.AddItem "Elixer˙ ˙ ˙ ˙ ˙ " & Elixer
LBox.AddItem "Dagger˙ ˙ ˙ ˙ ˙ ˙ " & Dagger
LBox.AddItem "Short Sword˙ ˙ ˙ ˙ ˙ ˙ " & ShortSword
LBox.AddItem "Scimitar˙ ˙ ˙ ˙ ˙ " & Scimitar
LBox.AddItem "Clothes˙ ˙ ˙ ˙ ˙ ˙ " & Clothes
LBox.AddItem "Chain Mail˙ ˙ ˙ ˙ ˙ ˙ " & ChainMail
LBox.AddItem "Gloves˙ ˙ ˙ ˙ ˙ ˙ " & Gloves
LBox.AddItem "Wooden Shield˙ ˙ ˙ ˙ ˙ ˙ " & WoodenShield
End Sub
This is in one of the forms, so player.hp is already defined..
Public Sub UseHerb()
player.Hp = player.Hp + 1 ' restores the specific HP
Herb = Herb - 1 ' uses the herb and decearses it by 1
End Sub
No coding is in the Listbox when I double click it to get teh code window.
The command button that has the caption "Use Item" has code in it that reads:
Call UpdateList(List1)
I believe that's all the coding that is pertaining to the listboxes stuff.I've ben fiddle with code int he command button, but nothing is working so I deleted it. Time for me to sleep, I am dead tired. I hope you can help cause this is really frustrating me ! And thank you for your time.
Sincerely,
˙ DiS
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks