|
-
Aug 20th, 2000, 08:20 PM
#1
Thread Starter
Junior Member
I've been trying to remove the selected item from a listbox but I havn't got it yet. Any help is appreciated.
-
Aug 20th, 2000, 08:35 PM
#2
Use this:
Code:
Public Sub List_Remove(List As ListBox)
On Error Resume Next
If List.ListCount < 0 Then Exit Sub
List.RemoveItem List.ListIndex
End Sub
Usage:
Call List_Remove(List1)
-
Aug 21st, 2000, 01:41 AM
#3
Fanatic Member
I think he is just trying to remove the selected item only Matthew. Is that what you mean Tony900?
Try this:
Code:
Option Explicit
Private Sub Form_Load()
List1.AddItem "A"
List1.AddItem "B"
List1.AddItem "C"
List1.AddItem "D"
End Sub
Private Sub List1_Click()
List1.RemoveItem List1.ListIndex
End Sub
If it is a multiple selecting Listbox then you can try this:
Code:
Option Explicit
Private Sub Form_Load()
'PURPOSE: Change Multiselect to 1 or 2 for multiple selecting
'List1.MultiSelect = 2
'PURPOSE: Test Data
Dim int_X As Integer
Do
List1.AddItem int_X
int_X = int_X + 1
Loop Until int_X = 11
End Sub
Private Sub Form_Click()
'PURPOSE: If select then remove
Dim int_TotalItem As Integer
Do Until List1.ListCount = int_TotalItem
If List1.Selected(int_TotalItem) = True Then
List1.ListIndex = int_TotalItem
List1.RemoveItem List1.ListIndex
Else
int_TotalItem = int_TotalItem + 1
End If
Loop
End Sub
Chemically Formulated As:
Dr. Nitro
-
Aug 21st, 2000, 01:48 AM
#4
That is what Matthew's code does, but his has error handling, making sure that if the ListIndex Is greater than zero, and if that still doesnt work(if you try to remove a list item without selecting it, you get an error) he has the On Error Resume Next line to make sure there are no errors.
-
Aug 21st, 2000, 01:58 AM
#5
The code does remove the selected text in the list.
Code:
Public Sub List_Remove(List As ListBox)
On Error Resume Next
If List.ListCount < 0 Then Exit Sub 'If less than 0 then exit sub
List.RemoveItem List.ListIndex 'Remove item currently selected
End Sub
Usage:
'Call List_Remove(Object)
Call List_Remove(List1)
It's just turned into a Public Sub, rather than written out code. So this can be used in many modules . And it's easier to understand also.
-
Aug 21st, 2000, 02:06 AM
#6
Fanatic Member
Sorry Matthrew!
I thought it removes the entire list. I misunderstood. SORRY AGAIN my friend.
Chemically Formulated As:
Dr. Nitro
-
Aug 21st, 2000, 02:25 AM
#7
It's ok Nitro, I do that a lot to sometimes, misread the code/question/answer/etc. So if you want to remove the whole list, all you gotta do is use this code:
Code:
Public Sub List_Clear(List As ListBox)
List.Clear
End Sub
Usage:
Call List_Clear(List1)
Simple as that .
-
Aug 21st, 2000, 08:44 AM
#8
I don't think it's necessary to put it in a function though. Functions are mainly used to save time when you have to work with large amounts of code that are repeated throughout different parts of the project. In this case, it will take longer to call the function than to empty the ListBox.
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
|