simulating double click from within VB.NET

  • Thread starter Thread starter Co
  • Start date Start date
C

Co

Hi All,

I'm writing a program that uses a form with files on my hdd.
It's sort of a CMS. Now I was wondering if it is possible in vb.net to
simulate a double click like you can do in the windows explorer to
open a file. I'm using doc, xls, pdf, txt, rtf, ppt, html.

Otherwise I have to create a code for all different extensions to open
a file.

Marco
 
Hi All,

I'm writing a program that uses a form with files on my hdd.
It's sort of a CMS. Now I was wondering if it is possible in vb.net to
simulate a double click like you can do in the windows explorer to
open a file. I'm using doc, xls, pdf, txt, rtf, ppt, html.

Otherwise I have to create a code for all different extensions to open
a file.

Marco

Hi All,

I found following simple line to open a document from the hdd:
System.Diagnostics.Process.Start(sDocumentName)

Now I have a Listview with filenames. When I click a file the name
gets stored to sDocumentName.

Protected Sub DocumentDetailHandler(ByVal sender As System.Object,
ByVal e As System.EventArgs)

Dim I As Integer
Dim ID As Integer
For I = 0 To ListView.SelectedItems.Count - 1
ID = ListView.SelectedItems(I).Tag
Next
sDocumentName = ""
Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & ID
Dim cmd As New OleDbCommand(sql, conn)
Dim dr As OleDbDataReader
conn.Open()
dr = cmd.ExecuteReader()

Do While (dr.Read())
sDocumentName = dr.Item("hddlokatie") & dr.Item
("filenaam")
WebBrowser1.Url = New Uri("C:\" &
System.IO.Path.GetFileNameWithoutExtension(dr.Item("filenaam")) &
".html")
Loop
dr.Close()
conn.Close()

End Sub
But this happens only when I left-click the document.
When I right click a menu appears with the Open option:

Private Sub CMenuClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.ToolStripItemClickedEventArgs)

'Point to menu item clicked
Select Case e.ClickedItem.Text
Case "Open" : System.Diagnostics.Process.Start
(sDocumentName)
Case "Edit" : MsgBox("Edit")
Case "Delete" : MsgBox("Delete")
End Select

End Sub

So when I directly right-click and choose open the documentname is
empty.
Is there a better way to make sure I always have the right name to
open a document?

Regards
M<rco
 
Hello Marco,

to get the item when the user right clicks an item, you need to use the
MouseDown event.

For this little test program, create a form with one ListView (named
ListView1) and copy this code into it:

Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
ListView1.GetItemAt(e.X, e.Y).Selected = True
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles _
ListView1.SelectedIndexChanged
If ListView1.SelectedItems.Count > 0 Then
Me.Text = ListView1.SelectedItems.Item(0).Text
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles Me.Load
ListView1.Items.Add("ABC")
ListView1.Items.Add("DEF")
ListView1.Items.Add("GHI")
End Sub
So when I directly right-click and choose open the documentname is
empty.
Is there a better way to make sure I always have the right name to
open a document?

This code will create 3 items in a list view. If you right-click one,
the text of the first item which is selected is shown in the form's title.

Best regards

Martin
 
Hello Marco,

to get the item when the user right clicks an item, you need to use the
MouseDown event.

For this little test program, create a form with one ListView (named
ListView1) and copy this code into it:

Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
  ListView1.GetItemAt(e.X, e.Y).Selected = True
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles _
  ListView1.SelectedIndexChanged
        If ListView1.SelectedItems.Count > 0 Then
        Me.Text = ListView1.SelectedItems.Item(0).Text
        End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles Me.Load
ListView1.Items.Add("ABC")
ListView1.Items.Add("DEF")
ListView1.Items.Add("GHI")
End Sub


This code will create 3 items in a list view. If you right-click one,
the text of the first item which is selected is shown in the form's title..

Best regards

Martin

Martin,
when I rus this I get an error:
Object reference not set to an instance of an object.

on this code:
Private Sub ListView_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView.MouseDown
ListView.GetItemAt(e.X, e.Y).Selected = True
End Sub

Marco
 
Hello Marco,
Martin,
when I rus this I get an error:
Object reference not set to an instance of an object.

on this code:
Private Sub ListView_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView.MouseDown
ListView.GetItemAt(e.X, e.Y).Selected = True
End Sub

Your code always says ListView rather than ListView1. Make sure that all
objects are properly named.

Best regards,

Martin
 
Hello Marco,



Your code always says ListView rather than ListView1. Make sure that all
objects are properly named.

Best regards,

Martin

That's because it is named Listview.
Thats not the problem.

Marco
 
Hello Marco,

Then I could imagine that e.X or e.Y is <0 or > than the maximum
width/height of your listview.

Best regards,

Martin
 
Martin H. said:
Hello Marco,

Then I could imagine that e.X or e.Y is <0 or > than the maximum
width/height of your listview.

Best regards,

Martin

Marco,

There are eight set of areas in a listview. First is the header (which
is only shown in Details view), the second is the area with items and the
third the area without items. The others are of less value.

You can use the HitTest function of the listview and then check the
Location property of the ListViewHitTestInfo returned from the function.

You can also change your code to:

If ListView.GetItemAt(e.x,e.Y) isnot nothing then
ListView.GetItemAt(e.x,e.Y).selected = true
end if

What is happening is that the GetItemAt is returning nothing if you do not
click an item.

Another property of value is the FullRowSelect which allows the user to
select an item by clicking anywhere on the list other than the label (or
first column).

LS
 
Back
Top