Solved

Selecting a listview item in code...

Posted on 2001-06-27
11
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 4

Expert Comment

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

Expert Comment

by:3rsrichard
ID: 6231621
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
ID: 6231673
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6231706
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
ID: 6231735
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
 
LVL 7

Accepted Solution

by:
Z_Beeblebrox earned 50 total points
ID: 6231752
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
ID: 6231759
I'm sorry to confuse you, I was thinking ListBox.
0
 
LVL 4

Expert Comment

by:wileecoy
ID: 6231846
'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
ID: 6231927
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
ID: 6232074
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
ID: 6234866
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

726 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