473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing the DataSource of a Listbox

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.EndCurrentEdi t()
at System.Windows.Forms.CurrencyManager.ChangeRecordS tate(Int32
newPosition, Boolean validating, Boolean endCurrentEdit, Boolean
firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position( Int32 value)
at System.Windows.Forms.ListBox.OnSelectedIndexChange d(EventArgs e)
at System.Windows.Forms.ListBox.WmReflectCommand(Mess age& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessa ge(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.ReflectMessageInterna l(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(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.ParkingWindow.WndProc(Message & m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& 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(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(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\groundstation train.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(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSGIl
programma "[1476] TrainGroundStation.exe" è terminato con il codice 0 (0x0).
& msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(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\groundstation train.cs:line 524
Any help?

Greetings,
Luca
Nov 16 '05 #1
2 3411
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" <lu**@rewind.com> wrote in message
news:ua**************@tk2msftngp13.phx.gbl...
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.EndCurrentEdi t()
at System.Windows.Forms.CurrencyManager.ChangeRecordS tate(Int32
newPosition, Boolean validating, Boolean endCurrentEdit, Boolean
firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position( Int32 value)
at System.Windows.Forms.ListBox.OnSelectedIndexChange d(EventArgs e)
at System.Windows.Forms.ListBox.WmReflectCommand(Mess age& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessa ge(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.ReflectMessageInterna l(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(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.ParkingWindow.WndProc(Message & m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& 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(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMetho ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(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\groundstation train.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(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSGIl
programma "[1476] TrainGroundStation.exe" è terminato con il codice 0 (0x0). & msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMetho ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(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\groundstation train.cs:line 524
Any help?

Greetings,
Luca

Nov 16 '05 #2
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 [Syncfusion]" <cl***@syncfusion.com> ha scritto nel messaggio
news:%2****************@tk2msftngp13.phx.gbl...
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" <lu**@rewind.com> wrote in message
news:ua**************@tk2msftngp13.phx.gbl...
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.EndCurrentEdi t()
at System.Windows.Forms.CurrencyManager.ChangeRecordS tate(Int32
newPosition, Boolean validating, Boolean endCurrentEdit, Boolean
firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position( Int32 value)
at System.Windows.Forms.ListBox.OnSelectedIndexChange d(EventArgs e)
at System.Windows.Forms.ListBox.WmReflectCommand(Mess age& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessa ge(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.ReflectMessageInterna l(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(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.ParkingWindow.WndProc(Message & m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& 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(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg) at

System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32

reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(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\groundstation train.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(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSGIl
programma "[1476] TrainGroundStation.exe" è terminato con il codice 0

(0x0).
& msg)
at

System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32

reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(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\groundstation train.cs:line 524
Any help?

Greetings,
Luca


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4590
by: Jesse Martinez | last post by:
My problem occurs when I use an ArrayList as a ListBox.DataSource. When the ArrayList attached to the Listbox is not empty the Listbox behave normal without problem, but if I remove all items...
0
2127
by: JMe9ka | last post by:
I have a ListBox to which I am assigning a DataTable as a DataSource, as shown below. ======================================================= DataTable VendorData = new DataTable(); // Code...
3
10998
by: Alvaro E. Gonzalez V. | last post by:
Hello!!! I'm building a control that in a property assign a Dataset which they are initialized and another property Like DataMember. Similar as it happens to the DataSource property of a...
2
1959
by: Alpha | last post by:
I have a window application. On one of the form, there is a listbox and a few combox. The lstSchItem has a dataview as a datasource. The comboxes are bind to its selected value but through the...
4
6951
by: Stephen | last post by:
IN VB.NET I have a form with a ListBox and would like to set the DataSource property at design-time but I can't seem to declare any kind of variable that will show up in the list. I have tried a...
11
8532
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox datasource to nothing. I set the listbox datasource to...
0
1675
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
2
1790
by: tfsimes | last post by:
Hi, I am a long time ASP developer learning .NET, so please bear with me. I am trying to find an article or such that will help me understand how to change control properties at runtime based on...
1
8296
by: Refugnic | last post by:
I tried to fill a ListBox with a DataSource pointing to an ArrayList. It all works fine...up to one point. The ArrayList is dynamic, which means the contents of it change, during the course of...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.