Selecting a listview item in code...

I'm wondering how I would go about selecting a listview item in code so that it is highlighted just as if I had clicked on it.  I've looked in the help files and found the following example which doesn't seem to work for me.

Set ListView1.SelectedItem = ListView1.ListItems(1)

Even when I open a new project with nothing on the form but a command button and a listview it won't select the first item for me.

Am I trying to do the impossible or is there something I am overlooking.  Any help will be greatly appreciated.
moshecristelAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Z_BeeblebroxConnect With a Mentor Commented:
Ahah, this makes it blue:

Me.ListView1.HideSelection = False
Set Me.ListView1.SelectedItem = Me.ListView1.ListItems(1)
Me.ListView1.SetFocus

Zaphod.
0
 
chabaudCommented:
First of all, set the ListView property
HideSelection=False
0
 
3rsrichardCommented:
Here is an example similar to what you want;

    BaudLst.ListIndex = 1
    If (Rate) Then
        If (Rate = 600) Then BaudLst.ListIndex = 0
        If (Rate = 2400) Then BaudLst.ListIndex = 2
        If (Rate = 4800) Then BaudLst.ListIndex = 3
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
moshecristelAuthor Commented:
chabaud:

Sorry, still no luck.  I don't know if it helps but I am trying to select an item in the click event of a command button.

3rsrichard:

It doesn't seem that a listview control has a "ListIndex" property.  
0
 
mohan_sekarCommented:
Hi,

    I can't get your question. Could u please elaborate your question so that I can suggest u a solution.

bye
0
 
Z_BeeblebroxCommented:
Hi,

This code will make it selected in gray on my machine.

Me.ListView1.HideSelection = False
Set Me.ListView1.SelectedItem = Me.ListView1.ListItems(1)

Zaphod.
0
 
3rsrichardCommented:
I'm sorry to confuse you, I was thinking ListBox.
0
 
wileecoyCommented:
'copy-paste the following into notepad and save it as
'  form1.frm, then open in vb
VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   2484
   ClientLeft      =   48
   ClientTop       =   348
   ClientWidth     =   3744
   LinkTopic       =   "Form1"
   ScaleHeight     =   2484
   ScaleWidth      =   3744
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox Text1
      Height          =   252
      Left            =   1260
      TabIndex        =   2
      Text            =   "2"
      Top             =   240
      Width           =   612
   End
   Begin VB.ListBox List1
      Height          =   1584
      ItemData        =   "Form1.frx":0000
      Left            =   180
      List            =   "Form1.frx":0002
      TabIndex        =   1
      Top             =   660
      Width           =   3372
   End
   Begin VB.CommandButton Command1
      Caption         =   "Select Item"
      Height          =   372
      Left            =   180
      TabIndex        =   0
      Top             =   120
      Width           =   972
   End
   Begin VB.Label Label1
      Caption         =   "Enter Item To Select"
      Height          =   252
      Left            =   1980
      TabIndex        =   3
      Top             =   240
      Width           =   1452
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
    If IsNumeric(Text1.Text) And Text1.Text > 0 And Text1.Text < 26 Then
        List1.ListIndex = Text1.Text - 1    ' this is because the items start
                                            '  index counting at zero
    Else
        MsgBox "Please enter a number between 1 and 25", vbOKOnly, "Listbox Test"
    End If
End Sub

Private Sub Form_Load()
Dim i
For i = 1 To 25
    List1.AddItem "Item Number " & i
Next i

End Sub
0
 
amebaCommented:
To select item i, use:
    Set listview1.selecteditem = Listview1.ListItems(i)
*and*
    Listview1.ListItems(i).Selected = True


(This is just a comment, do not accept it 'as answer')
0
 
HelixDaKatCommented:
Listview1.ListItems(i).Selected = True

This line is Correct for selecting a list item.
Change (i) to the item you want selected.

If the item you want to select if not visible then your code should look like this:

Listview1.ListItems(i).Selected = True
ListView1.ListItems(i).EnsureVisible

This will move the selected Item into the visible area of the list view.

HelixDaKat
0
 
moshecristelAuthor Commented:
Z Beeblebrox:

It seems that my problem was that when I clicked on the command button, it brought the focus away from the listview (corrected by the "ListView1.SetFocus").

Thanks to all for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.