Scrolling selectedrowindex listbox

Hi,
I have a code, detecting the last input from textfield into listbox.
this code below is working as long as the rows is visible on the listbox.
the listbox has 500 rows, how to make the listbox scrolling the position into the top position while the bFound is true.

any helps

thanks
arief

dim i as integer
dim s as string
s = EditField3.Text.trim
timer2.Enabled=true

for i = 0 to listbox6.ListCount -1
  Dim o as integer
  dim bFound as boolean
  bFound = false
  dim lr as integer 
  
  if listbox6.cell(i,0).Trim = s  then
    for o= 0 to Listbox1.ListCount-1
      if Listbox1.Cell(o,0)=listbox6.cell(i,0) then
        
        Listbox1.Cell(o,0)=listbox6.cell(i,0).Trim
        Listbox1.Cell(o,1)=l_date.text
        Listbox1.Cell(o,2)=l_time.text
        Listbox1.Cell(o,3) =listbox6.cell(i,1).Trim
        Listbox1.cell(o,4) =  listbox6.cell(i,2).Trim
        label2.text= "Terakhir di Scan : "+listbox6.cell(i,1).Trim
        Listbox1.cell(o,5) =  str(val(Listbox1.cell(o,5)) +val(EditField2.text))
        bFound = true
        lr=o 
      end if
    next o
    
    if bfound = false  then
      Listbox1.AddRow  ""
      lr  = Listbox1.LastAddedRowIndex 
      Listbox1.cell(Listbox1.LastIndex,0) =  listbox6.cell(i,0)
      Listbox1.cell(Listbox1.LastIndex,1) =  l_date.text
      Listbox1.cell(Listbox1.LastIndex,2) =  l_time.text
      Listbox1.cell(Listbox1.LastIndex,3) =  listbox6.cell(i,1).Trim
      Listbox1.cell(Listbox1.LastIndex,4) =  listbox6.cell(i,2).Trim
      label2.text= "Terakhir di Scan : "+listbox6.cell(i,1).Trim
      Listbox1.cell(Listbox1.LastIndex,5) =  str(val(Listbox1.cell(o,5)) +val(EditField2.text))
    end if
    EditField3.text=""
    Listbox1.SelectedRowIndex=lr
    EditField3.SetFocus
  end if
next i

check_last

Listbox1.ScrollPosition=Listbox1.LastRowIndex

Listbox1.ScrollPosition=lr

hi,
The listbox still not scrolling into top row with exact text.

thanks
arief

Can you share a demo? No problem here:
ezgif.com-video-to-gif-converted

  1. When you find the match (and set bfound = True) why do you not exit the for loop immediately?
  2. The code block with if bfound = false then ... should be outside and after the for loop.
1 Like

Have you checked with a breakpoint whether the code goes in “if bfound = false then” and the value of lr?

Also, since bFound is declared inside the loop (for i), it always starts as false. You don’t need to explicitly write “bFound = false” just after. That’s not an issue, though.

1 Like

If it is not obvious from the above comments:

dim bFound as boolean
dim lr as integer

are inside the loop, they will always reset to false/0 on each for. I’m sure that is not what you want.

1 Like

I made some mistake, putting this outside the loop.
now its worked nicely.

thanks

regards,
arief