Results 1 to 7 of 7

Thread: [RESOLVED] Select First Item + ListView

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    60

    Resolved [RESOLVED] Select First Item + ListView

    Code:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim lCount As Integer
            For lCount = 1 To 5
                ListView1.Items.Add(lCount.ToString)
            Next
    
            If ListView1.Items.Count > 0 Then
                ListView1.Items(0).Selected = True
            End If
    I have even write the code to select the first node,i want first row should be blue(Selected). But its not coming to be blue?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Select First Item + ListView

    Your code works fine for me. Of course, the selected item will only be blue if the ListView has focus. The item will still be selected if the ListView doesn't have focus but the highlight will be far more subtle.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    60

    Re: Select First Item + ListView

    Its not wroking even if listview has focus
    Code:
     Dim lCount As Integer
            For lCount = 1 To 5
                ListView1.Items.Add(lCount.ToString)
            Next
    
            If ListView1.Items.Count > 0 Then
                ListView1.Focus()
                ListView1.Items(0).Selected = True
                ListView1.Items(0).EnsureVisible()
            End If

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Select First Item + ListView

    So presumably you can see all items but none appears selected? What happens if you click on an item manually at runtime - does it appear selected then?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Select First Item + ListView

    But does the ListView have focus? If that code is in the Load event handler then calling the ListView's Focus method is NOT going to mean that it has focus when the form is displayed IF the ListView is not the first control in the Tab order. You have to set focus AFTER the form is displayed or else make the ListView first in the Tab order. Try running this code:
    Code:
    Private Sub Form1_Load(ByVal sender As Object, _
                           ByVal e As EventArgs) Handles MyBase.Load
        Dim lCount As Integer
        For lCount = 1 To 5
            ListView1.Items.Add(lCount.ToString)
        Next
    
        If ListView1.Items.Count > 0 Then
            ListView1.Items(0).Selected = True
            ListView1.Items(0).EnsureVisible()
        End If
    End Sub
    
    Private Sub Form1_Shown(ByVal sender As Object, _
                            ByVal e As EventArgs) Handles Me.Shown
        Me.ListView1.Select()
    End Sub
    Does that work?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Select First Item + ListView

    By the way, you're talking about rows so I assume that you have set the View property to Details. Have you actually added any columns to the ListView? If not then you won't see the items so you won't see an item selected.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    60

    Re: Select First Item + ListView

    hi jmcilhinney,I set the TabIndex of Listview to 0 and it works out.

    Code:
      Dim lCount As Integer
            Dim lvitem As ListViewItem
    
            For lCount = 1 To 5
                lvitem = ListView1.Items.Add(lCount.ToString)
                lvitem.SubItems.Add("A")
            Next
    
            If ListView1.Items.Count > 0 Then
                ListView1.Items(0).Selected = True
                ListView1.Items(0).EnsureVisible()
            End If

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