|
-
Oct 9th, 2009, 08:10 AM
#1
Thread Starter
Member
[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?
-
Oct 9th, 2009, 08:21 AM
#2
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.
-
Oct 11th, 2009, 03:51 AM
#3
Thread Starter
Member
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
-
Oct 11th, 2009, 04:00 AM
#4
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?
-
Oct 11th, 2009, 04:08 AM
#5
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?
-
Oct 11th, 2009, 04:11 AM
#6
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.
-
Oct 11th, 2009, 06:59 AM
#7
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|