Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to fire ListView_ItemClick event?

The ListView.SetFocus sets focus on the ListView item when the form loads automatically.
But the selected item is not displayed in the relevent RichTextBox untill the ListView_ItemClick is fired.
So how to fire the ListView_ItemClick event automatically with the form load?
[291 byte] By [naikosen] at [2007-11-11 10:26:35]
# 1 Re: How to fire ListView_ItemClick event?
In the Form_Load event, call lstView_ItemClick, passing the listview item you want to select.
Phil Weber at 2007-11-11 17:22:40 >
# 2 Re: How to fire ListView_ItemClick event?
How to pass the listview item.
I tried but it says Argument not optional.
Help me please.
naikosen at 2007-11-11 17:23:40 >
# 3 Re: How to fire ListView_ItemClick event?
Call lstView_ItemClick(lstView.ListItems(0))

Replace '0' with the index of the item you want to select.
Phil Weber at 2007-11-11 17:24:39 >
# 4 Re: How to fire ListView_ItemClick event?
It still flashes back the same message "Argument not optional" highlighting the words "Call ListView1_ItemClick".
I used:
Call ListView1_ItemClick(ListView1.ListItems(1))
naikosen at 2007-11-11 17:25:43 >
# 5 Re: How to fire ListView_ItemClick event?
If your listview is named ListView1, then the code should be:
Call ListView1_ItemClick(ListView1.ListItems(1))

Is the listview a member of a control array?
Phil Weber at 2007-11-11 17:26:42 >
# 6 Re: How to fire ListView_ItemClick event?
My listview name is ListView1, and I used "Call ListView1_ItemClick(ListView1.ListItems(1))", but produced the same error.
naikosen at 2007-11-11 17:27:41 >
# 7 Re: How to fire ListView_ItemClick event?
The following works great, but it is not fired automatically with the form load, whereas the first item in the listview is selected with the ListView1.SetFocus:
------

Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.IListItem, ByVal Button As Integer, ByVal SecondClick As Boolean)
Dim I As Integer

With rtfText
' Clear RichTextBox
.Text = ""

.SelBold = True
.SelFontName = "verdana"
.SelText = vbTab & Item.Text & vbCrLf
.SelText = Item.Text & vbCrLf & vbCrLf

' Repeat for subitems
For I = 2 To ListView1.ColumnHeaders.Count
.SelBold = True
.SelText = ListView1.ColumnHeaders(I).Text & vbCrLf
.SelText = vbTab & Item.SubItems(I - 1) & vbCrLf
Next
End With
End Sub
naikosen at 2007-11-11 17:28:47 >
# 8 Re: How to fire ListView_ItemClick event?
You're getting the error because your ItemClick procedure expects three parameters, but you're only passing one. What kind of listview control are you using? The standard listview ItemClick procedure looks like this:

Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)

Anyway, you can get rid of the error by passing all three parameters:

Call ListView1_ItemClick(ListView1.ListItems(1), 0, False)
Phil Weber at 2007-11-11 17:29:50 >
# 9 Re: How to fire ListView_ItemClick event?
Thank you very much 'Phil'.
It worked great!
So nice of you to help me.
naikosen at 2007-11-11 17:30:47 >