listbox

listbox

Anonymous
Not applicable
658 Views
8 Replies
Message 1 of 9

listbox

Anonymous
Not applicable
if i have a listbox with the multiselect property set to extended how do i
create a button to remove all the selected items or add them to another
listbox?

thanks,
Rob

w2k, vb6, a2k2
0 Likes
659 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
you can use the removeitem method. here's an example that you may be able to
use in your code.

lstExistBlks.RemoveItem (lstExistBlks.ListIndex)


"Rob Tomson" wrote in message
news:EFF239DEF7733338448E318E7BF69865@in.WebX.maYIadrTaRb...
> if i have a listbox with the multiselect property set to extended how do i
> create a button to remove all the selected items or add them to another
> listbox?
>
> thanks,
> Rob
>
> w2k, vb6, a2k2
>
>
0 Likes
Message 3 of 9

Anonymous
Not applicable
i know how to do that but if i have more than one item selected then
.listindex only returns the currently selected item. how do i remove (or
add) them all?

"Russell Cygan" wrote in message
news:14DA20A663C69A9FAC2E00E71164F04C@in.WebX.maYIadrTaRb...
> you can use the removeitem method. here's an example that you may be able
to
> use in your code.
>
> lstExistBlks.RemoveItem (lstExistBlks.ListIndex)
>
>
> "Rob Tomson" wrote in message
> news:EFF239DEF7733338448E318E7BF69865@in.WebX.maYIadrTaRb...
> > if i have a listbox with the multiselect property set to extended how do
i
> > create a button to remove all the selected items or add them to another
> > listbox?
> >
> > thanks,
> > Rob
> >
> > w2k, vb6, a2k2
> >
> >
>
>
0 Likes
Message 4 of 9

Anonymous
Not applicable
From the MSDN Library:

MultiSelect Property Example
This example fills a ListBox control with the names of your screen fonts and
illustrates how the MultiSelect property affects the behavior of a ListBox.
To try this example, create two ListBox controls and a CommandButton control
on a form. In the first ListBox, set the MultiSelect property to 1 or 2. At
run time, select several items in the first ListBox, and then click the
CommandButton. All selected items are displayed in the second ListBox. Run
the example several times with different settings of the MultiSelect
property. Paste the code into the Declarations section, and then press F5 to
run the program.

Private Sub Form_Load ()
Dim I ' Declare variable.
' Fill the list box with screen font names.
For I = 0 To Screen.FontCount - 1
List1.AddItem Screen.Fonts(I)
Next I
End Sub

Private Sub Command1_Click ()
Dim I ' Declare variable.
' Clear all items from the list.
List2.Clear
' If an item is selected, add it to List2.
For I = 0 To List1.ListCount - 1
If List1.Selected(I) Then
List2.AddItem List1.List(I)
End If
Next I
End Sub

HTH

--
Veign
Designing Solutions
www.veign.com
VB Code Samples & Projects
www.veign.com/information/info_main.html


"Rob Tomson" wrote in message
news:2F84D10178904090E0B54836376BDA78@in.WebX.maYIadrTaRb...
> i know how to do that but if i have more than one item selected then
> .listindex only returns the currently selected item. how do i remove (or
> add) them all?
>
> "Russell Cygan" wrote in message
> news:14DA20A663C69A9FAC2E00E71164F04C@in.WebX.maYIadrTaRb...
> > you can use the removeitem method. here's an example that you may be
able
> to
> > use in your code.
> >
> > lstExistBlks.RemoveItem (lstExistBlks.ListIndex)
> >
> >
> > "Rob Tomson" wrote in message
> > news:EFF239DEF7733338448E318E7BF69865@in.WebX.maYIadrTaRb...
> > > if i have a listbox with the multiselect property set to extended how
do
> i
> > > create a button to remove all the selected items or add them to
another
> > > listbox?
> > >
> > > thanks,
> > > Rob
> > >
> > > w2k, vb6, a2k2
> > >
> > >
> >
> >
>
>
0 Likes
Message 5 of 9

Anonymous
Not applicable
have you tried creating an array of all the selected items as you select
them so you can use the removeitem using these as listindexes rather than
just the currently selected one?

"Rob Tomson" wrote in message
news:2F84D10178904090E0B54836376BDA78@in.WebX.maYIadrTaRb...
> i know how to do that but if i have more than one item selected then
> .listindex only returns the currently selected item. how do i remove (or
> add) them all?
>
> "Russell Cygan" wrote in message
> news:14DA20A663C69A9FAC2E00E71164F04C@in.WebX.maYIadrTaRb...
> > you can use the removeitem method. here's an example that you may be
able
> to
> > use in your code.
> >
> > lstExistBlks.RemoveItem (lstExistBlks.ListIndex)
> >
> >
> > "Rob Tomson" wrote in message
> > news:EFF239DEF7733338448E318E7BF69865@in.WebX.maYIadrTaRb...
> > > if i have a listbox with the multiselect property set to extended how
do
> i
> > > create a button to remove all the selected items or add them to
another
> > > listbox?
> > >
> > > thanks,
> > > Rob
> > >
> > > w2k, vb6, a2k2
> > >
> > >
> >
> >
>
>
0 Likes
Message 6 of 9

Anonymous
Not applicable
there has to be an easier way to do this...


"Russell Cygan" wrote in message
news:AFD523A8E50165D7849B9C4EE33612D1@in.WebX.maYIadrTaRb...
> have you tried creating an array of all the selected items as you select
> them so you can use the removeitem using these as listindexes rather than
> just the currently selected one?
>
> "Rob Tomson" wrote in message
> news:2F84D10178904090E0B54836376BDA78@in.WebX.maYIadrTaRb...
> > i know how to do that but if i have more than one item selected then
> > .listindex only returns the currently selected item. how do i remove
(or
> > add) them all?
> >
> > "Russell Cygan" wrote in message
> > news:14DA20A663C69A9FAC2E00E71164F04C@in.WebX.maYIadrTaRb...
> > > you can use the removeitem method. here's an example that you may be
> able
> > to
> > > use in your code.
> > >
> > > lstExistBlks.RemoveItem (lstExistBlks.ListIndex)
> > >
> > >
> > > "Rob Tomson" wrote in message
> > > news:EFF239DEF7733338448E318E7BF69865@in.WebX.maYIadrTaRb...
> > > > if i have a listbox with the multiselect property set to extended
how
> do
> > i
> > > > create a button to remove all the selected items or add them to
> another
> > > > listbox?
> > > >
> > > > thanks,
> > > > Rob
> > > >
> > > > w2k, vb6, a2k2
> > > >
> > > >
> > >
> > >
> >
> >
>
>
0 Likes
Message 7 of 9

Anonymous
Not applicable
thanks, that works like a champ but now how do i remove the selected items?


"Veign" wrote in message
news:A825845389D422F7EC8288E9B8630E81@in.WebX.maYIadrTaRb...
> From the MSDN Library:
>
> MultiSelect Property Example
> This example fills a ListBox control with the names of your screen fonts
and
> illustrates how the MultiSelect property affects the behavior of a
ListBox.
> To try this example, create two ListBox controls and a CommandButton
control
> on a form. In the first ListBox, set the MultiSelect property to 1 or 2.
At
> run time, select several items in the first ListBox, and then click the
> CommandButton. All selected items are displayed in the second ListBox. Run
> the example several times with different settings of the MultiSelect
> property. Paste the code into the Declarations section, and then press F5
to
> run the program.
>
> Private Sub Form_Load ()
> Dim I ' Declare variable.
> ' Fill the list box with screen font names.
> For I = 0 To Screen.FontCount - 1
> List1.AddItem Screen.Fonts(I)
> Next I
> End Sub
>
> Private Sub Command1_Click ()
> Dim I ' Declare variable.
> ' Clear all items from the list.
> List2.Clear
> ' If an item is selected, add it to List2.
> For I = 0 To List1.ListCount - 1
> If List1.Selected(I) Then
> List2.AddItem List1.List(I)
> End If
> Next I
> End Sub
>
> HTH
>
> --
> Veign
> Designing Solutions
> www.veign.com
> VB Code Samples & Projects
> www.veign.com/information/info_main.html
>
>
> "Rob Tomson" wrote in message
> news:2F84D10178904090E0B54836376BDA78@in.WebX.maYIadrTaRb...
> > i know how to do that but if i have more than one item selected then
> > .listindex only returns the currently selected item. how do i remove
(or
> > add) them all?
> >
> > "Russell Cygan" wrote in message
> > news:14DA20A663C69A9FAC2E00E71164F04C@in.WebX.maYIadrTaRb...
> > > you can use the removeitem method. here's an example that you may be
> able
> > to
> > > use in your code.
> > >
> > > lstExistBlks.RemoveItem (lstExistBlks.ListIndex)
> > >
> > >
> > > "Rob Tomson" wrote in message
> > > news:EFF239DEF7733338448E318E7BF69865@in.WebX.maYIadrTaRb...
> > > > if i have a listbox with the multiselect property set to extended
how
> do
> > i
> > > > create a button to remove all the selected items or add them to
> another
> > > > listbox?
> > > >
> > > > thanks,
> > > > Rob
> > > >
> > > > w2k, vb6, a2k2
> > > >
> > > >
> > >
> > >
> >
> >
>
>
0 Likes
Message 8 of 9

Anonymous
Not applicable
This has worked for me:

Private Sub CommandButton2_Click()
Dim nEntryNumber As Integer
Dim j As Integer
Dim flag As Integer
flag = 0
nEntryNumber = 0

Do While nEntryNumber < ListBox1.ListCount
If ListBox1.Selected(nEntryNumber) = True Then
For j = 0 To ListBox2.ListCount - 1
ListBox2.ListIndex = j
If ListBox1.List(nEntryNumber) = ListBox2.List(j) Then
flag = 1
End If
Next j
If flag = 0 Then
ListBox2.AddItem ListBox1.List(nEntryNumber)
End If
ListBox1.RemoveItem nEntryNumber

Else
nEntryNumber = nEntryNumber + 1
End If
flag = 0
Loop

End Sub



"Rob Tomson" wrote in message
news:4D6E0A6CDBFE15229BFB58B1E0D8CBD7@in.WebX.maYIadrTaRb...
> there has to be an easier way to do this...
>
>
> "Russell Cygan" wrote in message
> news:AFD523A8E50165D7849B9C4EE33612D1@in.WebX.maYIadrTaRb...
> > have you tried creating an array of all the selected items as you select
> > them so you can use the removeitem using these as listindexes rather
than
> > just the currently selected one?
> >
> > "Rob Tomson" wrote in message
> > news:2F84D10178904090E0B54836376BDA78@in.WebX.maYIadrTaRb...
> > > i know how to do that but if i have more than one item selected then
> > > .listindex only returns the currently selected item. how do i remove
> (or
> > > add) them all?
> > >
> > > "Russell Cygan" wrote in message
> > > news:14DA20A663C69A9FAC2E00E71164F04C@in.WebX.maYIadrTaRb...
> > > > you can use the removeitem method. here's an example that you may be
> > able
> > > to
> > > > use in your code.
> > > >
> > > > lstExistBlks.RemoveItem (lstExistBlks.ListIndex)
> > > >
> > > >
> > > > "Rob Tomson" wrote in message
> > > > news:EFF239DEF7733338448E318E7BF69865@in.WebX.maYIadrTaRb...
> > > > > if i have a listbox with the multiselect property set to extended
> how
> > do
> > > i
> > > > > create a button to remove all the selected items or add them to
> > another
> > > > > listbox?
> > > > >
> > > > > thanks,
> > > > > Rob
> > > > >
> > > > > w2k, vb6, a2k2
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>
0 Likes
Message 9 of 9

Anonymous
Not applicable
just do the same thing when you check for selections, but don't use the
for...next - loop :

Dim A As Integer
A = -1
Do
A = A + 1
If List1.Selected(A) Then
List2.AddItem List1.List(A)
List1.RemoveItem (A)
A = A - 1
End If
Loop Until A = List1.ListCount - 1




"Rob Tomson" wrote in message
news:FEDD39E5265E0516283F02BF7B4945D5@in.WebX.maYIadrTaRb...
> thanks, that works like a champ but now how do i remove the selected
items?
>
>
> "Veign" wrote in message
> news:A825845389D422F7EC8288E9B8630E81@in.WebX.maYIadrTaRb...
> > From the MSDN Library:
> >
> > MultiSelect Property Example
> > This example fills a ListBox control with the names of your screen fonts
> and
> > illustrates how the MultiSelect property affects the behavior of a
> ListBox.
> > To try this example, create two ListBox controls and a CommandButton
> control
> > on a form. In the first ListBox, set the MultiSelect property to 1 or 2.
> At
> > run time, select several items in the first ListBox, and then click the
> > CommandButton. All selected items are displayed in the second ListBox.
Run
> > the example several times with different settings of the MultiSelect
> > property. Paste the code into the Declarations section, and then press
F5
> to
> > run the program.
> >
> > Private Sub Form_Load ()
> > Dim I ' Declare variable.
> > ' Fill the list box with screen font names.
> > For I = 0 To Screen.FontCount - 1
> > List1.AddItem Screen.Fonts(I)
> > Next I
> > End Sub
> >
> > Private Sub Command1_Click ()
> > Dim I ' Declare variable.
> > ' Clear all items from the list.
> > List2.Clear
> > ' If an item is selected, add it to List2.
> > For I = 0 To List1.ListCount - 1
> > If List1.Selected(I) Then
> > List2.AddItem List1.List(I)
> > End If
> > Next I
> > End Sub
> >
> > HTH
> >
> > --
> > Veign
> > Designing Solutions
> > www.veign.com
> > VB Code Samples & Projects
> > www.veign.com/information/info_main.html
> >
> >
> > "Rob Tomson" wrote in message
> > news:2F84D10178904090E0B54836376BDA78@in.WebX.maYIadrTaRb...
> > > i know how to do that but if i have more than one item selected then
> > > .listindex only returns the currently selected item. how do i remove
> (or
> > > add) them all?
> > >
> > > "Russell Cygan" wrote in message
> > > news:14DA20A663C69A9FAC2E00E71164F04C@in.WebX.maYIadrTaRb...
> > > > you can use the removeitem method. here's an example that you may be
> > able
> > > to
> > > > use in your code.
> > > >
> > > > lstExistBlks.RemoveItem (lstExistBlks.ListIndex)
> > > >
> > > >
> > > > "Rob Tomson" wrote in message
> > > > news:EFF239DEF7733338448E318E7BF69865@in.WebX.maYIadrTaRb...
> > > > > if i have a listbox with the multiselect property set to extended
> how
> > do
> > > i
> > > > > create a button to remove all the selected items or add them to
> > another
> > > > > listbox?
> > > > >
> > > > > thanks,
> > > > > Rob
> > > > >
> > > > > w2k, vb6, a2k2
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>
0 Likes