Listbox question

Post

Posted
Rating:
#1 (In Topic #1428)
Regular
rj71 is in the usergroup ‘Regular’
Hi All,

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)

  1. Public Sub Button3_Click()
  2.  
  3. ' set index at 0 if nothing is selected
  4.  If ListBox1.Index = -1 Then ListBox1.Index = 0
  5.  For i = 0 To 4 ' do this 5 times
  6.  
  7.   valb1.Value = Listbox1.Current.Text 'doesnt put the right values in here
  8. ' valb2.Value = Listbox1.Current.Text
  9. ' valb3.Value = Listbox1.Current.Text
  10. ' valb4.Value = Listbox1.Current.Text
  11. ' valb5.Value = Listbox1.Current.Text
  12.  
  13. 'obviously got something wrong here. valb1.value ends up with the 5th item instead of the 1st item value
  14.  
  15.  
  16. 'Message(ListBox1.Current.Text) ' this works if I keep clicking the button
  17.  
  18. ValueBox1.Value = ListBox1.Index 'where the listbox index number left off
  19.  
  20.  
  21.   If ListBox1.Index = ListBox1.Count - 1 Then  ' if at end of list then deselect and exit
  22.    ListBox1.Index = -1
  23.    Break
  24.   Else
  25.     Inc ListBox1.Index  ' or goto next item
  26.  
  27.  
  28.  
  29.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
Use Cint() or Val() to convert the text to an integer value
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
rj71 is in the usergroup ‘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?
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
rj71 is in the usergroup ‘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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
rj71 is in the usergroup ‘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"
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
You need to add the Text parameter to get the string value held in the list box item.

Code (gambas)

  1. valb1.Value = Cint(listbox1[listbox1.index].Text

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
 Oops sorry I thought the listbox worked that way like a direct pointer to the array.

(I think maybe it's a combo box that does that then, not listbox)

Quin has your answer.
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
rj71 is in the usergroup ‘Regular’

Quincunxian said

You need to add the Text parameter to get the string value held in the list box item.

Code (gambas)

  1. valb1.Value = Cint(listbox1[listbox1.index].Text

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.
Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
rj71 is in the usergroup ‘Regular’

rj71 said

Quincunxian said

You need to add the Text parameter to get the string value held in the list box item.

Code (gambas)

  1. valb1.Value = Cint(listbox1[listbox1.index].Text

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.
Online now: No Back to the top

Post

Posted
Rating:
#11
Regular
rj71 is in the usergroup ‘Regular’

rj71 said

rj71 said

Quincunxian said

You need to add the Text parameter to get the string value held in the list box item.

Code (gambas)

  1. valb1.Value = Cint(listbox1[listbox1.index].Text

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.
Online now: No Back to the top

Post

Posted
Rating:
#12
Regular
rj71 is in the usergroup ‘Regular’
Getting close. Here's the updated code thanks to bruce and quin but it is now skipping the first 4 items on the first button click. Any idea why it skips the first 4 items?

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)

  1. Public Sub Button3_Click()
  2.    
  3. ' set index at 0 if nothing is selected
  4.  If ListBox1.Index = -1 Then ListBox1.Index = 0
  5.  For i = 0 To 4 ' do this 5 times
  6. valb1.Value = CInt(ListBox1[ListBox1.Index].Text)
  7. valb2.Value = CInt(listbox1[ListBox1.Index + 1].text)
  8. valb3.Value = CInt(listbox1[ListBox1.Index + 2].text)
  9. valb4.Value = CInt(listbox1[ListBox1.Index + 3].text)
  10. valb5.Value = CInt(listbox1[ListBox1.Index + 4].text)
  11. ValueBox1.Value = ListBox1.Index 'where the listbox index number left off
  12.  
  13.  
  14.   If ListBox1.Index = ListBox1.Count - 1 Then  ' if at end of list then deselect and exit
  15.    ListBox1.Index = -1
  16.    Break
  17.   Else
  18.     Inc ListBox1.Index  ' or goto next item
  19.  
  20.  
  21.  
Online now: No Back to the top

Post

Posted
Rating:
#13
Guru
BruceSteers is in the usergroup ‘Guru’
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)

  1.  
  2. Public Sub Button3_Click()
  3.  
  4. Dim hObj As Object
  5.  
  6. ' set index at 0 if nothing is selected
  7.  If ListBox1.Index = -1 Then ListBox1.Index = 0
  8.  
  9.  For i = 0 To 4 ' do this 5 times
  10.  
  11. hObj = Me["valb" & cInt(1 + i)]  ' get valb1, valb2, valb3, ect sequentially
  12. hObj.Value = CInt(ListBox1[ListBox1.Index].Text)  ' set the value
  13.  
  14. ValueBox1.Value = ListBox1.Index 'where the listbox index number is.
  15.      
  16.   If ListBox1.Index = ListBox1.Count - 1 Then  ' if at end of list then deselect and exit
  17.    ListBox1.Index = -1
  18.    Break
  19.   Else
  20.     Inc ListBox1.Index  ' or goto next item
  21.    
  22.  
Online now: No Back to the top

Post

Posted
Rating:
#14
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Simpler, no loops!

Code (gambas)

  1. Public Sub Button3_Click()
  2.  
  3.   Static groupptr As Integer
  4.  
  5.   ' In case the list length isnt a multiple of 5
  6.   ValueBox1.Value = Null
  7.   ValueBox2.Value = Null
  8.   ValueBox3.Value = Null
  9.   ValueBox4.Value = Null
  10.   ValueBox5.Value = Null
  11.  
  12.   ValueBox1.Value = CInt(ListBox1.List[groupptr])
  13.   ValueBox2.Value = CInt(ListBox1.List[groupptr + 1])
  14.   ValueBox3.Value = CInt(ListBox1.List[groupptr + 2])
  15.   ValueBox4.Value = CInt(ListBox1.List[groupptr + 3])
  16.   ValueBox5.Value = CInt(ListBox1.List[groupptr + 4])
  17.  
  18.   groupptr += 5
  19.   If groupptr >= ListBox1.List.Count Then groupptr = 0
  20.  
  21.   Return
  22.  
b

Online now: No Back to the top

Post

Posted
Rating:
#15
Regular
rj71 is in the usergroup ‘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)

  1.  
  2. Public Sub Button3_Click()
  3.  
  4. Dim hObj As Object
  5.  
  6. ' set index at 0 if nothing is selected
  7.  If ListBox1.Index = -1 Then ListBox1.Index = 0
  8.  
  9.  For i = 0 To 4 ' do this 5 times
  10.  
  11. hObj = Me["valb" & cInt(1 + i)]  ' get valb1, valb2, valb3, ect sequentially
  12. hObj.Value = CInt(ListBox1[ListBox1.Index].Text)  ' set the value
  13.  
  14. ValueBox1.Value = ListBox1.Index 'where the listbox index number is.
  15.      
  16.   If ListBox1.Index = ListBox1.Count - 1 Then  ' if at end of list then deselect and exit
  17.    ListBox1.Index = -1
  18.    Break
  19.   Else
  20.     Inc ListBox1.Index  ' or goto next item
  21.    
  22.  

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  :lol:
Online now: No Back to the top

Post

Posted
Rating:
#16
Regular
rj71 is in the usergroup ‘Regular’

thatbruce said

Simpler, no loops!

Code (gambas)

  1. Public Sub Button3_Click()
  2.  
  3.   Static groupptr As Integer
  4.  
  5.   ' In case the list length isnt a multiple of 5
  6.   ValueBox1.Value = Null
  7.   ValueBox2.Value = Null
  8.   ValueBox3.Value = Null
  9.   ValueBox4.Value = Null
  10.   ValueBox5.Value = Null
  11.  
  12.   ValueBox1.Value = CInt(ListBox1.List[groupptr])
  13.   ValueBox2.Value = CInt(ListBox1.List[groupptr + 1])
  14.   ValueBox3.Value = CInt(ListBox1.List[groupptr + 2])
  15.   ValueBox4.Value = CInt(ListBox1.List[groupptr + 3])
  16.   ValueBox5.Value = CInt(ListBox1.List[groupptr + 4])
  17.  
  18.   groupptr += 5
  19.   If groupptr >= ListBox1.List.Count Then groupptr = 0
  20.  
  21.   Return
  22.  
b


Thanks bruce. Brucesteers got me covered but this is totally going in my code snippet database!
Online now: No Back to the top

Post

Posted
Rating:
#17
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
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.
Attachment

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#18
Regular
rj71 is in the usergroup ‘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.
Online now: No Back to the top

Post

Posted
Rating:
#19
Regular
rj71 is in the usergroup ‘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.
Online now: No Back to the top

Post

Posted
Rating:
#20
Guru
BruceSteers is in the usergroup ‘Guru’
hmm, reverse like this maybe?

Code (gambas)

  1.  
  2. Dim iCurrent As Integer = ListBox1.Index ' get current selected item position
  3. ListBox1.List = ListBox1.List.Reverse()  ' reverse the list
  4. If iCurrent <> -1 Then ListBox1.Index = ListBox1.List.Max - iCurrent ' set index to previous selected item if any
  5.  
  6.  
Online now: No Back to the top

Post

Posted
Rating:
#21
Regular
rj71 is in the usergroup ‘Regular’

BruceSteers said

hmm, reverse like this maybe?

Code (gambas)

  1.  
  2. Dim iCurrent As Integer = ListBox1.Index ' get current selected item position
  3. ListBox1.List = ListBox1.List.Reverse()  ' reverse the list
  4. If iCurrent <> -1 Then ListBox1.Index = ListBox1.List.Max - iCurrent ' set index to previous selected item if any
  5.  
  6.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#22
Regular
rj71 is in the usergroup ‘Regular’
I had an idea. I introduced a 2nd listbox that gets the same delimited string but in the exact reverse order that gets populated at the same time as Listbox1. Seems like all I need to is when I click the Next button (Listbox1), the list item in the group of 5 in Listbox1 gets selected in Listbox2. So ListBox2.Current.Text = ListBox1.Current.Text  inside the For loop so Listbox2 selected item should end with the last item from Listbox1.  Then the Back button click would go "backwards" through the items in Listbox2 but not actually backwards in the listbox itself. And then vice versa when the Back button is clicked….or am I not understanding how a Listbox works?

Code (gambas)

  1. Public Sub Button3_Click()
  2. 'clear out all valb's
  3. valb1.Value = 0
  4. valb2.Value = 0
  5. valb3.Value = 0
  6. valb4.Value = 0
  7. valb5.Value = 0
  8.  
  9.  
  10.   ' set index at 0 if nothing is selected
  11.  If ListBox1.Index = -1 Then ListBox1.Index = 0
  12.  For i = 0 To 4 ' do this 5 times
  13.   hObj = Me["valb" & CInt(1 + i)]  ' get valb1, valb2, valb3, ect sequentially
  14.   hObj.Value = CInt(ListBox1[ListBox1.Index].Text)  ' set the value
  15.   'Message(ListBox1.Current.Text)
  16.   ListBox2.Current.Text = ListBox1.Current.Text     'Here but it isnt working
  17.   ValueBox1.Value = ListBox1.Index
  18.  
  19.  
  20.   If ListBox1.Index = ListBox1.Count - 1 Then  ' if at end of list then deselect and exit
  21.    ListBox1.Index = -1
  22.    Break
  23.   Else
  24.     Inc ListBox1.Index  ' or goto next item
  25.  
  26.  
  27.  
Online now: No Back to the top

Post

Posted
Rating:
#23
Guru
BruceSteers is in the usergroup ‘Guru’
 .Text changes the .Text

Are you expecting changing the .Text property to change the selected index?
Online now: No Back to the top

Post

Posted
Rating:
#24
Regular
rj71 is in the usergroup ‘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.
Online now: No Back to the top

Post

Posted
Rating:
#25
Guru
BruceSteers is in the usergroup ‘Guru’
 If you've reversed the other list then it's index is just Length-position
same code as when i reversed the list and set the selected item.

ListBox2.Index = ListBox1.List.Max - ListBox1.Index
Online now: No Back to the top
1 guest and 0 members have just viewed this.