I need to kill a specific process, i got ListBox1, and Button1 and Button2

Button1 Gets Process which is notepad, When Button1 is clicked in the ListBox1 it shows the Main titled of the windows, Example Notepad is running and his title is Untitled - Notepad in Listbox1 will show the title, And the Button2 Should kill the process of the selected item in the ListBox1, Like i Got 2 Notepad open, 1 with titled 1 and other with title 2, when i select in ListBox1 the one that says 1 and i click button2 it should kill the notepad process that have the titled 1 no all the notepads.

Button1 Code

Dim iGet() As Process = Process.GetProcessesByName("notepad")
        Me.ListBox1.Items.Clear()
        For Each Proc As Process In iGet
            Me.ListBox1.Items.Add(Proc.MainWindowTitle)
        Next

I need code for Button2

I got this for Button2

Dim proc() As Process = Process.GetProcesses
        For i As Integer = 0 To proc.GetUpperBound(0)
            If proc(i).ProcessName = "notepad" Then
                proc(i).Kill()
            End If
        Next

But it closes all notepad process, not even selecting the iem from ListBox1 and then Button i need it, please help...

there must be kill method for that

@GeekByCoiCe it worked perfectly but, is there any way to prevent it to rename it in listbox ? cuz it says [Untitled - Notepad, 8056] i would like it to say Untitled - Notepad only... But im working on it, i hope you respond me back :)

@GeekByCoiCe it worked perfectly but, is there any way to prevent it to rename it in listbox ? cuz it says [Untitled - Notepad, 8056] i would like it to say Untitled - Notepad only... But im working on it, i hope you respond me back :)

ListBox1.Items.Add(New KeyValuePair(Of String, Integer)(proc.MainWindowTitle, proc.Id))

=>

ListBox1.Items.Add(proc.MainWindowTitle)

select your listbox in your project,and change its properties DisplayMember to "Key" and ValueMember to "Value" (ofc without the quotes).

@GeekNyCoiCe Damnn you are a beast xDD works perfectly ^^

With this code that you gave me is possible to Hide and Unhide the windows ? selecting in the ListBox1 and clicking Button3?

Yes it is possible...
But keep in mind, once you hide a window and try to refresh the listbox the process will be added but no windowtitle will be shown.

Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Integer
	'possible nCmdShow values:
	'vbMinimizedFocus
	'vbMinimizedNoFocus
	'vbMaximizedFocus
	'vbNormalFocus
	'vbNormalNoFocus
	'vbHide

	Private Sub btnEndProcess_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEndProcess.Click
		CType(ListBox1.SelectedItem, Process).CloseMainWindow()
		ListBox1.Items.Remove(ListBox1.SelectedItem)  'add complete process information 
	End Sub

	Private Sub btnHide_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHide.Click
		ShowWindow(CType(ListBox1.SelectedItem, Process).MainWindowHandle, vbHide)
	End Sub

	Private Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
		ShowWindow(CType(ListBox1.SelectedItem, Process).MainWindowHandle, vbNormalNoFocus)
	End Sub


	Private Sub btnGetProcesses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetProcesses.Click
		ListBox1.Items.Clear()
		Dim p() As Process = Process.GetProcessesByName("notepad")
		Dim listBoxItems As New Dictionary(Of String, Integer)
		For Each proc As Process In p
			ListBox1.Items.Add(proc)
		Next
	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		ListBox1.DisplayMember = "MainWindowTitle"	 'only show the window title in list
	End Sub

It worked, but isn't any chance to get the refresh button to get the hided windows? cuz like u said when i hide, then refresh there is an item in blank but cant get it unhide when i refresh.

Bump T.T

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.