Re: Confused about the Selected property of a ListView

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Zoury (yanick_lefebvre_at_hotmail.com)
Date: 06/01/04


Date: Tue, 1 Jun 2004 13:17:21 -0400

Hi Dale!

The last item to be added to the list has its Selected property
automatically set to True AND the SelectedItem property of the list is set
to it. The SelectedItem property of the list returns the item that has the
*focus* in the list. This doesn't garanty that the item is selected though.
So if you want to prevent this you can unselect the last item when you had
it and set the SelectedItem property to Nothing when your done filling the
list.

here's different tests :
'***
' Selects the first item and gives it focus
Option Explicit

Private Sub Form_Load()

    Dim i As Long

    With ListView1

        .View = lvwReport
        Call .ColumnHeaders.Add

        For i = 1 To 10
            Call .ListItems.Add(, , "item " & i)
        Next i

    End With

End Sub
'***
'***
' Selects the first item and without giving it the focus
<snip>
        For i = 1 To 10
            Call .ListItems.Add(, , "item " & i)
        Next i

        Set .SelectedItem = Nothing
<snip>
'***
'***
' Do not selects any item but gives focus to the first one..
<snip>
        For i = 1 To 10
            .ListItems.Add(, , "item " & i).Selected = False
        Next i
<snip>
End Sub
'***
'***
' Do not selects any items and doesn't give focus to any of them..
<snip>
        For i = 1 To 10
            .ListItems.Add(, , "item " & i).Selected = False
        Next i

        Set .SelectedItem = Nothing
<snip>
End Sub
'***

-- 
Best Regards
Yanick Lefebvre
"Dale" <dale0610@hotmail.com> a écrit dans le message de
news:OJThnc0REHA.904@TK2MSFTNGP12.phx.gbl...
> Hi, dumb question, I have a listview on my form which I manually
> populate.  When it is loaded, I see the first row is selected which is
> not what I was expecting - I was expected nothing to be selected ... do
> I have to manually set the Selected flag to False for all the items if I
> want nothing selected upon loading?  Thanks.


Relevant Pages

  • dropdownlist.selectedItem not working from in a datalist
    ... I have a dropdownlist in my DataList control and I am trying to set the ... SelectedItem property of the dropdownlist so when the datalist displays ... dim strTest as string = ddlCategory.SelectedValue.ToString ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Listview - make sure no item is selected
    ... > How can you make sure that there are no items selected in a ListView? ... ListView's SelectedItem property to Nothing. ... property of ListItem objects to False. ...
    (microsoft.public.vb.general.discussion)
  • getting the value of a selected/unselected item in a checkboxlist
    ... I am having a bit of a problem getting the current value of the checkbox ... I have also tried to use the selectedItem property but that does the same as ... Brett ...
    (microsoft.public.dotnet.languages.csharp)
  • DropDownList SelectedIndex
    ... I want to display the DropDownList box with no SelectedItem. ... I tried setting the SelectedItem property to -1 but it does not work. ... Tap ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Im Getting Postal
    ... Private Sub lblAdd_Click ... Dim rs As Recordset, db As Database, sql As String ... Dim SelectedItem As Variant ... > is selected in "lstAdmin" as a new record to the current database. ...
    (microsoft.public.access.formscoding)