473,698 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArgumentOutOfRa ngeException in ListView.Items. Add

Hi,

I got the System.Argument OutOfRangeExcep tion exception when adding an
item in ListView.
This ListView (listViewOutput ) is used to display messages to user
(normal messages and error messages):

private void AddOutputMessag e (string message)
{
listViewOutput. BeginUpdate ();
if (listViewOutput .Items.Count > MaxOutputLines)
listViewOutput. Items.RemoveAt (0);
ListViewItem lvi =
new ListViewItem (DateTime.Now.T oString ("HH:mm:ss.ff") );
lvi.SubItems.Ad d (message);
listViewOutput. Items.Add (lvi);
lvi.EnsureVisib le ();
listViewOutput. EndUpdate ();
}

private void AddOutputError (string message)
{
listViewOutput. BeginUpdate ();
if (listViewOutput .Items.Count > MaxOutputLines)
listViewOutput. Items.RemoveAt (0);
ListViewItem lvi =
new ListViewItem (DateTime.Now.T oString ("HH:mm:ss.ff") );
ListViewItem.Li stViewSubItem lviMessage =
lvi.SubItems.Ad d (message);
lvi.UseItemStyl eForSubItems = false;
lviMessage.Back Color = Color.Red;
// the exception is throwed by following line
listViewOutput. Items.Add (lvi);
lvi.EnsureVisib le ();
listViewOutput. EndUpdate ();
}

Do I need some locking as these 2 functions uses the same ListView ?
Besides, the AddOutputError can be called from other functions located
in the main Form (and so, in the GUI thread) or in other threads.
In the latter case, to ensure that the GUI thread is used, a message is
inserted in a queue.
This queue is read in a dedicated thread as following :

...
switch (guiInfo.EventT ype)
{
case GuiEventType.Me ssage:
case GuiEventType.Er ror:
if (listViewOutput .InvokeRequired )
BeginInvoke
(new HandleGuiEventD elegate
(UpdateMiscelan eous),
new object[] { guiInfo });
else
UpdateMiscelane ous (guiInfo);
break;
...
}
...

public void UpdateMiscelane ous (GuiEventArgs guiInfo)
{
switch (guiInfo.EventT ype)
{
case GuiEventType.Me ssage:
GuiDataMessage guiData =
(GuiDataMessage ) guiInfo.Data;
AddOutputMessag e (guiData.Messag e);
break;

case GuiEventType.Er ror:
guiData = (GuiDataMessage ) guiInfo.Data;
AddOutputError (guiData.Messag e);
break;

default:
if (_logger.IsErro rEnabled)
_logger.Error
("UpdateMiscela neous: unknown event
type " +
guiInfo.EventTy pe.ToString ());
break;
}
}

Thanks a lot in advance for your help.

May 2 '06 #1
0 3138

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

Similar topics

6
4646
by: VM | last post by:
How can I fill up a listview with text file contents? My listview has two columns and the first column fills up with a while loop: while (myString != null) { myString = sr.Readline(); listView1.Items.Add (myString); } Is there a way I can fill up the second column this easily? Thw way I'm currently doing it is using the subitems property to add it. Unfortunately,
7
6443
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override ListView.Items.Add(), . I see that it is a virtual method so it should be easy to do. If anyone can help I would appreciate it greatly. I can do what I need to do in a different way this would just make everything significantly cleaner and eaasier...
6
3408
by: Nick | last post by:
Hi there, I'm trying to implement drag-drop for my listview control in large icon view mode. Unfortunately the order of the items gets completely messed up upon inserting the item back into the listview, removing the item obviously keeps everything fine but the second I use insert the item is added to the *end* of the listview. Does anyone have any tips on how I can solve this issue?? Call Items.Remove(SourceItem)
12
7483
by: J L | last post by:
When I fill a listview, I resize the columns to fit the data. I need to know if the data will fit vertically or if there will be a vertical scroll bar. I need to know this so I can allow for it on the overall size of the listview. My question therefore is, how can I tell if the items I have added will fit in the listview at its given height? A secondary one, just for interest sake...is there a way to determine the exact heght needed...
1
2057
by: samoore33 | last post by:
I found this code on MSDN, and it works great. It creates a ListView dynamically and add items to it and all. It is great. I have changed a few of the column names to suit me. Dim listView1 As New ListView listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200)) ' Set the view to show details.
1
5087
by: dvestal | last post by:
I have a ListView with checkboxes. I want to remove items when the checkboxes are unchecked, but to do so yields an ArgumentOutOfRangeException. Is there an easy way to get around this? A simple program that illustrates the exception is below: using System; using System.Windows.Forms; namespace WindowsApplication1
0
1888
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the form every thing works fine: checking items by code as well as checking with mouse: Using the CheckdItems Property is confused too.
2
6827
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the form every thing works fine: checking items by code
1
8356
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, In my application I need to periodically remove all current items from a ListView and add a new set into it. The following abbreviated code contains the basic idea: private void FillListViewFromFile(string path) {
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.