How to have a second list box accept multiple selections with an add button


Posted by Kurt on October 19, 2000 9:26 AM

Hello everyone,

How would I use an add button on a user form with two list boxes to accept either a single or multiple selection from the first list box to be moved to the second list box?

Please help. Nobody responded to my earlier question.

Thanks in advance,

Kurt

Posted by Tim Francis-Wright on October 19, 2000 3:11 PM

I've assumed that the Add button is CommandButton 1,
and that the listboxes in question are Listbox1 and Listbox2.
Finally, I've assumed that Listbox1 has MultiSelect
set to either 1 (click on each selection) or to
2 (control-click on each additional selection).

The code for the button would be like this:

Private Sub CommandButton1_Click()
ListBox2.Clear
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
ListBox2.AddItem ListBox1.List(i)
End If
Next i
End Sub

HTH!

Posted by Kurt on October 20, 2000 7:05 AM

Hello Tim,

Thanks for your efforts!! The code works great except for a minor detail. I have 8 columns of data in list box 1 and list box 2 only accepts the first column.

How do I fix this?

Thanks again for your help and Celia's help!!

Kurt

Posted by Tim Francis-Wright on October 20, 2000 8:29 AM

I figured it out. To add the second and successive
columns of data, you need to use the List property
(the Additem property only works for the first
column). This probably made sense to someone
on the Excel Development Team--at least I hope
it made sense to them.

Anyway, the line about ColumnCount is almost
surely superfluous, because you have probably
already set the ColumnCount and ColumnWidths
properties for Listbox2 in some other bit of code.

Private Sub CommandButton1_Click()

i2 = 0
ListBox2.Clear
ListBox2.ColumnCount = ListBox1.ColumnCount '***
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
ListBox2.AddItem ListBox1.List(i)
If ListBox1.ColumnCount > 1 Then
For j = 1 To ListBox1.ColumnCount - 1
ListBox2.List(i2, j) = ListBox1.List(i, j)
Next j
End If
i2 = i2 + 1
End If
Next i
End Sub


HTH!

Posted by Kurt on October 20, 2000 8:53 AM

Hello Tim,

It works perfectly!! God Bless You and Celia and Ryan for always helping!!!

One last question. Please don't shoot me, I am just the piano player, but I think I have answered my question with a question.

To use a Remove Button, I would use the same code except use ListBox2.RemoveItem ListBox1.List(i)

I hope my thinking is correct!!

Again you guys are the greatest and have been a great God send!! Have a great weekend!!

Posted by Tim Francis-Wright on October 20, 2000 9:45 AM


Actually, to delete a row, you only need to
use RemoveItem once.
ListBox2.RemoveItem(i)

HTH

Posted by Kurt on October 20, 2000 12:46 PM

Re: How to change the color of the list selector to blue and how to toggle it on and off in the first and second list box.

Hello Tim,

Thanks for your earlier great help!! Now, how do you change the color of the selction bar in the first and second list box to blue and how do you toggle it on and off.

Thanks,

Kurt

Posted by Tim Francis-Wright on October 20, 2000 1:55 PM

Is Color of the list selector a Windows-level setting?

Unfortunately, I believe that the color of the
election bar is determined by the Windows
setting for selected items. (You can set
bordercolor, backcolor, and forecolor, among
other things.)

Plus, none of these things applies
to particular items in the list, only to the
list itself.

Can anyone prove me wrong?



Posted by Ivan Moala on October 22, 2000 2:02 AM

Re: Is Color of the list selector a Windows-level setting?

The Selection Bar color is indeed determined from
your Windows settings...BUT if you want to change this
and have the option of toggling the color then
you can try this routine;

Option Explicit


Public Declare Function SetSysColors Lib "user32" _
(ByVal nChanges As Long, lpSysColor As Long, _
lpColorValues As Long) As Long

Public Const COLOR_HIGHLIGHT = 13 'Selected item background color

Sub SetColour(lngColour As Long)
' Set the system colour for the Selection
SetSysColors 1, COLOR_HIGHLIGHT, lngColour
End Sub

Then in your userform;
Put in a toggle button

Private Sub ToggleButton1_Click()
If ToggleButton1.Value = False Then
SetColour (RGB(255, 255, 0)) 'Yellow
Else
'SetColour (RGB(0, 0, 255)) 'Lighter Blue
SetColour (RGB(0, 0, 128)) 'Darker Blue
End If
End Sub


Ivan