2013-11-14 15 views
1

我使用的是VB2005中創建的表單,在按下按鈕後打開程序,然後在文本字段中顯示進程ID(再次按下按鈕時)。當我運行它時,窗體將打開程序(Notepad.exe),但是當我單擊按鈕以查看進程ID時,Visual Studio 2005會提示:如何在VB表單啓動的VB表單中獲得進程ID?

InvalidCastException未處理並突出顯示「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 

當我試圖通過改變PROCID聲明爲一個字符串:

Dim ProcID As String 

VS2005提供了錯誤:

Value of type 'System.Diagnostics.Process' cannot be converted to 'String'.

我已經試過聲明昏暗PROCID爲整數,並得到:

Value of type 'System.Diagnostics.Process' cannot be converted to 'Integer'.

我也試過,沒有運氣以下變化:

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 

該錯誤說:

Class 'System.Diagnostics.Process' cannot be indexed because it has no default property.

請幫忙!這真讓我抓狂!

感謝

+0

今天是什麼? VS-2005意識日還是什麼? :)(前一段時間涉及VS 2005的另一個問題)。對不起沒有幫助(沒有VS 2005,不能確定這是不是問題的根源);只是想建議你轉移到一個更新的VS和一個小的更正:它是VS 2005. VB遵循不同的計數;它對應於維基百科的VB 8.0(http://en.wikipedia.org/wiki/Visual_Basic_.NET)。 – varocarbas

回答

0

下面的變化將讓你的進程ID,並把它到文本框中。

ProcID = Process.GetCurrentProcess.Id 
TextBox1.Text = ProcID.ToString 

如果您有問題,請留言。

+0

工作正常!非常感謝你! :) – user2993272