none
Programmatically select a Listview item RRS feed

  • Question

  • I can't programmatically select an item on a listview in c# 2005 beta2.  Basically, I:

    1. Create a Listview called Listview1
    2. Populate Listview1 with some preset items during startup of form
    3. Select first item by: 
          Listview1.Items[0].Selected = true;
    4. Problem is that I get zero when I execute: 
          Listview1.SelectedItems.Count

    I have tried setting focus to Listview1 but I still set zero returned.  Is this a bug - or am I doing something wrong?

    Thanks
    Monday, May 23, 2005 9:50 AM

Answers

  • Thanks for the suggestions.  The procedure is called during form startup and that seems to be the problem. Using debug, I can see that the item's selected value is correctly set to true but the selected.count is zero.  Once the form is up and running, the procedure works perfectly.  I presume this is a bug in beta2.

    I have got round the problem by looping through each item on the listview and check if its selected value is true.

    Friday, May 27, 2005 8:06 PM

All replies

  • Post the code to reproduce this.
    Monday, May 23, 2005 12:52 PM
    Moderator
  • Not sure if posting all the code will help because it is very long ... below are parts of it.  Program is a free song projection software which I have written in VB.NET.  I am in the process of porting it to c#:

    //Calling Procedure
    //WorshipList is the ListView which has just been populated with data from a text file

    if (WorshipList.Items.Count > 0)
    {
     WorshipList.Items[0].Selected = true;
     WorshipListIndexChanged(1);
    }

    private void WorshipListIndexChanged(int StartingSlide)
    {
     int Selecteditem;
     gf.PreviewItem.Source = gf.ItemSource.WorshipList;
     gf.TotalWorshipListItems = WorshipList.Items.Count;
     Selecteditem = gf.GetSelectedIndex(WorshipList);
     if (Selecteditem >= 0)
     {
      ...
     }

    public static int GetSelectedIndex(ListView InListView)
    {
     if (InListView.SelectedItems.Count == 0) 
     {
      ...
     }
    }

    //The above: InListView.SelectedItems.Count returns 0

    Monday, May 23, 2005 3:01 PM
  • This is just a wild guess, but do you run multiple threads in this program?  If you do, have you made sure all the UI related calls happen on the UI thread?

    It isn't a terribly good guess, I'd expect you have problems in VB.NET as well as C# if it was something like this.

    The following simple test case seems to do what one would expect so there must be some wrinkle in your program we aren't seeing?

    Create a simple form program and drop a a ListView and two Buttons on the form.  Use the designer to add one column to the ListView.

    The first button's click handler fills the list view



    private void button1_Click(object sender, System.EventArgs e)
    {
       ListViewItem item1 =
    new ListViewItem("item1");
       listView1.Items.Add (item1);

       ListViewItem item2 = new ListViewItem("item2");
       listView1.Items.Add (item2);

       ListViewItem item3 = new ListViewItem("item3");
       listView1.Items.Add (item3);
    }

     



    The second button programmatically selects the first item in the list



    private void button2_Click(object sender, System.EventArgs e)
    {
       listView1.Items[0].Selected =
    true;

       
    int i = GetSelectedIndex (listView1);

       Console.WriteLine ("Select: i={0}", i);
    }

    public static int GetSelectedIndex(ListView InListView)
    {
       
    if (InListView.SelectedItems.Count == 0) 
       {
          
    return 0;
       }

       // Too lazy to dial out the index
       
    return InListView.SelectedItems.Count;
    }

     


    Tuesday, May 24, 2005 3:26 AM
    Moderator
  • Hello,

    Try this...

    lvTheList.SelectedIndex = iIndexToSelect; //e.g 0 for the first item etc

    Hope this helps,
    Gary
    Friday, May 27, 2005 3:39 PM
  • Thanks for the suggestions.  The procedure is called during form startup and that seems to be the problem. Using debug, I can see that the item's selected value is correctly set to true but the selected.count is zero.  Once the form is up and running, the procedure works perfectly.  I presume this is a bug in beta2.

    I have got round the problem by looping through each item on the listview and check if its selected value is true.

    Friday, May 27, 2005 8:06 PM
  • hi,

    Please post the solution for this issue, right now I am having the same issue.

    Hi All,

    I Need to do Nunit testing, in that I need to select a listview item from Listview, I wrote following code

    ListView matchResultList = new ListView();

    ListViewItem lv = new ListViewItem();

                    lv.Text = "xyz";

                    ListViewItem lv1 = new ListViewItem();

                    lv1.Text = "xyz";

                    ListViewItem lv2 = new ListViewItem();

                    lv2.Text = "xyz";

                    this.matchResultList.Items.Clear();

                    this.matchResultList.Refresh();

                    matchResultList.Items.Add(lv);

                    matchResultList.Items.Add(lv1);

                    matchResultList.Items.Add(lv2);

                    //matchResultList.Items.Add("GigaHealth", 0);

                  

                    //matchResultList.Items.Add("Dental Health", 1);

                    //matchResultList.Items.Add("MegaHealth", 2);

                    //matchResultList.Items.Add("GigaHealth.AllProvider", 3);

                    this.matchResultList.FullRowSelect = true;

                    this.matchResultList.SelectedItems.Clear();

                    this.matchResultList.HideSelection = true;

                    this.matchResultList.MultiSelect = false;

                    this.matchResultList.GridLines = true;

                    this.matchResultList.Enabled = true;

                    this.matchResultList.CausesValidation = true;

                    this.matchResultList.AutoArrange = true;

                    this.matchResultList.IsAccessible = true;

                    this.matchResultList.Items[1].Focused = true;

                    this.matchResultList.Items[1].Selected = true;

    I tried all the possible ways but when is this.matchResultList.SelectedItems.Count() it shows ZERO. How should I make the ListViewItem to be selected. Please help me on this.

    Wednesday, September 20, 2017 2:38 PM
  • I have always hated when I get answers saying "this looks right" but...this looks right. 

    If you are still stuck on this, here are some possible sources of the problem.

    1)  Are doing this in the "load" function.  I have had issues with certain things done during the "load" function and when I move them to the "shown" function, they work. 

    2)  This might also be a case where you do not get a valid count because the listview has not been "refreshed".  I've also had weird problems with that also.  I think you can do a matchResultList.Refresh() and the count will be correct but it has been a long time so I might not be remembering how I got around this correctly.

    Hope that helps.

    Thursday, January 4, 2018 4:15 PM