Click here to Skip to main content
15,942,426 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello !
I am creating a task manager but i have a problem :
i want to create a button that will open the directory of a item selected in my listview
SO if anyone can help me ,please answer. Thanks!

Imports System.IO

Public Class Form1

    Dim File As New Process()

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim P As New Process()
        Dim Count As Integer = 0
        ListView1.Items.Clear()
        For Each P In Process.GetProcesses(My.Computer.Name)
            On Error Resume Next
            ListView1.Items.Add(P.ProcessName)
            ListView1.Items(Count).SubItems.Add(FormatNumber(Math.Round(P.PrivateMemorySize64 / 1024), 0))
            ListView1.Items(Count).SubItems.Add(P.Responding)
            ListView1.Items(Count).SubItems.Add(P.StartTime.ToString.Trim)
            ListView1.Items(Count).SubItems.Add(P.Id)
            Count = Count + 1
        Next
    End Sub
    Private Sub KillProcess()
        On Error GoTo Handled
        For Each P As ListViewItem In ListView1.SelectedItems
            System.Diagnostics.Process.GetProcessById(P.SubItems(4).Text).Kill()
            P.SubItems.Clear()
        Next
Handled:
        MsgBox("Echech de fermer le processus")
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        KillProcess()
    End Sub
    Private Sub GetProcess()
        Dim P As New Process()
        Dim Count As Integer = 0
        ListView1.Items.Clear()
        For Each P In Process.GetProcesses(My.Computer.Name)
            On Error Resume Next
            ListView1.Items.Add(P.ProcessName)
            ListView1.Items(Count).SubItems.Add(FormatNumber(Math.Round(P.PrivateMemorySize64 / 1024), 0))
            ListView1.Items(Count).SubItems.Add(P.Responding)
            ListView1.Items(Count).SubItems.Add(P.StartTime.ToString.Trim)
            ListView1.Items(Count).SubItems.Add(P.Id)
            Count = Count + 1
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        GetProcess()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each P As ListViewItem In ListView1.SelectedItems
            System.Diagnostics.Process.GetProcessById(P.SubItems(4).Text)
            Directory.GetCurrentDirectory()
        Next

    End Sub

End 
Sorry ................
Button3 don't work
Posted
Updated 19-Jun-13 8:53am
v2
Comments
koleraba 19-Jun-13 12:41pm    
you will have to provide more information. What excatly is your listview populated with? Do you use MVVM pattern? Maybe post some sample code that you have so far.

Uros
Haykerman 19-Jun-13 14:23pm    
Ok , all process running will be displayed in my listeview and i want to add a button that will open the folder where is the process.
this is my introduction but i dont no how to finish :
Dim File As String
File = Me.ListView1.SelectedItems.ToString
i don't know if it's good ....
Richard C Bishop 19-Jun-13 14:17pm    
Indeed you do have a problem, as you have not described a problem or even provided the code you have attempted.

1 solution

You have a similar code on MSDN samples for the TreeView control. Take a look at: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selecteditems.aspx[^]

You could create a class (ProcessInfo or something) which would contain info about the retrieved processes and store the instance of that class in the Tag property of each generated TreeViewItem. When the user selects an item, you just retrieve the tag of the selected item(s), cast them to ProcessInfo and retrive the directory from there.

Uros
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900