Solved

Selecting a listview item in code...

Posted on 2001-06-27
11
240 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:moshecristel
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 4

Expert Comment

by:chabaud
Comment Utility
First of all, set the ListView property
HideSelection=False
0
 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
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
 

Author Comment

by:moshecristel
Comment Utility
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
 
LVL 15

Expert Comment

by:mohan_sekar
Comment Utility
Hi,

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

bye
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Accepted Solution

by:
Z_Beeblebrox earned 50 total points
Comment Utility
Ahah, this makes it blue:

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

Zaphod.
0
 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
I'm sorry to confuse you, I was thinking ListBox.
0
 
LVL 4

Expert Comment

by:wileecoy
Comment Utility
'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
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
 
LVL 1

Expert Comment

by:HelixDaKat
Comment Utility
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
 

Author Comment

by:moshecristel
Comment Utility
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now