?
Solved

Selecting a listview item in code...

Posted on 2001-06-27
11
Medium Priority
?
247 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

Technology Partners: 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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