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. Programaticaly select a listview item

Programaticaly select a listview item

Scheduled Pinned Locked Moved C#
tutorialquestion
10 Posts 5 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 Offline
    M Offline
    Manster
    wrote on last edited by
    #1

    Can anyone tell me how to programmatically select a listview item in a windows form listview? I want to force an item to be selected based upon its text. Thanks, :)

    I 1 Reply Last reply
    0
    • M Manster

      Can anyone tell me how to programmatically select a listview item in a windows form listview? I want to force an item to be selected based upon its text. Thanks, :)

      I Offline
      I Offline
      Ista
      wrote on last edited by
      #2

      lv.SelectedItem = lv.Items[0]; I'm not an expert yet, but I play one at work. Yeah and here too.

      M B 2 Replies Last reply
      0
      • I Ista

        lv.SelectedItem = lv.Items[0]; I'm not an expert yet, but I play one at work. Yeah and here too.

        M Offline
        M Offline
        Manster
        wrote on last edited by
        #3

        I've tried this and it doesn't seem to work, I get a compile error when I tried this. :((

        1 Reply Last reply
        0
        • I Ista

          lv.SelectedItem = lv.Items[0]; I'm not an expert yet, but I play one at work. Yeah and here too.

          B Offline
          B Offline
          Bo Hunter
          wrote on last edited by
          #4

          The ListView does not have a SelectedItem property. It has a SelectedItems property but is readonly. The proper way is to set the item's Selected Property to true; There are also a bunch of methods to help Like EnsureVisible( index ), TopItem and so on. Bo Hunter

          Bo Hunter

          M C I 3 Replies Last reply
          0
          • B Bo Hunter

            The ListView does not have a SelectedItem property. It has a SelectedItems property but is readonly. The proper way is to set the item's Selected Property to true; There are also a bunch of methods to help Like EnsureVisible( index ), TopItem and so on. Bo Hunter

            Bo Hunter

            M Offline
            M Offline
            Manster
            wrote on last edited by
            #5

            So after I set a listviewitems's selected property to true, how can I programaticaly select that item in the listview? Is this even possible? I can't find anything on the net about this.

            1 Reply Last reply
            0
            • B Bo Hunter

              The ListView does not have a SelectedItem property. It has a SelectedItems property but is readonly. The proper way is to set the item's Selected Property to true; There are also a bunch of methods to help Like EnsureVisible( index ), TopItem and so on. Bo Hunter

              Bo Hunter

              C Offline
              C Offline
              Csharp
              wrote on last edited by
              #6

              C#:


              private void button1_Click(object sender, System.EventArgs e)
              {
              listView1.Items[1].Selected=true;
              MessageBox.Show(listView1.SelectedItems[0].Text);
              }


              Csharp™  the coder formally known as dynamic


              Me.twins.duedate = DateTime.Now.AddDays(+17).ToLongDateString
              Me.Birthday = DirectCast(Me.twins.DueDate, DateAndTime.SameDay)


              M 1 Reply Last reply
              0
              • C Csharp

                C#:


                private void button1_Click(object sender, System.EventArgs e)
                {
                listView1.Items[1].Selected=true;
                MessageBox.Show(listView1.SelectedItems[0].Text);
                }


                Csharp™  the coder formally known as dynamic


                Me.twins.duedate = DateTime.Now.AddDays(+17).ToLongDateString
                Me.Birthday = DirectCast(Me.twins.DueDate, DateAndTime.SameDay)


                M Offline
                M Offline
                Manster
                wrote on last edited by
                #7

                I added listView1.Items[1].Selected=true; to my function and it still doesn't highlight the selected item for me. Is their something I'm not doing correctly?

                B F 2 Replies Last reply
                0
                • M Manster

                  I added listView1.Items[1].Selected=true; to my function and it still doesn't highlight the selected item for me. Is their something I'm not doing correctly?

                  B Offline
                  B Offline
                  Bo Hunter
                  wrote on last edited by
                  #8

                  Set the HideSelection to false; Bo Hunter

                  Bo Hunter

                  1 Reply Last reply
                  0
                  • B Bo Hunter

                    The ListView does not have a SelectedItem property. It has a SelectedItems property but is readonly. The proper way is to set the item's Selected Property to true; There are also a bunch of methods to help Like EnsureVisible( index ), TopItem and so on. Bo Hunter

                    Bo Hunter

                    I Offline
                    I Offline
                    Ista
                    wrote on last edited by
                    #9

                    lvOfficeNote.Items[0].Selected = true is how you would do it I'm not an expert yet, but I play one at work. Yeah and here too.

                    1 Reply Last reply
                    0
                    • M Manster

                      I added listView1.Items[1].Selected=true; to my function and it still doesn't highlight the selected item for me. Is their something I'm not doing correctly?

                      F Offline
                      F Offline
                      Furty
                      wrote on last edited by
                      #10

                      Make sure you call the Focus() method of the listview after selecting the listviewitem.

                      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