if list1.ListIndex <> -1 then List1.Selected(List1.ListIndex) = -1 This is not working. I think because I have MultiSelect on 1. Which I want.
Wondering if list1.Clear then Add Items again is overkill... or is there some other way ?
Oct 7th, 2018, 02:59 AM #2
Oct 7th, 2018, 03:04 AM #3
I did. Also = 0.
List Index seems to be the number of items selected. And setting a value is ignored.
Oct 7th, 2018, 03:16 AM #4
ListIndex is the index of the last selected item, not the count of selected items.
For multi-select lists List1.Selected(List1.ListIndex) = False deselects last selected item here ok -- just tested it.
thinBasic Code:
Private Sub Command1_Click()
List1.Selected(List1.ListIndex) = True
Private Sub Command2_Click()
List1.Selected(List1.ListIndex) = False
cheers,
Oct 7th, 2018, 07:39 AM #5
I assume that you want to deselect the item rather than remove the selected item. If so then you have your answer. If however you do want to remove the selected item as your thread title states then you have to use the .RemoveItem() method.
Oct 7th, 2018, 11:47 AM #6
Hi AlexanderBB, If you are asking removing selected items from a multiselect listbox (MultiSelect property set to Simple or Extended or else Style property set to CheckBox), the following code may help. Just check the attached sample if you wish.
Code:
Option Explicit
Private Sub Command1_Click()
Call RemoveSelectedItems(List1)
End Sub
Private Sub Form_Load()
Dim i As Integer: For i = 1 To 10: List1.AddItem "Item" & i: Next
End Sub
Private Sub RemoveSelectedItems(lst As ListBox)
Dim selitems As Integer, c As Integer
selitems = lst.SelCount
For c = 1 To selitems
Dim s As Integer
For s = 0 To lst.ListCount - 1
If lst.Selected(s) Then _
lst.RemoveItem s: Exit For
Next
Next
lst.ListIndex = -1
End Sub
Attached Files
RemoveListItems.zip (1.5 KB, 258 views)
Last edited by PGBSoft; Oct 7th, 2018 at 11:51 AM.
Oct 7th, 2018, 01:21 PM #7
To remove multiple items start with last item and work backwards.
That way you only need 1 loop.
Code:
Private Sub RemoveSelectedItems(lst As ListBox)
Dim i As Long
For i = lst.ListCount - 1 To 0 Step -1
If lst.Selected(i) Then
lst.RemoveItem i
End If
Next
End Sub
Oct 7th, 2018, 03:50 PM #8
Thanks all for the help and replies. Because MultiSelect cannot be used at Run Time I use a checkbox to toggle between two list boxes.
As they change my aim is to deselect any selected items (not remove them). {Its the Highlight I want to remove.}
lstFrom and lstTo set a Range of Years. lstFrom2 is for non consecutive years (where MultiSelect=1)
Here's my code
Code:
Private Sub chkRange_Click()
Select Case chkRange
Case 0
'Dim I As Integer
Frame2.Caption = "Set Range"
lstFrom2.Visible = False
lstFrom.Visible = True
lblFrom.Caption = "From"
lstTo.Enabled = True
lbTo.Enabled = True
If lstFrom.ListIndex <> -1 Then lstFrom.Selected(lstFrom.ListIndex) = False
If lstTo.ListIndex <> -1 Then lstTo.Selected(lstTo.ListIndex) = False
'If lstFrom2.ListIndex >= 0 Then
'lstFrom2.Clear
' For I = MinYear To MaxYear
'Me.lstFrom2.AddItem I
'Next
' End If
lstFrom2.Selected(lstFrom2.ListIndex) = False '< This not not seem to work
Case Else
Frame2.Caption = "Multi-Year"
lstFrom.Visible = False
lstFrom2.Visible = True
lstTo.Enabled = False
lbTo.Enabled = False
lblFrom.Caption = "Years"
If lstTo.ListIndex <> -1 Then lstTo.Selected(lstTo.ListIndex) = False
End Select
End Sub I find always one item always remains selected in lstFrom2. I think you guys are wrong, or am I missing something ?
My solution is the remmed code, which does what's required (but doesn't seem the best method).
I also found if listIndex <> -1 then setting it False invokes error 381. But as lstFrom2 is never -1 it avoids that error.
Last edited by AlexanderBB; Oct 7th, 2018 at 04:42 PM.
Oct 7th, 2018, 05:07 PM #9
Multiple items can be removed with one forward loop, but need bit work
Code:
Private Sub RemoveSelectedItems(lst As ListBox)
Dim s As Integer
For s = s To lst.ListCount - 1
If lst.Selected(s) Then _
lst.RemoveItem s: s = s - 1
If s = lst.ListCount - 1 Then Exit For
Next
End Sub
Last edited by PGBSoft; Oct 7th, 2018 at 10:21 PM.
Oct 7th, 2018, 06:17 PM #10
Originally Posted by AlexanderBB
I find always one item always remains selected in lstFrom2. I think you guys are wrong, or am I missing something ?
My solution is the remmed code, which does what's required (but doesn't seem the best method).
I also found if listIndex <> -1 then setting it False invokes error 381. But as lstFrom2 is never -1 it avoids that error.
might not work because the item is only highlighted, not actually selected. However, one or more of those may work. Probably the 4th and/or 5th. 1
Last edited by PGBSoft; Oct 7th, 2018 at 10:58 PM.
Oct 7th, 2018, 11:06 PM #11
Hi PGB,
1 did nothing, 2 de-selected the last (selected) item only, and 3 did the trick !
I'll change to that as I like it better than my Add Items loop.
I see now why the loop is needed (and my original method was flawed).