嗨 我有一個工作非常小的vb.net項目,但它使用一個專有的com對象,這是在VB項目中有線來處理事件!vb.net到C#幫助
我已經改變了COM對象的名稱,但語法在下面的代碼是正確的:
Public Class Form1
Private WithEvents PM As comObj.PManager
Delegate Sub SetTextCallback([text] As String)
Private Function PreTransmit(ByVal s As String) As String Handles PM.PreTransmit
Me.SetText(s)
Return s
End Function
Private Function PreResponse(ByVal s As String) As String Handles PM.PreResponse
Return s
End Function
Private Sub SetText(ByVal [text] As String)
If Me.textBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.textBox1.Text = [text]
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
PM = CreateObject("ObjName.PManager")
End Sub
End Class
上面的代碼創建對象被點擊的按鈕,每當對象獲得的數據也引發一個事件時,將數據傳遞到傳輸前或接收前。
我只是不能似乎找出如何做到這一點的C#
VB
Private WithEvents PM As comObj.PManager
C#
private comObj.PManager PM;
但什麼WITHEVENTS?
VB
Private Function PreResponse(ByVal s As String) As String Handles PM.PreResponse
Return s
End Function
C#
private string PreResponse(string s)
{
return s;
}
但什麼把手PM.PreResponse?
VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
PM = CreateObject("ObjName.PManager")
End Sub
C#
private void Button1_Click(System.Object sender, System.EventArgs e)
{
PM = Interaction.CreateObject("ObjName.PManager");
}
不知道是我要去錯了!?
我可以寫一個vb.net類並在c#中使用它嗎?
你嘗試過一個轉換器,如:http://www.developerfusion.com/tools/convert/vb-to-csharp/ – Matt
@Matt - 我的最愛之一。 –
感謝這顯然得到了它的大部分,但一行我仍然有一個問題,阻止我編譯它。 PM = Interaction.CreateObject(「ObjName.PManager」); 這仍然相同,谷歌表示,其在Microsoft.VisualBasic程序命名空間 但是我得到 錯誤名稱「互動」不存在當前上下文 – Adrian