Selected Last added row

Hi,

I want to find out last added row in a listbox and selected the row.

There is no error, but the last added row is not selected.

I am using 2021 R1.1

dim i as integer
dim s as string
s = t_kode.Text.trim

for i = 0 to lstPrice.ListCount -1
  
  Dim o as integer
  dim bFound as boolean
  
  bFound = false
  
  if lstPrice.cell(i,0).Trim = s and s<>"" then
    for o= 0 to Listbox1.ListCount-1
      if Listbox1.Cell(o,1)=lstPrice.cell(i,0) then
        Listbox1.Cell(o,1)=lstPrice.cell(i,0).Trim
        Listbox1.Cell(o,2)=lstPrice.cell(i,1).Trim
        Listbox1.Cell(o,3)=lstPrice.cell(i,2).Trim
        Listbox1.Cell(o,0)=str(val(Listbox1.cell(o,0))+val(cmbQty.text))
        
        bFound = true
        t_kode.SetFocus
        t_kode.Text=""
        Listbox1.SelectedRowIndex=Listbox1.LastAddedRowIndex
      end if
    next o
    
    if bfound = false  then
      Listbox1.AddRow  ""
      Listbox1.cell(Listbox1.LastIndex,0) =  cmbQty.text
      Listbox1.cell(Listbox1.LastIndex,1) =  lstPrice.cell(i,0)
      Listbox1.cell(Listbox1.LastIndex,2) =  lstPrice.cell(i,1)
      Listbox1.cell(Listbox1.LastIndex,3) =  lstPrice.cell(i,2)
      
      dim z as integer
      z=CDbl(listbox1.cell(o,0))* CDbl(window1.listbox1.cell(o,3))
      Listbox1.cell(o,4) =  Format(z , "###,##0")
      
      t_kode.SetFocus
      t_kode.Text=""
      Listbox1.SelectedRowIndex=Listbox1.LastAddedRowIndex
    end if

Any Helps ?

Thanks
Arief

What happens if you remove:

Listbox1.SelectedRowIndex=Listbox1.LastAddedRowIndex

from the for/next o and for/next i and you put it after next i?

Hi,
Still the same, nothing selected.
But If I do removing in this section, its running, but I have to click the button twice to make it work.

if bfound = false  then
      Listbox1.AddRow  ""
      Listbox1.cell(Listbox1.LastIndex,0) =  cmbQty.text
      Listbox1.cell(Listbox1.LastIndex,1) =  lstPrice.cell(i,0)
      Listbox1.cell(Listbox1.LastIndex,2) =  lstPrice.cell(i,1)
      Listbox1.cell(Listbox1.LastIndex,3) =  lstPrice.cell(i,2)
      
      dim z as integer
      z=CDbl(listbox1.cell(o,0))* CDbl(window1.listbox1.cell(o,3))
      Listbox1.cell(o,4) =  Format(z , "###,##0")
      
      t_kode.SetFocus
      t_kode.Text=""

    end if

thanks
regards
arief

The first part of the code selects the last added row, even if what you are searching for is on some other row

try this:


dim lr as integer 

if lstPrice.cell(i,0).Trim = s and s<>"" then
    for o= 0 to Listbox1.ListCount-1
      if Listbox1.Cell(o,1)=lstPrice.cell(i,0) then
        Listbox1.Cell(o,1)=lstPrice.cell(i,0).Trim
        Listbox1.Cell(o,2)=lstPrice.cell(i,1).Trim
        Listbox1.Cell(o,3)=lstPrice.cell(i,2).Trim
        Listbox1.Cell(o,0)=str(val(Listbox1.cell(o,0))+val(cmbQty.text))

        bFound = true
        lr=o   
      end if
    next o
    
if not bfound then


      Listbox1.AddRow  ""
      lr  = Listbox1.LastAddedRowIndex 

      Listbox1.cell(lr,0) =  cmbQty.text
      Listbox1.cell(lr,1) =  lstPrice.cell(i,0)   //what is the value of i ?
      Listbox1.cell(lr,2) =  lstPrice.cell(i,1)
      Listbox1.cell(lr,3) =  lstPrice.cell(i,2)
      
      dim z as integer
      z=  CDbl(listbox1.cell(lr,0))   *   CDbl(listbox1.cell(lr,3))
      Listbox1.cell(lr,4) =  Format(z , "###,##0")
  
    end if


    Listbox1.SelectedRowIndex=lr
  t_kode.SetFocus
   t_kode.Text=""
1 Like

Yes,
Its working perfectly… thanks for all the helps.

regards,
arief