I did figure out a workaround that basically sets the datasource to the
clone of the original list as in the following:
Private alRegionNames As New ArrayList
Private Sub form4_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
loadDBRegionNames()
End Sub
Private Sub loadDBRegionNames()
alRegionNames = SalesRegionsDB.GetRegionNamesActiveAL
'the right hand box
lstRegionNames.DisplayMember = "reg_name"
lstRegionNames.ValueMember = "reg_idx"
lstRegionNames.DataSource = alRegionNames
End Sub
'sub called by add button click event
Private Sub Add()
Dim sindex As Integer = Me.lstRegionNames.SelectedIndex
If sindex < 0 Then Return
Dim sr As New SalesRegionDD
sr = Me.lstRegionNames.SelectedItem
Dim itemcount As Integer = Me.lstRegionNames.Items.Count
Me.lstSelectedRegionNames.Items.Add(sr) 'this is another listbox
that is not bound
Me.lstRegionNames.SelectedIndex = -1
Me.alRegionNames.RemoveAt(sindex)
' I put this test in to see if there is only 1 item left in the
source listbox, if so,
' set the datasource to nothing or else there will be a very
noticeable delay for the ' last item to pass over to the left box
If itemcount = 1 Then
Me.lstRegionNames.DataSource = Nothing
Else
Me.lstRegionNames.DisplayMember = "reg_name"
Me.lstRegionNames.ValueMember = "reg_idx"
Me.lstRegionNames.DataSource = Me.alRegionNames.Clone
' if you use the clone, then you don't have to set the datasource to
nothing first
End If
End Sub
However, my solution is not very elegant (and probably slow) and the ListBox
bugs will probably be remedied in DotNet 2.0.
I'll probably just do your solution and keep track of both the listbox and
the arraylist contents without a DataSource, adding and removing items via
comparison as you suggest.
"alejandro lapeyre" <alejandrolapeyre@jotmail.com> wrote in message
news:euVhUTz%23EHA.2700@TK2MSFTNGP14.phx.gbl...[color=blue]
> Do not attach it to the DataSource property.
>
> Simply fill the ListBox with the ArrayList contents.
>
> Alejandro Lapeyre
>
> "MCDave" <daven(delete_this)@miraclecatDELETETHISTOO.com> escribió en el
> mensaje news:%23T1BpHt%23EHA.1400@TK2MSFTNGP11.phx.gbl...[color=green]
> > When I say a bound listbox cannot be updated, I mean that I get the
> > following error:
> >
> > "An unhandled exception of type 'System.ArgumentException' occurred in
> > system.windows.forms.dll
> > Additional information: Cannot modify the Items collection when the
> > DataSource property is set."
> >
> > It appears there is a bug that prevents updating of a listbox from an
> > arraylist (when the arraylist is bound to the listbox).
> >
> >
> > "MCDave" <daven(delete_this)@miraclecatDELETETHISTOO.com> wrote in[/color][/color]
message[color=blue][color=green]
> > news:%23kaGHyf%23EHA.1604@TK2MSFTNGP12.phx.gbl...[color=darkred]
> >> The first listbox cannot be updated directly as it is bound by the
> >> arraylist.
> >> So I remove an item from the arraylist that is tied to the first[/color][/color][/color]
listbox,[color=blue][color=green][color=darkred]
> >> but because the listbox is bound, I can't simply remove an item[/color][/color][/color]
directly[color=blue][color=green][color=darkred]
> >> from the listbox (if I try to remove anything directly from the[/color][/color][/color]
listbox,[color=blue][color=green][color=darkred]
> >> I
> >> get an error that basically says it is already bound - catch 22).
> >>
> >> In other words:
> >> say the bound listbox is called "lstRegionNames" and it is bound to the
> >> arraylist "alRegionNames"
> >>
> >>
> >> Private alRegionNames As New ArrayList
> >> Private alSelectedRegionNames As New ArrayList
> >>
> >> Private Sub form4_Load(ByVal sender As System.Object, ByVal e As
> >> System.EventArgs) Handles MyBase.Load
> >> loadDBRegionNames()
> >>
> >> End Sub
> >>
> >>
> >> Private Sub loadDBRegionNames()
> >>
> >> alRegionNames = SalesRegionsDB.GetRegionNamesActiveAL
> >>
> >> 'the first listbox bound to the arraylist
> >> lstRegionNames.DisplayMember = "reg_name"
> >> lstRegionNames.ValueMember = "reg_idx"
> >> lstRegionNames.DataSource = alRegionNames
> >>
> >> ' the second listbox -
> >> lstSelectedRegionNames.DisplayMember = "reg_name"
> >> lstSelectedRegionNames.ValueMember = "reg_idx"
> >>
> >> End Sub
> >>
> >>
> >> Private Sub Add() 'called when clicking the Add button
> >>
> >> If lstRegionNames.SelectedIndex < 0 Then Return 'if nothing[/color][/color][/color]
selected,[color=blue][color=green][color=darkred]
> >> exit sub
> >> Dim sr As New SalesRegionDD 'the object which contains "reg_idx" and
> >> "reg_name"
> >> sr = lstRegionNames.SelectedItem
> >> Me.lstSelectedRegionNames.Items.Add(sr) 'add the object to the second
> >> listbox - this works fine
> >>
> >> Me.alRegionNames.RemoveAt(lstRegionNames.SelectedI ndex) 'remove that[/color]
> > object[color=darkred]
> >> from the arraylist - this works fine- I verified this by looping[/color][/color][/color]
through[color=blue][color=green]
> > the[color=darkred]
> >> objects in "alRegionNames" - the object was indeed removed from the
> >> arraylist
> >>
> >> 'an attempt at refreshing the first listbox by re-binding it to the
> >> arraylist - this does not refresh "lstRegionNames"
> >> Me.lstRegionNames.DataSource = Me.alRegionNames
> >>
> >> End Sub
> >>
> >>
> >> I had done this with a datatable and removing an item from the[/color][/color][/color]
datatable[color=blue][color=green][color=darkred]
> >> removes it from the bound listbox automatically.
> >> Doing the exact same thing with an arraylist does not work, though.
> >>
> >> I don't know how to proceed or what else needs to be done to be able to
> >> remove the item from "lstRegionNames"
> >>
> >>
> >>
> >> "alejandro lapeyre" <alejandrolapeyre@jotmail.com> wrote in message
> >> news:eACxXnb%23EHA.2180@TK2MSFTNGP10.phx.gbl...
> >> > What is so difficult in writing this control?
> >> >
> >> > insert one lisbox
> >> > insert the buttons
> >> > insert other listbox
> >> >
> >> > fill the first listbox
> >> >
> >> > create an empty arraylist
> >> >
> >> > when the user clicks the move here button
> >> > get the item using the listbox.selectedindex
> >> > remove from the collection, and the listbox.items
> >> > add it to the other collection and to the other listbox
> >> >
> >> > I am missing something?
> >> >
> >> > Alejandro Lapeyre
> >> > "MCDave" <daven(delete_this)@miraclecatDELETETHISTOO.com> escribió en
> >> > el
> >> > mensaje news:Oaw$Yjb%23EHA.1260@TK2MSFTNGP12.phx.gbl...
> >> > > This image should clarify.
> >> > >
> >> > >
www.miraclecat.com/listbox2listbox.gif
> >> > >
> >> > > MCDave
> >> > >
> >> > >
> >> >
> >> >
> >>
> >>[/color]
> >
> >[/color]
>
>[/color]