How Do I get the Process ID within a VB Form of a Process I Launched with that VB Form?
The following change will get you the process ID and put it into the textbox.
ProcID = Process.GetCurrentProcess.Id
TextBox1.Text = ProcID.ToString
Comment if you have issues.
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
I'm using a form I made in VB2005 to open a program upon button press and then in a text field display the Process ID (again upon button press). When I run it the form will open the program (Notepad.exe) but when I click the button to view the Process ID Visual Studio 2005 says:
InvalidCastException was unhandled and highlights the line "TextBox1.Text = ProcID"
Imports System Imports System.Diagnostics Public Class Form1 Dim myProcess As New Process Dim ProcID Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Launch.Click myProcess.StartInfo.FileName = "notepad.exe" myProcess.Start() End Sub Private Sub GetID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetID.Click ProcID = Process.GetCurrentProcess() TextBox1.Text = ProcID End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class
When I try to change the ProcID declaration into a string by:
Dim ProcID As String
VS2005 gives the error:
Value of type 'System.Diagnostics.Process' cannot be converted to 'String'.
I've tried declaring the Dim ProcID As Integer and got:
Value of type 'System.Diagnostics.Process' cannot be converted to 'Integer'.
I've also tried the following change with no luck:
Private Sub GetID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetID.Click ProcID = Process.GetCurrentProcess(myProcess) TextBox1.Text = CInt(ProcID) End Sub
That error says:
Class 'System.Diagnostics.Process' cannot be indexed because it has no default property.
Please help! This is driving me crazy!
Thanks