What is wrong in this code?
-
I don't understand what nodes you want duplicated, you might need to post an example of the input and output. There is not much point in doing a foreach over a single item like you have done in the inner loop. Here is an example that duplicates each node in the listview and increments the subitem. This only adjusts SubItems[1] other subitems are not changed. It also does no checking if SubItems[1].Text is an number or not. e.g
Before:
A 1
B 1
C 1After:
A 1
B 1
C 1
A 2
B 2
C 2foreach (ListViewItem item in listView1.Items) { //Convert text to integer int value = int.Parse(item.SubItems[1].Text); //Clone item, so you dont have to recreate the subitems ListViewItem newItem = (ListViewItem)item.Clone(); newItem.SubItems[1].Text = (value + 1).ToString(); listView1.Items.Add(newItem); }
-
What are you trying to do really? The text of a SubItem is a string, and a string doesn't contain any integers, it only contains characters. If you use foreach on a string, it will loop through the characters in the string, and casting each character to int means that you get the unicode character codes for the characters in the string. You won't find any characters in the string with the character code 0. What does the SubItems contain? If it is the string representation of a number, you have to parse the string if you want to get the number. Use the int.Parse method.
--- single minded; short sighted; long gone;
-
Subitem[1] is gonna contain one letter and than all numbers, like this: Before: A k111 B k111 A k112 After a click on A k111: A k111 B k111 A k112 A k113
andredani wrote:
Subitem[1] is gonna contain one letter and than all numbers, like this: Before: A k111 B k111 A k112 After a click on A k111: A k111 B k111 A k112 A k113
That doesn't make sense. "A k111" is not one letter then all numbers. That's a letter, a space, another letter, and then some digits. If you click "A k111", what is the process to get to "A k113"? That's not adding one, so the items have to be related somehow?
--- single minded; short sighted; long gone;
-
I want to copy one listview.item and change name on subitem[1] by +1 if that exists add+1 and futher Dictionary list = new Dictionary(); if (listView2.SelectedItems.Count > 0) { foreach (ListViewItem lvi in listView2.Items) { int number = Convert.ToInt32(lvi.SubItems[1]); bool alreadyExists = true; while (alreadyExists) { alreadyExists = false; foreach (int existingNumber in listView2.Items) { if (existingNumber == number) { alreadyExists = true; break; } } if (alreadyExists) { number = number + 1; } } } } it says: Unable to cast object of type 'ListViewSubItem' to type 'System.IConvertible'? what should i do?:doh:sigh tnx mates
IList m_SubItems = new List(); if (m_ListView.SelectedItems.Count > 0) { // Why do you need SelectedItems.Count > 0? ... // You don't ever reference them again within the if statement for (int i = m_ListView.Items.Count - 1; i >= 0; --i) { ListViewItem current = m_ListView.Items[i]; string item = current.SubItems[1].Text; if (m_SubItems.Contains(item)) { // This code assumes that all integers have only one digit. // If you want something different, you must change how index is created. int index = (int)item[item.Length - 1]; item = item.Substring(0, item.Length - 1); do { ListViewItem newItem = new ListViewItem(); ... newItem.SubItems.Add(new ListViewSubitem(item + (++index).ToString())); m_ListView.Items.Add(newItem); } while (m_SubItems.Contains(item + index.ToString())); } else { m_SubItems.Add(item); } } } Hope this helps! Jeff
-
IList m_SubItems = new List(); if (m_ListView.SelectedItems.Count > 0) { // Why do you need SelectedItems.Count > 0? ... // You don't ever reference them again within the if statement for (int i = m_ListView.Items.Count - 1; i >= 0; --i) { ListViewItem current = m_ListView.Items[i]; string item = current.SubItems[1].Text; if (m_SubItems.Contains(item)) { // This code assumes that all integers have only one digit. // If you want something different, you must change how index is created. int index = (int)item[item.Length - 1]; item = item.Substring(0, item.Length - 1); do { ListViewItem newItem = new ListViewItem(); ... newItem.SubItems.Add(new ListViewSubitem(item + (++index).ToString())); m_ListView.Items.Add(newItem); } while (m_SubItems.Contains(item + index.ToString())); } else { m_SubItems.Add(item); } } } Hope this helps! Jeff
-
thanks man! but this says: newItem.SubItems.Add(new ListViewSubitem(item + (++index).ToString())); Error 1 The type or namespace name 'ListViewSubItem' could not be found tnx :^)
-
I never type in a compiler. Push F1 when the cursor is over Add, and see what type it should be
-
it does´t work for me, i´m i doing wrong?? i get all the listview items subitem[1] copyied and the rest empty... strange... :confused::confused::((
-
IList m_SubItems = new List(); for (int i = listView1.Items.Count - 1; i >= 0; --i) { ListViewItem current = listView1.Items[i]; string item = current.SubItems[1].Text; if (m_SubItems.Contains(item)) { int index = (int)item[item.Length - 1]; item = item.Substring(0, item.Length - 1); do { ListViewItem newItem = new ListViewItem(); newItem.SubItems.Add(item + (++index).ToString()); listView1.Items.Add(newItem); } while (m_SubItems.Contains(item + index.ToString())); } else { m_SubItems.Add(item); } } }
-
IList m_SubItems = new List(); for (int i = listView1.Items.Count - 1; i >= 0; --i) { ListViewItem current = listView1.Items[i]; string item = current.SubItems[1].Text; if (m_SubItems.Contains(item)) { int index = (int)item[item.Length - 1]; item = item.Substring(0, item.Length - 1); do { ListViewItem newItem = new ListViewItem(); newItem.SubItems.Add(item + (++index).ToString()); listView1.Items.Add(newItem); } while (m_SubItems.Contains(item + index.ToString())); } else { m_SubItems.Add(item); } } }