Another Listbox question

Post your Gambas programming questions here.
Post Reply
rj71
Posts: 127
Joined: Thursday 13th April 2023 6:39pm
Location: Vancouver WA

Another Listbox question

Post by rj71 »

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.

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


User avatar
BruceSteers
Posts: 2070
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Another Listbox question

Post by BruceSteers »

Stop using Wait unless you need the event loop to cycle.

My guess is you have things also set to events like ListBox1_Click.

If you set ListBox1.Index and then use Wait the ListBox1_Click event will trigger before you are done.

If you want to set something like a listbox but not trigger it's events try using Object.Lock()


Public Sub Button4_Click()
 
If ValueBox1.Value = 0
  'nothing more to advance
  ValueBox1.Value = lb1count.Value - 1

  Object.Lock(ListBox1) ' stop events
  listbox1.Index = lb1count.Value - 1
   Object.UnLock(ListBox1) ' restart events

  TextBox2.Text = ListBox1.Current.Text
Else 
  Object.Lock(ListBox1) ' stop events
    ListBox1.Index = ValueBox1.value - 1
  Object.UnLock(ListBox1) ' stop events
    TextBox2.Text = ListBox1.Current.Text
  '  Wait 0.1 ' nope , don't do it, it really should not be needed,
  ValueBox1.Value = ListBox1.Index
   
 Endif 
End


All my programs have a function called SetProp to set an objects property by locking it first so i can set properties without triggering events


Public Sub SetProp(Obj As Object, Prop As String, Value As Variant)

  Object.Lock(Obj) ' stop events
  Object.SetProperty(Obj, Prop, Value) ' set property
  Object.UnLock(Obj) ' restart events

End


SetProp(listbox1, "Index", lb1count.Value - 1)

Note: some events can trigger even if locked, depending on the control.
If at first you don't succeed , try doing something differently.
BruceS
rj71
Posts: 127
Joined: Thursday 13th April 2023 6:39pm
Location: Vancouver WA

Re: Another Listbox question

Post by rj71 »

Thanks Bruce. I actually don't remember why I put the Wait in there. I must have thought I needed it at some point. There is also no listbox_click event nor will there be. Eventually, the plan would be to have the listbox hidden so no clicking on it allowed as the buttons will do the movement. I will take out the Wait though.The concept of what I'm trying to accomplish seems simple but it's proving to not be. I'm exhausted so I will get back at it tomorrow and try more things.
rj71
Posts: 127
Joined: Thursday 13th April 2023 6:39pm
Location: Vancouver WA

Re: Another Listbox question

Post by rj71 »

I think I have solved this. Pretty much the same way as looking ahead 5 but for some reason I made more complicated than it needed to be. Thanks for your input Bruce!
Post Reply