Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What is wrong in this code?

What is wrong in this code?

Scheduled Pinned Locked Moved C#
question
17 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Insincere Dave

    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 1

    After:
    A 1
    B 1
    C 1
    A 2
    B 2
    C 2

    foreach (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);
    }
    
    A Offline
    A Offline
    andredani
    wrote on last edited by
    #8

    it´s gonna work like this: before: a k111 b k111 a k112 after a click on a k111: a k111 b k111 a k112 a k113 (this is the copy)

    1 Reply Last reply
    0
    • G Guffa

      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;

      A Offline
      A Offline
      andredani
      wrote on last edited by
      #9

      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

      G 1 Reply Last reply
      0
      • A andredani

        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

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #10

        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;

        1 Reply Last reply
        0
        • A andredani

          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

          S Offline
          S Offline
          Skippums
          wrote on last edited by
          #11

          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

          A 1 Reply Last reply
          0
          • S Skippums

            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

            A Offline
            A Offline
            andredani
            wrote on last edited by
            #12

            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 :^)

            S 1 Reply Last reply
            0
            • A andredani

              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 :^)

              S Offline
              S Offline
              Skippums
              wrote on last edited by
              #13

              I never type in a compiler. Push F1 when the cursor is over Add, and see what type it should be

              A 1 Reply Last reply
              0
              • S Skippums

                I never type in a compiler. Push F1 when the cursor is over Add, and see what type it should be

                A Offline
                A Offline
                andredani
                wrote on last edited by
                #14

                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::((

                S 1 Reply Last reply
                0
                • A andredani

                  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::((

                  S Offline
                  S Offline
                  Skippums
                  wrote on last edited by
                  #15

                  Show your code.

                  A 1 Reply Last reply
                  0
                  • S Skippums

                    Show your code.

                    A Offline
                    A Offline
                    andredani
                    wrote on last edited by
                    #16

                    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); } } }

                    S 1 Reply Last reply
                    0
                    • A andredani

                      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); } } }

                      S Offline
                      S Offline
                      Skippums
                      wrote on last edited by
                      #17

                      you need to copy all subitems, not just the first.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups