add and remove items in a listbox
I have been trying to make a listbox in which, everytime clicking a button a new item is added. And also, there is another button which removes selected items from the listbox. I've been unable to do both....
First. I tried to add 4 items in a row for 4 colums. The problem is that .List reset everytime the listbox.
Second. In order to remove the items I've been using .Selected() but when I run the user form it won't recognize the function...
Re: add and remove items in a listbox
What about using .AddItem to add and .RemoveItem? You can do the same thing with the .ListIndex property to determine which is selected.
VB Code:
Me.List1.List(Me.List1.ListIndex)
Re: add and remove items in a listbox
In fact I'm using .AddItem, but I can't find out how to specify that the item should be displayed in four columns. At the moment, I'm able only to insert the item just in one row and one column.
Re: add and remove items in a listbox
You need to specify that its a multicolumn listbox.
VB Code:
ListBox1.ColumnCount = 3
ListBox1.List() = MyArray 'Populate it with your multidimential array
Re: add and remove items in a listbox
I still find two errors with this code:
VB Code:
Dim Name() As Variant
ReDim Name(0, 3)
Name(0, 0) = item1
Name(0, 1) = item2
Name(0, 2) = item3
Name(0, 3) = item4
lboList.ColumnWidths = 75
lboList.ColumnCount = 4
lboList.List() = Name()
1. Everytime I run it, it adds other items in the same row
2. The .ColumnWidths instruction works only for the first column
Re: add and remove items in a listbox
Your using a munildimensial array with only one column populated. Also dont use Name as a variable as its a reserved word.
VB Code:
Dim MyName() As Variant
ReDim MyName(3, 3)
MyName(0, 0) = item1
MyName(0, 1) = item1
MyName(0, 2) = item1
MyName(0, 3) = item1
MyName(1, 0) = item2
MyName(1, 1) = item2
MyName(1, 2) = item2
MyName(1, 3) = item2
MyName(2, 0) = item3
MyName(2, 1) = item3
MyName(2, 2) = item3
MyName(2, 3) = item3
lboList.ColumnCount = 3
lboList.List() = MyName()
Re: add and remove items in a listbox
Every row has 4 items. But what if I'd like to add to select the value of each of the four items and then by clicking some button you can add this row. Then you compose another choice of 4 items and you add it to the next row. And so on.
Re: add and remove items in a listbox
You will be selecting items from where? and adding them to the listbox?
Re: add and remove items in a listbox
For each item I have some option buttons to select their value. Then, once the 4 items values have been selected you, have the 1 row vector MyName(). You add your choice to the listbox. You compose another vector and you add it. So now you have two rows in the listbox.
Re: add and remove items in a listbox
Sorry but I dont quite follow the operat. Do your items get added vertically in a particular column or do they get added horizontally?
You can also additems two dimentially too. This may be easier for your scererio
VB Code:
ListBox1.AddItem Text, Index
Re: add and remove items in a listbox
I can't find out how to attach a file, but this is basically the code I wrote. Each Add button does something different. cmdAdd1 adds with .List and cmdAdd2 with .AddItem. With cmdAdd2 you can progressively add new items to the list box and with cmdAdd1 you can only one row.
VB Code:
Option Explicit
Dim i As Long
Dim Num As String
Dim Lett As String
Dim DLett As String
Dim Vector() As Variant
Dim MyName As String
Private Sub cmdAdd2_Click()
If optProp1.Value = True Then
Num = "1"
ElseIf optProp2.Value = True Then
Num = "2"
End If
If optPropA.Value = True Then
Lett = "A"
ElseIf optPropB.Value = True Then
Lett = "B"
End If
If optPropAA.Value = True Then
DLett = "AA"
ElseIf optPropAB.Value = True Then
DLett = "AB"
End If
MyName = Num & vbTab & Lett & vbTab & DLett
With lboList
.ColumnCount = 1
.AddItem MyName
End With
End Sub
Private Sub cmdAdd1_Click()
If optProp1.Value = True Then
Num = "1"
ElseIf optProp2.Value = True Then
Num = "2"
End If
If optPropA.Value = True Then
Lett = "A"
ElseIf optPropB.Value = True Then
Lett = "B"
End If
If optPropAA.Value = True Then
DLett = "AA"
ElseIf optPropAB.Value = True Then
DLett = "AB"
End If
ReDim Vector(1, 3)
Vector(0, 0) = Num
Vector(0, 1) = Lett
Vector(0, 2) = DLett
With lboList
.ColumnCount = 3
.List() = Vector()
.ColumnWidths = 20
End With
End Sub
Private Sub cmdRemove_Click()
With lboList
For i = .ListCount - 1 To 0 Step -1
If .Selected(i) Then .RemoveItem i
Next i
End With
End Sub
Re: add and remove items in a listbox
What do you mean "attach a file"?
Re: add and remove items in a listbox
To add a file to the thread.
Re: add and remove items in a listbox
To attach a file, go to Additional Options in the Post Reply screen (towards the bottom of the screen, under where you enter your post). There's a section for attachments there.
1 Attachment(s)
Re: add and remove items in a listbox