Listbox question
Posted
#1
(In Topic #1428)
Regular

I have a follow up to question I posted last year with a slight change in what I'm trying to do. Got a lot of help from the members here but I ended up getting side tracked and not finishing it and then I somehow lost the project files. I'm loading some numbers into a listbox coming from a delimited string, easy enough-works great, then with a button click it will "process" 5 items at a time then the next button click picks up where the last click left off. With help from this forum (thanks BruceSteers & thatbruce), this is working great however what I'm trying to do now is take those 5 items and stick each of their values individually into 5 different valueboxes (valb1 through valb5). I can't get the values into the corresponding valboxes. In testing if I use a Message box, I can see each listbox value in the message but valb1.value always ends with the last value from the listbox. Here's what I have:
Code (gambas)
- ' set index at 0 if nothing is selected
- valb1.Value = Listbox1.Current.Text 'doesnt put the right values in here
- ' valb2.Value = Listbox1.Current.Text
- ' valb3.Value = Listbox1.Current.Text
- ' valb4.Value = Listbox1.Current.Text
- ' valb5.Value = Listbox1.Current.Text
- 'obviously got something wrong here. valb1.value ends up with the 5th item instead of the 1st item value
- 'Message(ListBox1.Current.Text) ' this works if I keep clicking the button
Posted
Guru

Posted
Guru

valb1.Value = Cint(listbox1[listbox1.index])
valb2.Value = Cint(listbox1[listbox1.index +1])
valb3.Value = Cint(listbox1[listbox1.index +2])
etc
Listbox.Current is only the selected item.
Posted
Regular

BruceSteers said
Use Cint() or Val() to convert the text to an integer value
Thanks Bruce but that's not the problem I'm having. My fault for not explaining very well. valb1.value is getting a number in it, just not the right one.
Say listbox1 has this:
22
56
34
58
99
44
61
23
60
41
Button3 first click should produce:
valb1.value -> 22
valb2.value -> 56
valb3.value -> 34
valb4.value -> 58
valb5.value -> 99
and the next click should start at 44 in the listbox but when I first click button3, 99 is what shows up in valb1.value instead of the expected 22.
I believe I need a With loop here?
Posted
Regular

BruceSteers said
Also use something like this…
valb1.Value = Cint(listbox1[listbox1.index])
valb2.Value = Cint(listbox1[listbox1.index +1])
valb3.Value = Cint(listbox1[listbox1.index +2])
etc
Listbox.Current is only the selected item.
Thanks Bruce. I replied before I saw your second post. I'll try that.
Posted
Regular

rj71 said
BruceSteers said
Also use something like this…
valb1.Value = Cint(listbox1[listbox1.index])
valb2.Value = Cint(listbox1[listbox1.index +1])
valb3.Value = Cint(listbox1[listbox1.index +2])
etc
Listbox.Current is only the selected item.
Thanks Bruce. I replied before I saw your second post. I'll try that.
I'm getting a "type mismatch, wanted integer got listbox_item instead"
Posted
Expert

Posted
Guru

(I think maybe it's a combo box that does that then, not listbox)
Quin has your answer.
Posted
Regular

Quincunxian said
Thanks Quin, that solved the error but I'm still getting the wrong number in valb1. It should be 22 but I still keep getting 99 and valb2 thru valb5 are empty.
Posted
Regular

rj71 said
Quincunxian said
Thanks Quin, that solved the error but I'm still getting the wrong number in valb1. It should be 22 but I still keep getting 99 and valb2 thru valb5 are empty.
Wait…I'm getting numbers in the other valueboxes but they are still wrong.
Posted
Regular

rj71 said
rj71 said
Quincunxian said
Thanks Quin, that solved the error but I'm still getting the wrong number in valb1. It should be 22 but I still keep getting 99 and valb2 thru valb5 are empty.
Wait…I'm getting numbers in the other valueboxes but they are still wrong.
OK I'm getting close here…it just seems to be skipping the first 5 items in the listbox.
EDIT: Correction…the first 4 items are skipped.
Posted
Regular

listbox1:
22
56
34
58
99
44
61
23
60
41
desired result:
valb1.value -> 22
valb2.value -> 56
valb3.value -> 34
valb4.value -> 58
valb5.value -> 99
what I'm getting instead:
valb1.value -> 99
valb2.value -> 44
valb3.value -> 61
valb4.value -> 23
valb5.value -> 60
and if I click the button a 2nd time, this would be the desired result:
valb1.value -> 44
valb2.value -> 61
valb3.value -> 23
valb4.value -> 60
valb5.value -> 41
and then keep clicking until I've moved through all the items in the listbox.
Code (gambas)
- ' set index at 0 if nothing is selected
Posted
Guru

i missed what was going on in the code. (i was at work o my phone)
maybe you can adapt something like this to work?
Code (gambas)
- ' set index at 0 if nothing is selected
Posted
Regular

Code (gambas)
- ' In case the list length isnt a multiple of 5
- groupptr += 5
Posted
Regular

BruceSteers said
because you are filling ALL 5 items 5 times.
i missed what was going on in the code. (i was at work o my phone)
maybe you can adapt something like this to work?Code (gambas)
' set index at 0 if nothing is selected
Thanks Bruce, that works. I think I understand how that new code works. Tested and it even starts back at the beginning once it reaches the end which is actually what i was hoping to do also. This code is going to be so much better than the hacky pagination I came up with
Posted
Regular

thatbruce said
Simpler, no loops!bCode (gambas)
' In case the list length isnt a multiple of 5 groupptr += 5
Thanks bruce. Brucesteers got me covered but this is totally going in my code snippet database!
Posted
Expert

Dice button will fill the listbox with 28 random numbers - not a multiple of 5 so you can check that logic for overflows.
If the list items to be transferred is less than the amount of value boxes then the remaining value boxes will be set to 0.
The checkbox show loaded will simply select the transfer list so you can check them more easier.
Note# you can change the spinbox after the first transfer click and this should be avoided - needs some logic but out-of-scope of what I was trying to show.
Cheers - Quin.
I code therefore I am
I code therefore I am
Posted
Regular

Quincunxian said
Try something like this.
Dice button will fill the listbox with 28 random numbers - not a multiple of 5 so you can check that logic for overflows.
If the list items to be transferred is less than the amount of value boxes then the remaining value boxes will be set to 0.
The checkbox show loaded will simply select the transfer list so you can check them more easier.
Note# you can change the spinbox after the first transfer click and this should be avoided - needs some logic but out-of-scope of what I was trying to show.
List Box to Valuebox transfer_001.png
Thanks Quin. After the 'Next' part I have some more stuff going on and it handles any valueboxes that end up 0 or empty but that looks interesting. I do have another question though. Progressing with this app thanks to you guys, I am now seeing the need for a "Back" button. So a forward button that advances 5 items (got that) then a Back button that goes backwards 5 item from where ever it is in the listbox. Can this be accomplished with what brucesteers posted with a minor change? I'm not sure I understand how to go 'backwards' in a for loop.
Posted
Regular

rj71 said
Quincunxian said
Try something like this.
Dice button will fill the listbox with 28 random numbers - not a multiple of 5 so you can check that logic for overflows.
If the list items to be transferred is less than the amount of value boxes then the remaining value boxes will be set to 0.
The checkbox show loaded will simply select the transfer list so you can check them more easier.
Note# you can change the spinbox after the first transfer click and this should be avoided - needs some logic but out-of-scope of what I was trying to show.
List Box to Valuebox transfer_001.png
Thanks Quin. After the 'Next' part I have some more stuff going on and it handles any valueboxes that end up 0 or empty but that looks interesting. I do have another question though. Progressing with this app thanks to you guys, I am now seeing the need for a "Back" button. So a forward button that advances 5 items (got that) then a Back button that goes backwards 5 item from where ever it is in the listbox. Can this be accomplished with what brucesteers posted with a minor change? I'm not sure I understand how to go 'backwards' in a for loop.
Is there a way to reverse the order of what is in the listbox and then just continue to use the button that is advancing by 5? gambaswiki seems to be down for me at the moment so I can't really find any info.
Posted
Guru

Posted
Regular

BruceSteers said
Thanks Bruce that works. I am going to try to figure out how to get this to work with a Next button and a Back button. I have a couple ideas for that.
Posted
Regular

Code (gambas)
- 'clear out all valb's
- valb1.Value = 0
- valb2.Value = 0
- valb3.Value = 0
- valb4.Value = 0
- valb5.Value = 0
- ' set index at 0 if nothing is selected
- 'Message(ListBox1.Current.Text)
- ListBox2.Current.Text = ListBox1.Current.Text 'Here but it isnt working
Posted
Guru

Are you expecting changing the .Text property to change the selected index?
Posted
Regular

BruceSteers said
.Text changes the .Text
Are you expecting changing the .Text property to change the selected index?
I was wanting to match the selected value/number not the index number of the listbox.
So listbox1 looks like this:
1
2
3
4
5
6
7
8
9
10
Since I'm not sure how to go truly backwards in a listbox, listbox2 is like this:
10
9
8
7
6
5
4
3
2
1
The Next button (the chunk of code you helped me with) processes 1, 2, 3, 4, and 5 in listbox1 and ends at 5 so that button click would select 5 in listbox2. Since listbox2 is in reverse order, it would process 5, 4, 3, 2, and 1 since the Back button will be listbox2. Basically want both listboxes to be in sync of whatever number is being processed, one goes forward and the other goes backwards. I'm probably doing a terrible job of explaining this.
Posted
Guru

same code as when i reversed the list and set the selected item.
ListBox2.Index = ListBox1.List.Max - ListBox1.Index
1 guest and 0 members have just viewed this.

