Listbox loses selections

OK - had a couple - but this has me stumped. I’ve searched everywhere I can think of, but no joy (so forgive what, to me, is a very “beginner” question.

First time I’ve set a listbox for “multiple selection” - goal is to tally a total from the selected rows. I am able to get a selected row count and display the count, but as soon as I write that total to a textfield, the selection(s) in my listbox disappear - gotta be something basic I’m missing - but I would love a kick in the hooter to remind me.

HELP! - and thanks!

We need to see the code to be able to make suggestions. My guess is the code that sums the numbers is where the issue is. How do you trigger the calculation of the total, does the user need to click a button ?

Format the copied code with the </> button

I figured - just thought I’d throw that out - lol.
My code is in the change event of the listbox (works fine with a messagebox, not with putting the result into the “totals” field:

Dim i, i2 As Integer
Dim iSelected As Integer = 0

If me.SelectedRowIndex < 0 then
  btn_DeleteSession.Enabled = False
  'btn_UpdateSession.Enabled = False
  mCurrentSessionID = ""
  
Else
  
  //Check to see if multiple rows have been selected
  If me.SelectedRowCount = 1 then
    btn_DeleteSession.Enabled = True
    'btn_UpdateSession.Enabled = True
    i = me.RowTagAt(me.SelectedRowIndex) // The Session ID
    mCurrentSessionID = i.ToString
    
  Elseif me.SelectedRowCount > 1 then
    // There are multiple rows selected, so we can't determine which session to be deleted - so disable the delete button
    btn_DeleteSession.Enabled = False
    mCurrentSessionID = ""
    
    //Total the amounts of the selected rows and put the total in the TotalSelected field
    For i2 = 0 to me.RowCount - 1
      If me.Selected(i2) then
        iSelected = iSelected + 1
      End If
      
    Next
    
    MessageBox(iSelected.ToString)
    fld_SelectedSessionsTotal.Text = iSelected.ToString
    
  End If
  
End If

Can you create a small example that shows this problem, zip it and upload it to the forum?
It may be easier for someone to review.

When the TextField gets focus, the selected rows of the listbox will lose their blue highlighting and will turn gray, but they should still be selected and will turn blue again when the listbox regains focus.

If you return focus to the listbox by clicking on a row, then your selection will be lost/changed.

1 Like

I have a feature request to add an optional footer to listboxes, so that totals can be displayed inside the listbox.
I´m sure it could use some more votes :slight_smile:

Like @Julia_Truchsess wrote: Try the following:

 fld_SelectedSessionsTotal.Text = iSelected.ToString
 Me.SetFocus()

OK - My apologies - was working on a project done in V2019 - opened in V2024 - after doing some homework, found the solution (to what was probably not even a problem - lol) - entire thing was resoled with “iSelected = me.SelectedRowCount” - I think I can go from here.

Thanks all!

AND - (I’m only mentioning this because I’ve been bit by this pest a few times) - If you copy an object, CHECK TO SEE IF THERE IS ANY CODE ASSOCIATED WITH THAT OBJECT (I’m embarrassed to admit - THAT was the issue) OK - step to the back of the class (I think triangular hats suit me - lol)

1 Like