moshecristel
asked on
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.
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.
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
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
ASKER
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.
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.
Hi,
I can't get your question. Could u please elaborate your question so that I can suggest u a solution.
bye
I can't get your question. Could u please elaborate your question so that I can suggest u a solution.
bye
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.
This code will make it selected in gray on my machine.
Me.ListView1.HideSelection
Set Me.ListView1.SelectedItem = Me.ListView1.ListItems(1)
Zaphod.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm sorry to confuse you, I was thinking ListBox.
'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
' 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
To select item i, use:
Set listview1.selecteditem = Listview1.ListItems(i)
*and*
Listview1.ListItems(i).Sel ected = True
(This is just a comment, do not accept it 'as answer')
Set listview1.selecteditem = Listview1.ListItems(i)
*and*
Listview1.ListItems(i).Sel
(This is just a comment, do not accept it 'as answer')
Listview1.ListItems(i).Sel ected = 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).Sel ected = True
ListView1.ListItems(i).Ens ureVisible
This will move the selected Item into the visible area of the list view.
HelixDaKat
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).Sel
ListView1.ListItems(i).Ens
This will move the selected Item into the visible area of the list view.
HelixDaKat
ASKER
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.
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.
HideSelection=False