473,702 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ListViewItem.In dex = -1 ... why?

Hi everyone

I have a form which contains a ListView control. The code
handles the SelectedIndexCh anged event, and changes some
icons on some items in the ListView.

I've found that when I deactivate and reactivate the form
(an MDI child, in case that matters), the
SelectedIndexCh anged event is fired. On its own, that's not
a problem... but the following code causes trouble:
Dim lvw As ListView = Me.MyListView

' get the currently selected item
Dim itm1 As ListViewItem = lvw.SelectedIte ms(0)

If (itm1.Index < lvw.Items.Count - 1) Then
' get the following item in the list
Dim itm2 As ListViewItem = lvw.Items(itm1. Index + 1)

' and change its icon
itm2.ImageIndex = 0
End If
The line that sets the ImageIndex is causing an exception,
saying that "index" cannot be -1. But -1 is a valid value
for ImageIndex, and I passed a value of zero. So it can't
be talking about the value I specified for ImageIndex...

The call stack shows:

System.Windows. Forms.ListView. SetItemImage(in t index=-1,
int image=0)
System.Windows. Forms.ListViewI tem.set_Image(i nt value=0)

So "set_Image" is passing a value of -1 ... and sure
enough, if I put a breakpoint on the line of code that sets
"itm2.ImageInde x", I can see that "itm2.Index " is indeed -1.

Why?

i.e., I just looked it up by its index, so obviously the
ListView knows this item's index... so why does the item
not know its own index?

Can anyone suggest a workaround, other than simply ignoring
the exception? I'd rather not do that because then it might
be possible that the icons do not get set properly.

Thanks

-- graham

Nov 21 '05 #1
0 1555

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

Similar topics

0
1296
by: PHH | last post by:
Help me, please, When I redraw a ListviewItem in Listview (redraw items with icon and text columms). I used SetStyle(ControlStyle.UserPaint, true) function. And I redraw finish and run program, when I run program I choose first line (line with index = 0) but this line is not actived (draw border over that), after I click to another control (ex: other Listview, button ...) then this line is redrew border around. Only first line (first...
0
1905
by: WHITETIGER | last post by:
Help me, please, When I redraw a ListviewItem in Listview (redraw items with icon and text columms). I used SetStyle(ControlStyle.UserPaint, true) function. And I redraw finish and run program, when I run program I choose first line (line with index = 0) but this line is not actived (draw border over that), after I click to another control (ex: other Listview, button ...) then this line is redrew border around. Only first line (first...
6
13094
by: grs | last post by:
The following is a code example from the Microsoft MSDN. My question is on the following three lines of code: ListViewItem item1 = new ListViewItem("item1",0); ListViewItem item2 = new ListViewItem("item2",1); ListViewItem item3 = new ListViewItem("item3",0); You would in most cases know the ListViewItem name or the number of items to create (item1,item2 etc). How would you create this individual rows programmatically ?
1
4261
by: LagartijaNick | last post by:
Question: I have a ListView in which I'm selecting a ListViewItem based on the value of a textbox. On my XP machine (with default color scheme) the ..Selected row is a noticable grey (back color of ListView is white). However, on a custom Win2K desktop, the selected item is a faint grey and the users can barely see the highlighted row. The custom desktop settings have been locked down (ie. cannot be changed via Control Panel etc.)
2
3936
by: Kela | last post by:
An interesting problem: I have a ListView with LabelEdit set to TRUE. When I change the label, I want to make some decisions as to whether the ListViewItem (that's just been edited) should stay in the ListView or not. The natural place to inspect this is in the AfterLabelEdit event... If the decision is to take the item out, I used the ListViewItem.Remove(). However - check this out: A) say that the item's index is N (indices are...
6
5021
by: Padu | last post by:
I've been searching the newsgroup for an answer but probably with the wrong keywords... Imagine that I have a ListView with some items and the listview view is Details. How do I move an item up or down the list? Cheers Padu
0
1597
by: garyusenet | last post by:
I am trying to create a form that will display a list of open internet explorer windows and allow the user to choose one of them. So far I have created an arraylist which contains all of the open internet explorer windows. In the for() part of the code below I have tried to create a listviewitem for each window of internet explorer open (i.e. each element of the arraylist). However it's not working. I don't seem to have access to IE's...
13
10228
by: deciacco | last post by:
How can I have access to the items collection of a listview control on my form from a background thread? I know I need delegates to update the listview control and I have those calls in the foreach loop, but I'm not sure how to access the items collection. foreach (ListViewItem li in this.listview1.Items) { Invoke(new deleg(updatectrl), new object { param });
2
6169
by: witpo | last post by:
Hi, I would like to display all listview items in one row with scroll bar below it – instead of multiple rows and scroll bar on the right. Someone told me that I can achieve it using LVM_SETITEMPOSITION message. According to my knowledge code below should set position for each item added to listView control. There are two ways of doing it - we can use MessageWindow.SendMessage from Microsoft.WindowsCE.Forms or use DllImport and SendMessage....
1
8979
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8939
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
7829
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
6575
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
4412
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
4667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3104
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
2399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2036
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.