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. Retrieving multiple items from listview

Retrieving multiple items from listview

Scheduled Pinned Locked Moved C#
20 Posts 2 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.
  • M mrithula8

    Hi I used the following code.Whichever item i select from the listview appears on the 1st label.The item that is selected 2nd does not appear in the 2nd label.Im not clear what sl[1] will contain?Can you tell me the mistake?

    Label[] lb = new Label[4];
    lb[0] = lb4;
    lb[1] = lb5;
    lb[2] = lb6;
    lb[3] = lb7;
    for (int h = 0; h < sl.Count; h++)
    {
    lb[h].Text = sl[h].Text;
    }

    K Offline
    K Offline
    King Julien
    wrote on last edited by
    #8

    ofcourse.... the code is working according to what it was written. I assume that you are calling this code after you select the item from the listview. In that case, have a close look at the for loop. every time the for loop counter initializes to 0

    mrithula8 wrote:

    for (int h = 0; h < sl.Count; h++)

    it means that whatever you select, that will be assigned in order from the first label. Thats wat is happening.... If you want to display the listview item 1 in label 1, then you try getting the item number of the list view and use the same number as the index for the label control array and i think your problem could be solved.

    for (int h = 0; h < sl.Count; h++)
    {
    lb[sl[i].Index].Text = sl[i].Text;
    }

    Make sure that you have n number of labels if there are n number of items... otherwise it may throw you an exception....

    Have a Happy Coding.....

    M 1 Reply Last reply
    0
    • K King Julien

      ofcourse.... the code is working according to what it was written. I assume that you are calling this code after you select the item from the listview. In that case, have a close look at the for loop. every time the for loop counter initializes to 0

      mrithula8 wrote:

      for (int h = 0; h < sl.Count; h++)

      it means that whatever you select, that will be assigned in order from the first label. Thats wat is happening.... If you want to display the listview item 1 in label 1, then you try getting the item number of the list view and use the same number as the index for the label control array and i think your problem could be solved.

      for (int h = 0; h < sl.Count; h++)
      {
      lb[sl[i].Index].Text = sl[i].Text;
      }

      Make sure that you have n number of labels if there are n number of items... otherwise it may throw you an exception....

      Have a Happy Coding.....

      M Offline
      M Offline
      mrithula8
      wrote on last edited by
      #9

      Hi Thanks for reply.I do not want to display the 1st item in the 1st label.Suppose for the 1st time i select the 10th item that should appear in the 1st label.The 2nd time if i select the 5th item it should be displayed in the 2nd the label......Actually i uesd the code in the selected indexchanged event <pre> private void listView1_SelectedIndexChanged(object sender, EventArgs e) { ListView.SelectedListViewItemCollection sl = this.listView1.SelectedItems; Label[] lb = new Label[4]; lb[0] = lb4; lb[1] = lb5; lb[2] = lb6; lb[3] = lb7; for (int h = 0; h <= sl.Count; h++) { lb[h].Text = sl[h].Text; } }

      modified on Wednesday, April 8, 2009 2:18 AM

      K 1 Reply Last reply
      0
      • M mrithula8

        Hi Thanks for reply.I do not want to display the 1st item in the 1st label.Suppose for the 1st time i select the 10th item that should appear in the 1st label.The 2nd time if i select the 5th item it should be displayed in the 2nd the label......Actually i uesd the code in the selected indexchanged event <pre> private void listView1_SelectedIndexChanged(object sender, EventArgs e) { ListView.SelectedListViewItemCollection sl = this.listView1.SelectedItems; Label[] lb = new Label[4]; lb[0] = lb4; lb[1] = lb5; lb[2] = lb6; lb[3] = lb7; for (int h = 0; h <= sl.Count; h++) { lb[h].Text = sl[h].Text; } }

        modified on Wednesday, April 8, 2009 2:18 AM

        K Offline
        K Offline
        King Julien
        wrote on last edited by
        #10

        Oh!!! then use a wild way... declare a static int and use that as label index

        static int counter = 0;

        for (int h=0; h<sl.count;>{
        if (counter < lb.Length)
        {
        lb[counter].Text = sl[h].Text;
        counter++;
        }
        }

        Have a Happy Coding.....

        M 1 Reply Last reply
        0
        • K King Julien

          Oh!!! then use a wild way... declare a static int and use that as label index

          static int counter = 0;

          for (int h=0; h<sl.count;>{
          if (counter < lb.Length)
          {
          lb[counter].Text = sl[h].Text;
          counter++;
          }
          }

          Have a Happy Coding.....

          M Offline
          M Offline
          mrithula8
          wrote on last edited by
          #11

          Hi When i used this, it says invalid static modifier for this item.So i used <pre>static int counter=0</pre> outside the method but im not able to select any item from listview.

          K 1 Reply Last reply
          0
          • M mrithula8

            Hi When i used this, it says invalid static modifier for this item.So i used <pre>static int counter=0</pre> outside the method but im not able to select any item from listview.

            K Offline
            K Offline
            King Julien
            wrote on last edited by
            #12

            Not sure how you have implemented it. i suggest declare the counter(static int counter = 0) not inside any methods. Just declare inside it inside the class. That would do... But how does this matter when selecting an item from the List View???? can you place the entire code block or atleast how you call this method after selecting the items.....???

            Have a Happy Coding.....

            M 1 Reply Last reply
            0
            • K King Julien

              Not sure how you have implemented it. i suggest declare the counter(static int counter = 0) not inside any methods. Just declare inside it inside the class. That would do... But how does this matter when selecting an item from the List View???? can you place the entire code block or atleast how you call this method after selecting the items.....???

              Have a Happy Coding.....

              M Offline
              M Offline
              mrithula8
              wrote on last edited by
              #13

              Hi, I used this code in the selectedIndexchanged().Is this right?I declared the conuter variable inside the class.When i execute the code the form does not respond. <pre>private void listView1_SelectedIndexChanged(object sender, EventArgs e) { ListView.SelectedListViewItemCollection sl = this.listView1.SelectedItems; Label[] lb = new Label[4]; lb[0] = lb4; lb[1] = lb5; lb[2] = lb6; lb[3] = lb7; for (int h = 0; h < sl.Count;){ if(counter<lb.Length) { lb[counter].Text=sl[h].Text; counter++; } } }

              modified on Wednesday, April 8, 2009 5:18 AM

              K 1 Reply Last reply
              0
              • M mrithula8

                Hi, I used this code in the selectedIndexchanged().Is this right?I declared the conuter variable inside the class.When i execute the code the form does not respond. <pre>private void listView1_SelectedIndexChanged(object sender, EventArgs e) { ListView.SelectedListViewItemCollection sl = this.listView1.SelectedItems; Label[] lb = new Label[4]; lb[0] = lb4; lb[1] = lb5; lb[2] = lb6; lb[3] = lb7; for (int h = 0; h < sl.Count;){ if(counter<lb.Length) { lb[counter].Text=sl[h].Text; counter++; } } }

                modified on Wednesday, April 8, 2009 5:18 AM

                K Offline
                K Offline
                King Julien
                wrote on last edited by
                #14

                Declare a label control array in the Form class like this

                public Label[] lblArray = null;
                static int counter = 0;

                Inside the form load method add your labels like this.....

                this.lblArray = new Label[] { label4, label5, label6 };

                Then the event should look like this...

                private void ListView1_SelectedIndexChanged_1(object sender, EventArgs e)
                {
                ListView.SelectedListViewItemCollection sl = ListView1.SelectedItems;

                        for (int h = 0; h < sl.Count; h++)
                        {
                            if (counter < lblArray.Length)
                            {
                                lblArray\[counter\].Text = sl\[h\].Text;
                                counter++;
                            }
                        }
                    }
                

                I have even checked this myself... Its working for me... without any problem...

                Have a Happy Coding.....

                M 1 Reply Last reply
                0
                • K King Julien

                  Declare a label control array in the Form class like this

                  public Label[] lblArray = null;
                  static int counter = 0;

                  Inside the form load method add your labels like this.....

                  this.lblArray = new Label[] { label4, label5, label6 };

                  Then the event should look like this...

                  private void ListView1_SelectedIndexChanged_1(object sender, EventArgs e)
                  {
                  ListView.SelectedListViewItemCollection sl = ListView1.SelectedItems;

                          for (int h = 0; h < sl.Count; h++)
                          {
                              if (counter < lblArray.Length)
                              {
                                  lblArray\[counter\].Text = sl\[h\].Text;
                                  counter++;
                              }
                          }
                      }
                  

                  I have even checked this myself... Its working for me... without any problem...

                  Have a Happy Coding.....

                  M Offline
                  M Offline
                  mrithula8
                  wrote on last edited by
                  #15

                  Hi Thanks its working now

                  K 1 Reply Last reply
                  0
                  • M mrithula8

                    Hi Thanks its working now

                    K Offline
                    K Offline
                    King Julien
                    wrote on last edited by
                    #16

                    unmaiyavae work aagutha... illa summa solringla...???? what did you do for decrementing the counter....?????

                    Have a Happy Coding.....

                    M 1 Reply Last reply
                    0
                    • K King Julien

                      unmaiyavae work aagutha... illa summa solringla...???? what did you do for decrementing the counter....?????

                      Have a Happy Coding.....

                      M Offline
                      M Offline
                      mrithula8
                      wrote on last edited by
                      #17

                      Hi Its working the way i wanted.I dont understand why the counter should be decremented?

                      K 1 Reply Last reply
                      0
                      • M mrithula8

                        Hi Its working the way i wanted.I dont understand why the counter should be decremented?

                        K Offline
                        K Offline
                        King Julien
                        wrote on last edited by
                        #18

                        I dont know exactly about your application.... So no comments,.... Anyway!! Cheers...

                        Have a Happy Coding.....

                        M 1 Reply Last reply
                        0
                        • K King Julien

                          I dont know exactly about your application.... So no comments,.... Anyway!! Cheers...

                          Have a Happy Coding.....

                          M Offline
                          M Offline
                          mrithula8
                          wrote on last edited by
                          #19

                          hi I have another doubt.If i want to find the selected index of a listbox or a listview control i can use listbox.SelectedIndex; Similarly if i have 5 checkboxes and if i want to find the index of the checkbox that is checked how do i do this?There is no Selected Index property for checkbox. I have the exe files listed and each one is assigned a checkbox.I want to start only those exe files that i select.I am able to do this.After starting the exe files i want the client to send message to only those exe files that have been started. I used the following code to send message to different ports <pre>public void SendMessage() { try { label1.Text = "Enter the number of times to send the message:"; String ns = textBox1.Text; int c = Convert.ToInt32(ns); int[] a = new int[4]; XmlTextReader xtr1 = new XmlTextReader("p1.xml"); XmlTextReader xtr2 = new XmlTextReader("p2.xml"); XmlTextReader xtr3 = new XmlTextReader("p3.xml"); XmlTextReader xtr4 = new XmlTextReader("p4.xml"); int p1 = 0, p2 = 0, p3 = 0, p4 = 0; while (xtr1.Read()) { switch (xtr1.NodeType) { case XmlNodeType.Text: String rv = xtr1.Value; p1 = Convert.ToInt32(rv); break; } } while (xtr2.Read()) { switch (xtr2.NodeType) { case XmlNodeType.Text: String rv1 = xtr2.Value; p2 = Convert.ToInt32(rv1); break; } } while (xtr3.Read()) { switch (xtr3.NodeType) { case XmlNodeType.Text: String rv2 = xtr3.Value; p3 = Convert.ToInt32(rv2); break; } } while (xtr4.Read()) { switch (xtr4.NodeType) { case XmlNodeType.Text: String rv3 = xtr4.Value;

                          K 1 Reply Last reply
                          0
                          • M mrithula8

                            hi I have another doubt.If i want to find the selected index of a listbox or a listview control i can use listbox.SelectedIndex; Similarly if i have 5 checkboxes and if i want to find the index of the checkbox that is checked how do i do this?There is no Selected Index property for checkbox. I have the exe files listed and each one is assigned a checkbox.I want to start only those exe files that i select.I am able to do this.After starting the exe files i want the client to send message to only those exe files that have been started. I used the following code to send message to different ports <pre>public void SendMessage() { try { label1.Text = "Enter the number of times to send the message:"; String ns = textBox1.Text; int c = Convert.ToInt32(ns); int[] a = new int[4]; XmlTextReader xtr1 = new XmlTextReader("p1.xml"); XmlTextReader xtr2 = new XmlTextReader("p2.xml"); XmlTextReader xtr3 = new XmlTextReader("p3.xml"); XmlTextReader xtr4 = new XmlTextReader("p4.xml"); int p1 = 0, p2 = 0, p3 = 0, p4 = 0; while (xtr1.Read()) { switch (xtr1.NodeType) { case XmlNodeType.Text: String rv = xtr1.Value; p1 = Convert.ToInt32(rv); break; } } while (xtr2.Read()) { switch (xtr2.NodeType) { case XmlNodeType.Text: String rv1 = xtr2.Value; p2 = Convert.ToInt32(rv1); break; } } while (xtr3.Read()) { switch (xtr3.NodeType) { case XmlNodeType.Text: String rv2 = xtr3.Value; p3 = Convert.ToInt32(rv2); break; } } while (xtr4.Read()) { switch (xtr4.NodeType) { case XmlNodeType.Text: String rv3 = xtr4.Value;

                            K Offline
                            K Offline
                            King Julien
                            wrote on last edited by
                            #20

                            you can use the same slvi to get that. slvi[index].checked will give you the corresponding checkbox of an item is checked or not.....

                            Have a Happy Coding.....

                            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