I have a listview with process that are running. my goal is to pass the "path" of the process to a text box when someone clicks on a selection.

but my code isnt working correctly, this is what I have:

Private Sub lvwProcBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwProcBox.SelectedIndexChanged
        Dim pID As Integer = Int32.Parse(lvwProcBox.SelectedItems(0).SubItems(1).ToString)
        Dim proc As System.Diagnostics.Process = Process.GetProcessById(pID)

            procPath.Clear()
            Try
                Dim selectCount As Integer
                For selectCount = 0 To lvwProcBox.Items.Count()
                    If lvwProcBox.SelectedItems(selectCount).Selected = True Then
                        procPath.Text = (proc.MainModule.FileName.ToString())

                    End If

                Next
            Catch
            End Try
    End Sub

would be nice to fix i spent a week in google just trying ot find my solution.
and of course the text boxes path changes to a different path if a different selection is made

Edited 6 Years Ago by p@rse: n/a

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
		TextBox1.Text = IO.Path.GetDirectoryName(CType(ListBox1.SelectedItem, Process).MainModule.FileName)
	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		ListBox1.DisplayMember = "ProcessName"
		Dim p() As Process = Process.GetProcesses
		ListBox1.Items.AddRange(p)
	End Sub

After inserting the code I chnaged my keywords to match my value name. it asked me to chnage process(this has a blue squiggly line) to diagnostics.process so I did and then my lvwProcBox.selecteditem was not found so I changed it to: lvwProcBox.SelectedItems(Plural this time). Then I get the same squiggly blue line under the lvwProcBox and it says:

Value of type 'System.Windows.Forms.Listview.SelectedListViewItemCollection' cannot be converted to 'System.Diagnostics.Process'

uhm the given example is only working for ListBox controls.

For ListView control this does the trick:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim p() As Process = Process.GetProcesses
		For Each proc As Process In p
			Dim item As New ListViewItem With {.Text = proc.ProcessName, .Tag = proc}
			ListView1.Items.Add(item)
		Next
	End Sub

	Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
		If ListView1.SelectedItems.Count = 0 Then Return
		TextBox1.Text = IO.Path.GetDirectoryName(CType(ListView1.SelectedItems(0).Tag, Process).MainModule.FileName)
	End Sub
This article has been dead for over six months. Start a new discussion instead.