Another Listbox question

Post

Posted
Rating:
#1 (In Topic #1434)
Regular
rj71 is in the usergroup ‘Regular’
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.

Code (gambas)

  1. Public Sub next_Click()
  2. If ValueBox1.Value = ListBox1.count - 1 Then
  3.   'nothing more to advance
  4.   ValueBox1.Value = 0
  5.   listbox1.Index = 0
  6.   TextBox2.Text = ListBox1.Current.Text
  7.   ValueBox2.Value = lb1count.Value
  8.   ListBox1.Index = ValueBox1.value + 1
  9.   TextBox2.Text = ListBox1.Current.Text
  10.  Wait 0.1
  11.  ValueBox1.Value = ListBox1.Index
  12.  TextBox2.Text = ListBox1.Current.Text
  13.  ValueBox2.Value = lb1count.Value - ValueBox1.Value
  14.  
  15.  fiveahead_Click
  16.  
  17.  
  18. Public Sub fiveahead_Click()
  19. If ValueBox2.value > 5 Then
  20.   listbox1.index = ValueBox1.Value + 1
  21.    pos1.Text = listbox1.Current.Text
  22.   listbox1.index = ValueBox1.Value + 2
  23.    pos2.Text = listbox1.Current.Text
  24.   listbox1.index = ValueBox1.Value + 3
  25.    pos3.Text = listbox1.Current.Text
  26.    listbox1.index = ValueBox1.Value + 4
  27.   pos4.Text = listbox1.Current.Text
  28.   listbox1.index = ValueBox1.Value + 5
  29.   pos5.Text = listbox1.Current.Text
  30.  
  31.   'go back to what was requested
  32.   listbox1.index = ValueBox1.Value
  33.  
  34.  
  35.  If ValueBox2.value = 5 Then
  36.   listbox1.index = ValueBox1.Value + 1
  37.   pos1.Text = listbox1.Current.Text
  38.    listbox1.index = ValueBox1.Value + 2
  39.    pos2.Text = listbox1.Current.Text
  40.    listbox1.index = ValueBox1.Value + 3
  41.    pos3.Text = listbox1.Current.Text
  42.    listbox1.index = ValueBox1.Value + 4
  43.    pos4.Text = listbox1.Current.Text
  44.    
  45.    listbox1.index = lb1count.Value - listbox1.count
  46.    pos5.Text = listbox1.Current.Text
  47.   'go back to what was requested
  48.   listbox1.index = ValueBox1.Value
  49.  
  50.  If ValueBox2.value = 4 Then
  51.   listbox1.index = ValueBox1.Value + 1
  52.   pos1.Text = listbox1.Current.Text
  53.    listbox1.index = ValueBox1.Value + 2
  54.    pos2.Text = listbox1.Current.Text
  55.    listbox1.index = ValueBox1.Value + 3
  56.    pos3.Text = listbox1.Current.Text
  57.    listbox1.index = lb1count.Value - listbox1.count
  58.    pos4.Text = listbox1.Current.Text
  59.    
  60.    listbox1.index = lb1count.Value - (listbox1.count - 1)
  61.    pos5.Text = listbox1.Current.Text
  62.   'go back to what was requested
  63.   listbox1.index = ValueBox1.Value
  64.  
  65. If ValueBox2.value = 3 Then
  66.   listbox1.index = ValueBox1.Value + 1
  67.   pos1.Text = listbox1.Current.Text
  68.    listbox1.index = ValueBox1.Value + 2
  69.    pos2.Text = listbox1.Current.Text
  70.    listbox1.index = lb1count.Value - listbox1.count
  71.    pos3.Text = listbox1.Current.Text
  72.    listbox1.index = lb1count.Value - (listbox1.count - 1)
  73.    pos4.Text = listbox1.Current.Text
  74.    
  75.    listbox1.index = lb1count.Value - (listbox1.count - 2)
  76.    pos5.Text = listbox1.Current.Text
  77.   'go back to what was requested
  78.   listbox1.index = ValueBox1.Value
  79.  
  80.  If ValueBox2.value = 2 Then
  81.   listbox1.index = ValueBox1.Value + 1
  82.   pos1.Text = listbox1.Current.Text
  83.    listbox1.index = lb1count.Value - listbox1.count
  84.    pos2.Text = listbox1.Current.Text
  85.    listbox1.index = lb1count.Value - (listbox1.count - 1)
  86.    pos3.Text = listbox1.Current.Text
  87.    listbox1.index = lb1count.Value - (listbox1.count - 2)
  88.    pos4.Text = listbox1.Current.Text
  89.    listbox1.index = lb1count.Value - (listbox1.count - 3)
  90.    pos5.Text = listbox1.Current.Text
  91.   'go back to what was requested
  92.   listbox1.index = ValueBox1.Value
  93.  
  94.  If ValueBox2.value = 1 Then
  95.   listbox1.index = lb1count.Value - listbox1.count
  96.   pos1.Text = listbox1.Current.Text
  97.    listbox1.index = lb1count.Value - (listbox1.count - 1)
  98.    pos2.Text = listbox1.Current.Text
  99.    listbox1.index = lb1count.Value - (listbox1.count - 2)
  100.    pos3.Text = listbox1.Current.Text
  101.    listbox1.index = lb1count.Value - (listbox1.count - 3)
  102.    pos4.Text = listbox1.Current.Text
  103.    listbox1.index = lb1count.Value - (listbox1.count - 4)
  104.    pos5.Text = listbox1.Current.Text
  105.   'go back to what was requested
  106.   listbox1.index = ValueBox1.Value
  107.  
  108.  
  109.  


Here's the Back button:

Code (gambas)

  1.  
  2. Public Sub Button4_Click()
  3.  
  4. If ValueBox1.Value = 0
  5.   'nothing more to advance
  6.   ValueBox1.Value = lb1count.Value - 1
  7.   listbox1.Index = lb1count.Value - 1
  8.  
  9.   TextBox2.Text = ListBox1.Current.Text
  10.     ListBox1.Index = ValueBox1.value - 1
  11.     TextBox2.Text = ListBox1.Current.Text
  12.   Wait 0.1
  13.   ValueBox1.Value = ListBox1.Index
  14.  
  15.  
  16.  
  17.  
Online now: No Back to the top

Post

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

Code (gambas)

  1.  
  2. Public Sub Button4_Click()
  3.  
  4. If ValueBox1.Value = 0
  5.   'nothing more to advance
  6.   ValueBox1.Value = lb1count.Value - 1
  7.  
  8.   Object.Lock(ListBox1) ' stop events
  9.   listbox1.Index = lb1count.Value - 1
  10.    Object.UnLock(ListBox1) ' restart events
  11.  
  12.   TextBox2.Text = ListBox1.Current.Text
  13.   Object.Lock(ListBox1) ' stop events
  14.     ListBox1.Index = ValueBox1.value - 1
  15.   Object.UnLock(ListBox1) ' stop events
  16.     TextBox2.Text = ListBox1.Current.Text
  17.   '  Wait 0.1 ' nope , don't do it, it really should not be needed,
  18.   ValueBox1.Value = ListBox1.Index
  19.    
  20.  

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

Code (gambas)

  1.  
  2. Public Sub SetProp(Obj As Object, Prop As String, Value As Variant)
  3.  
  4.   Object.Lock(Obj) ' stop events
  5.   Object.SetProperty(Obj, Prop, Value) ' set property
  6.   Object.UnLock(Obj) ' restart events
  7.  
  8.  
  9.  
SetProp(listbox1, "Index", lb1count.Value - 1)

Note: some events can trigger even if locked, depending on the control.
Online now: No Back to the top

Post

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

Post

Posted
Rating:
#4
Regular
rj71 is in the usergroup ‘Regular’
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!
Online now: No Back to the top
1 guest and 0 members have just viewed this.