Hi all, it's my first time in this forum, I hope you can help.
I've searched the web for a long time, couldn't find help, yet.

I have a listview that displays a list of PDF files. When the user clicks on a row (in the "selectedIndexChanged" event), I display the selected PDF underneath the Listview in a Webbrowser control

WebBrowser1.Navigate(sFile)

So far so good.
Additionally, when the user double clicks a row on the ListView (in the "DoubleClick" event), I use

Process.Start(sFile)

where sFile is the name of the PDF file, to open Adobe Acrobat Reader in a separate process.
Now here's the problem: This only works when the user double clicks the currently selected row. If however, the user double clicks on a row that is not selected in the list view, the application produces two "SelectedIndexChanged" events, and no double click event.
What I've discovered is that if I comment out the line

WebBrowser1.Navigate(sFile)

then my code works as expected. A single click produces a "selectedIndexChanged", a double click produces a "selectedIndexChanged" and a "DoubleClick".
So it seems as if calling WebBrowser1.Navigate(sFile) somehow interrupts the events from firing correctly.
Any ideas on how I could solve this issue?
Thank you.
Winkler

Dani AI

Generated

A few points that clarify what happened and give safe alternatives.

As discovered, relying on the ListView activation model is the clean solution here: set the control so items are activated by two clicks and handle the ItemActivate event rather than trying to mix SelectedIndexChanged + DoubleClick. The ListView Activation property and the ItemActivate event are intended exactly for that behavior. ListView.Activation, ListView.ItemActivate. (learn.microsoft.com)

Why the original pattern fails: a GUI double-click is a specific sequence (first click selects, then a second click within the system double-click time on the same target produces the double-click message). If your selection-change handler immediately starts navigation or opens another window/process, focus/capture can change and the second click will not be delivered to the ListView — so no DoubleClick event is raised. See the Win32 double-click message behavior and the requirement that the same target receive both clicks. WM_LBUTTONDBLCLK, UI Events dblclick rules. (learn.microsoft.com)

Practical alternatives that avoid timing races: 1) keep the ItemActivate + TwoClick approach (robust). 2) Handle the mouse double-click directly and determine which item was under the cursor with HitTest (so you do not rely on SelectedIndexChanged). ListView.HitTest. 3) If you must update the embedded viewer from SelectedIndexChanged, defer that update slightly (queue it to the UI message loop with BeginInvoke or a very short timer) so the double-click sequence can complete before navigation changes focus — but avoid blocking the UI thread. WinForms multithreading / BeginInvoke guidance. (learn.microsoft.com)

Notes: and were right that the DoubleClick/MouseDoubleClick events are the usual hooks, but those handlers become unreliable if a selection-change handler immediately moves focus or starts navigation. The ItemActivate + TwoClick pattern is the simplest robust fix; deferring the viewer update is the next-best option when activation semantics must differ.

Recommended Answers

All 6 Replies

Welcome winkler,

Event selectedIndexChanged will be fired when index of an item get changed. But with DoubleClick - There is no way to execute selectedIndexChanged.

Please verify your code.

hi frnd,if you want on Listview double click then,use the below event

Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
          MsgBox ("")
    End Sub

Use DoubleClick event instead of MouseDoubleClick.

Thank you for your ideas. I tried them and they don't work. Any other ideas? Anyone know about simulating a double click event by timing the interval between the clicks?
Thank you.

wrinkler,

It's not an idea. It's a suggestion and it must has to work.

Well, thank you for your suggestion, but it did not change anything in my situation.
I did, however, find a different workable solution.
ListView has an "activated" attribute, which, when set to "Two Clicks" activates the selected row on the listview in two clicks. This differs from "DoubleClick" because "Two Clicks" allows for some time to elapse between the two.
I put the Process.Start(sFile) into the ListView1_ItemActivate() event and voila! It works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.