Results 1 to 6 of 6

Thread: Selecting an item in a listview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    105

    Selecting an item in a listview

    I have a listview displaying some items in detail mode and i would like the second item to be already selected. Anyone know how to do this?

  2. #2
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124

    Re: Selecting an item in a listview

    Originally posted by Wallabie
    I have a listview displaying some items in detail mode and i would like the second item to be already selected. Anyone know how to do this?
    Code:
    listbox1.selectedindex = 1

  3. #3
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391
    'something like this?
    Code:
    ListView1.ListItems(2).Selected = True
    And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    105
    Hmmm, the first one is for a listbox not a listview.

    The second one looks like the old VB6 way which i don't think works in .NET because i don't see a ListItems in the intel-sense popup box.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    105
    Dont you just hate it when this happens?

    I've just been mucking around and found it myself now...for those that would like to know, this partialy works, the row itself is grey-ed out and not highlighted though in the first instance on load but after that it is highlighted ok.

    Code:
                Me.listView1.Items(1).Selected = True
                Me.listView1.HideSelection = False

    Hope this helps others looking for this.

  6. #6
    New Member
    Join Date
    Feb 2005
    Posts
    1

    Re: Selecting an item in a listview

    Hi all

    Wallabie, you are right. That worked a treat, thank you.

    I still think that there's a problem with the listview control though. If you add the following line after your code you will find that the control does not contain any selected items.

    Code:
    Me.listView1.Items(0).Selected = True
    Me.listView1.HideSelection = False
    
    Console.Write(listView1.SelectedItems(0).Selected.ToString)
    This will fail with: "Specified argument was out of the range of valid values. Parameter name: index".

    If anyone can explain why, or what i am doing wrong, I would be very grateful. What i'm trying to do is call the click event for the listview after i set it default selection. This works if I click the item, but errors when loading.

    Code:
    listView1_Click(listView1, System.EventArgs.Empty)
    Regards Eamon.

    PS: I’m using VB.net web forms 2003.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width