Jun 24th, 2006, 12:35 AM
#1
Thread Starter
Addicted Member
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...
Jun 24th, 2006, 12:56 AM
#2
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)
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Jun 24th, 2006, 09:45 AM
#3
Thread Starter
Addicted Member
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.
Jun 24th, 2006, 10:43 AM
#4
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
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Jun 24th, 2006, 11:01 AM
#5
Thread Starter
Addicted Member
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
Last edited by Fonty; Jun 24th, 2006 at 11:05 AM .
Jun 24th, 2006, 11:14 AM
#6
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()
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Jun 24th, 2006, 11:33 AM
#7
Thread Starter
Addicted Member
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.
Jun 24th, 2006, 11:37 AM
#8
Re: add and remove items in a listbox
You will be selecting items from where? and adding them to the listbox?
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Jun 24th, 2006, 11:42 AM
#9
Thread Starter
Addicted Member
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.
Jun 24th, 2006, 12:14 PM
#10
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
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Jun 25th, 2006, 09:55 AM
#11
Thread Starter
Addicted Member
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
Jun 26th, 2006, 02:58 AM
#12
Re: add and remove items in a listbox
What do you mean "attach a file"?
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Jun 26th, 2006, 06:28 AM
#13
Thread Starter
Addicted Member
Re: add and remove items in a listbox
To add a file to the thread.
Jun 26th, 2006, 07:51 AM
#14
Frenzied Member
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.
Tengo mas preguntas que contestas
Jun 26th, 2006, 08:23 AM
#15
Thread Starter
Addicted Member
Re: add and remove items in a listbox
Attached Files
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