Another Listbox question
Posted: Wednesday 12th March 2025 12:36am
I have been experimenting with a listbox and asked some questions previously. This project is evolving and I now have a form that loads up a listbox with non-sequential numbers. I have 2 buttons, one button advances 1 item/index in the Listbox. Another button goes backwards 1 item/index in the listbox. This works great and I have got it to the point where either button can advance to either the start or the end of the listbox without throwing an error...basically making the listbox a "loop". I want to introduce a "look ahead" function. So if index 0 is currently selected then I want to get the items of index 1, 2, 3, 4 and 5 and store those in a textbox and adjust accordingly when the Next button is clicked. I have this working as well even if the index is near the end of the listbox.count. Doing the "look backwards" 5 in reverse is just throwing out of bounds errors. Here is some code: (I have separated the Next and Look Ahead process into 2 buttons trying to make less messy for me). Any help to get the 5 previous items/index worked into the Back button would be appreciated....this is getting to be frustrating.
Here's the Back button:
Public Sub next_Click()
If ValueBox1.Value = ListBox1.count - 1 Then
'nothing more to advance
ValueBox1.Value = 0
listbox1.Index = 0
TextBox2.Text = ListBox1.Current.Text
ValueBox2.Value = lb1count.Value
Else
ListBox1.Index = ValueBox1.value + 1
TextBox2.Text = ListBox1.Current.Text
Wait 0.1
ValueBox1.Value = ListBox1.Index
TextBox2.Text = ListBox1.Current.Text
ValueBox2.Value = lb1count.Value - ValueBox1.Value
fiveahead_Click
Endif
Public Sub fiveahead_Click()
If ValueBox2.value > 5 Then
listbox1.index = ValueBox1.Value + 1
pos1.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 2
pos2.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 3
pos3.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 4
pos4.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 5
pos5.Text = listbox1.Current.Text
'go back to what was requested
listbox1.index = ValueBox1.Value
Endif
If ValueBox2.value = 5 Then
listbox1.index = ValueBox1.Value + 1
pos1.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 2
pos2.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 3
pos3.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 4
pos4.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - listbox1.count
pos5.Text = listbox1.Current.Text
'go back to what was requested
listbox1.index = ValueBox1.Value
Endif
If ValueBox2.value = 4 Then
listbox1.index = ValueBox1.Value + 1
pos1.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 2
pos2.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 3
pos3.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - listbox1.count
pos4.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 1)
pos5.Text = listbox1.Current.Text
'go back to what was requested
listbox1.index = ValueBox1.Value
Endif
If ValueBox2.value = 3 Then
listbox1.index = ValueBox1.Value + 1
pos1.Text = listbox1.Current.Text
listbox1.index = ValueBox1.Value + 2
pos2.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - listbox1.count
pos3.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 1)
pos4.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 2)
pos5.Text = listbox1.Current.Text
'go back to what was requested
listbox1.index = ValueBox1.Value
Endif
If ValueBox2.value = 2 Then
listbox1.index = ValueBox1.Value + 1
pos1.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - listbox1.count
pos2.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 1)
pos3.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 2)
pos4.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 3)
pos5.Text = listbox1.Current.Text
'go back to what was requested
listbox1.index = ValueBox1.Value
Endif
If ValueBox2.value = 1 Then
listbox1.index = lb1count.Value - listbox1.count
pos1.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 1)
pos2.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 2)
pos3.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 3)
pos4.Text = listbox1.Current.Text
listbox1.index = lb1count.Value - (listbox1.count - 4)
pos5.Text = listbox1.Current.Text
'go back to what was requested
listbox1.index = ValueBox1.Value
Endif
end
Here's the Back button:
Public Sub Button4_Click()
If ValueBox1.Value = 0
'nothing more to advance
ValueBox1.Value = lb1count.Value - 1
listbox1.Index = lb1count.Value - 1
TextBox2.Text = ListBox1.Current.Text
Else
ListBox1.Index = ValueBox1.value - 1
TextBox2.Text = ListBox1.Current.Text
Wait 0.1
ValueBox1.Value = ListBox1.Index
Endif
End