Changing the DataSource of a Listbox

L

Luca

Hello,

I'm using a windows form in which there is a standard ListBox control. I
want to add/remove objects from the ArrayList associated to the ListBox, and
I want the ListBox immediately shows graphically the change: e.g. if I
remove an element from the ArrayList object, then that element should not be
no more displayed in the list box.

Actually i'm using this code below:

private void UpdateListBox (KActivationList p_aL)
{
this.m_activation.DataSource = null;
this.m_activation.DataSource = p_aL;
this.m_activation.ValueMember = "VerboseName";
this.m_activation.DisplayMember = "VerboseName";
m_activation.Invalidate ();
}

The UpdateListBox method is called whenever an item has been removed/added
to a KActivationList object, which is a derived class of ArrayList.
m_activation is the ListBox. In this way it seems to work. But I'm getting
*sometimes* this exception when I select with the mouse an item in the list
box after having deleted any item:

Exception not managed: System.ArgumentOutOfRangeException: Index out of
bounds. the value must be non negative or less than the set length..
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32
newPosition, Boolean validating, Boolean endCurrentEdit, Boolean
firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position(Int32 value)
at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ListBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,
Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam,
IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd,
Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.ParkingWindow.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at TrainGroundStation.Form1.menuItem6_Click(Object sender, EventArgs e)
in c:\luca_tt\vcproj\traingroundstation\groundstationtrain.cs:line 366
at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
at System.Windows.Forms.MenuItemData.Execute()
at System.Windows.Forms.Command.Invoke()
at System.Windows.Forms.Command.DispatchID(Int32 id)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSGIl
programma "[1476] TrainGroundStation.exe" è terminato con il codice 0 (0x0).
& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at TrainGroundStation.Form1.Main() in
c:\luca_tt\vcproj\traingroundstation\groundstationtrain.cs:line 524


Any help?

Greetings,
Luca
 
C

ClayB [Syncfusion]

Try setting this.m_activation.SelectedIndex = 0 at the start of your
UpdateListBox. This will make sure the selected index is valid (unless the
list is empty) when the new list is being displayed for the first time.

======================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools

Luca said:
Hello,

I'm using a windows form in which there is a standard ListBox control. I
want to add/remove objects from the ArrayList associated to the ListBox, and
I want the ListBox immediately shows graphically the change: e.g. if I
remove an element from the ArrayList object, then that element should not be
no more displayed in the list box.

Actually i'm using this code below:

private void UpdateListBox (KActivationList p_aL)
{
this.m_activation.DataSource = null;
this.m_activation.DataSource = p_aL;
this.m_activation.ValueMember = "VerboseName";
this.m_activation.DisplayMember = "VerboseName";
m_activation.Invalidate ();
}

The UpdateListBox method is called whenever an item has been removed/added
to a KActivationList object, which is a derived class of ArrayList.
m_activation is the ListBox. In this way it seems to work. But I'm getting
*sometimes* this exception when I select with the mouse an item in the list
box after having deleted any item:

Exception not managed: System.ArgumentOutOfRangeException: Index out of
bounds. the value must be non negative or less than the set length..
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32
newPosition, Boolean validating, Boolean endCurrentEdit, Boolean
firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position(Int32 value)
at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ListBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,
Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam,
IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd,
Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.ParkingWindow.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at TrainGroundStation.Form1.menuItem6_Click(Object sender, EventArgs e)
in c:\luca_tt\vcproj\traingroundstation\groundstationtrain.cs:line 366
at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
at System.Windows.Forms.MenuItemData.Execute()
at System.Windows.Forms.Command.Invoke()
at System.Windows.Forms.Command.DispatchID(Int32 id)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSGIl
programma "[1476] TrainGroundStation.exe" è terminato con il codice 0 (0x0).
& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at TrainGroundStation.Form1.Main() in
c:\luca_tt\vcproj\traingroundstation\groundstationtrain.cs:line 524


Any help?

Greetings,
Luca
 
L

Luca

Hello Clay,

thanks for the tip. I havent tryed your tip yet, but adding (and removing)
directly the objects to the listbox's items collection called Items it works
nicely :) I ditched the use of the DataSource field.

Thanks again,
Luca


ClayB said:
Try setting this.m_activation.SelectedIndex = 0 at the start of your
UpdateListBox. This will make sure the selected index is valid (unless the
list is empty) when the new list is being displayed for the first time.

======================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools

Luca said:
Hello,

I'm using a windows form in which there is a standard ListBox control. I
want to add/remove objects from the ArrayList associated to the ListBox, and
I want the ListBox immediately shows graphically the change: e.g. if I
remove an element from the ArrayList object, then that element should
not
be
no more displayed in the list box.

Actually i'm using this code below:

private void UpdateListBox (KActivationList p_aL)
{
this.m_activation.DataSource = null;
this.m_activation.DataSource = p_aL;
this.m_activation.ValueMember = "VerboseName";
this.m_activation.DisplayMember = "VerboseName";
m_activation.Invalidate ();
}

The UpdateListBox method is called whenever an item has been removed/added
to a KActivationList object, which is a derived class of ArrayList.
m_activation is the ListBox. In this way it seems to work. But I'm getting
*sometimes* this exception when I select with the mouse an item in the list
box after having deleted any item:

Exception not managed: System.ArgumentOutOfRangeException: Index out of
bounds. the value must be non negative or less than the set length..
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32
newPosition, Boolean validating, Boolean endCurrentEdit, Boolean
firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position(Int32 value)
at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ListBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,
Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam,
IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd,
Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.ParkingWindow.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at TrainGroundStation.Form1.menuItem6_Click(Object sender, EventArgs e)
in c:\luca_tt\vcproj\traingroundstation\groundstationtrain.cs:line 366
at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
at System.Windows.Forms.MenuItemData.Execute()
at System.Windows.Forms.Command.Invoke()
at System.Windows.Forms.Command.DispatchID(Int32 id)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSGIl
programma "[1476] TrainGroundStation.exe" è terminato con il codice 0 (0x0).
& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at TrainGroundStation.Form1.Main() in
c:\luca_tt\vcproj\traingroundstation\groundstationtrain.cs:line 524


Any help?

Greetings,
Luca
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top