-
Aug 9th, 2007, 12:07 PM
#1
Thread Starter
Hyperactive Member
Deselect all items in listbox
Code:
Option Explicit
Private Sub Form_Load()
List1.AddItem "Apple"
List1.AddItem "Orange"
List1.AddItem "Banana"
List1.AddItem "Grape"
End Sub
Private Sub List1_Click()
Text1.Text = List1.List(List1.ListIndex)
End Sub
Run that and whenever you click on an item, it is selected properly. But at the end of that operation, I want the item clicked to be deselected.
On my VB, a selected item is colored blue. If I select a different item, the previously selected item turns white and the new item turns blue.
I want to never see blue.
Mac
P.S. I hope you don't suggest
Code:
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
List1.Clear
List1.AddItem "Apple"
List1.AddItem "Orange"
List1.AddItem "Banana"
List1.AddItem "Grape"
End Sub
Last edited by Mr.Mac; Aug 9th, 2007 at 12:18 PM.
-
Aug 9th, 2007, 12:28 PM
#2
Re: Deselect all items in listbox
As .ListIndex specifies the selected item, change it to the appropriate value (-1 means no selection):
Code:
List1.ListIndex = -1
-
Aug 9th, 2007, 12:32 PM
#3
Re: Deselect all items in listbox
If you have a multiselect listbox, and want to unselect everything that has been selected, you can do
Code:
Dim i As Long
For i = 0 To List1.ListCount - 1
List1.Selected(i) = False
Next
-
Aug 9th, 2007, 02:09 PM
#4
Thread Starter
Hyperactive Member
Re: Deselect all items in listbox
Thanks, guys. Good stuff. But meanwhile I did this, which isn't too bad.
Mac
Code:
Option Explicit
Dim IgnoreThis As Boolean
Private Sub Form_Activate()
List1.ListIndex = 0
End Sub
Private Sub Form_Load()
List1.AddItem "Select item below"
List1.AddItem "Apple"
List1.AddItem "Orange"
List1.AddItem "Banana"
List1.AddItem "Grape"
End Sub
Private Sub List1_Click()
If IgnoreThis Then Exit Sub
If List1.ListIndex > 0 Then
Text1.Text = List1.List(List1.ListIndex)
End If
End Sub
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
IgnoreThis = True
List1.ListIndex = 0
IgnoreThis = False
End Sub
-
Aug 9th, 2007, 02:11 PM
#5
Re: Deselect all items in listbox
or to beat the equine a little, more the following will invert the selections:
VB Code:
Dim i As Long
For i = 0 To List1.ListCount - 1
List1.Selected(i) = Not List1.Selected(i)
Next
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Aug 9th, 2007, 06:59 PM
#6
Re: Deselect all items in listbox
Code:
Dim i As Integer
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) = True Then
List1.Selected(i) = False
End If
Next i
I came up with this
-
May 20th, 2008, 03:15 AM
#7
Lively Member
Re: Deselect all items in listbox
what if i want to remove the selected lists in my listbox, my multiselect = true
this is error because if i removed list from my listbox, then the .ListCount is no longer the default value,
what will i do then
vbcode Code:
Private Sub Command2_Click()
Dim SelListCnt, listCount As Integer
listCount = List2.listCount - 1
For SelListCnt = 1 To listCount
If List2.Selected(SelListCnt) Then
List2.RemoveItem SelListCnt
End If
Next SelListCnt
End Sub
-
May 20th, 2008, 03:22 AM
#8
Lively Member
Re: Deselect all items in listbox
sorry for the reply, i got the code from the other threads... sorry
-
May 20th, 2008, 04:15 AM
#9
Re: Deselect all items in listbox
I use this in one of my apps to deselect all items in a listbox.
Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const LB_SETSEL = &H185&
Private Sub Command1_Click()
Call SendMessage(List1.hwnd, LB_SETSEL, False, ByVal -1)
End Sub
-
May 20th, 2008, 04:50 AM
#10
Re: Deselect all items in listbox
Originally Posted by Mr.Mac
Thanks, guys. Good stuff. But meanwhile I did this, which isn't too bad.
Mac
Code:
Option Explicit
Dim IgnoreThis As Boolean
Private Sub Form_Activate()
List1.ListIndex = 0
End Sub
Private Sub Form_Load()
List1.AddItem "Select item below"
List1.AddItem "Apple"
List1.AddItem "Orange"
List1.AddItem "Banana"
List1.AddItem "Grape"
End Sub
Private Sub List1_Click()
If IgnoreThis Then Exit Sub
If List1.ListIndex > 0 Then
Text1.Text = List1.List(List1.ListIndex)
End If
End Sub
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
IgnoreThis = True
List1.ListIndex = 0
IgnoreThis = False
End Sub
or to simplify
Code:
Option Explicit
Private Sub Form_Activate()
List1.ListIndex = 0
End Sub
Private Sub Form_Load()
List1.AddItem "Select item below"
List1.AddItem "Apple"
List1.AddItem "Orange"
List1.AddItem "Banana"
List1.AddItem "Grape"
End Sub
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = List1.List(List1.ListIndex)
List1.ListIndex = 0
End Sub
-
May 20th, 2008, 06:03 AM
#11
Re: Deselect all items in listbox
Originally Posted by youquijano
what if i want to remove the selected lists in my listbox,
Try this
Code:
Private Sub Command1_Click()
Dim i As Integer
Dim intCount As Integer
intCount = List1.ListCount
List1.Visible = False
For i = intCount - 1 To 0 Step -1
If List1.Selected(i) = True Then
List1.RemoveItem i
End If
Next
List1.Visible = True
End Sub
If you don't make the listbox temporarily invisible, then the routine will run slow because each item which is deleted will cause the listbox to be refreshed. By making the listbox temporarily invisible, the routine runs fast and there is no 'flickering' which takes place.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|