什么是本代码的VB.NET版本?

时间:2010-04-21 15:30:10

标签: c# vb.net c#-to-vb.net

if (InvokeRequired)
{
  BeginInvoke(new MethodInvoker(delegate()
  {
    textBox1.Text = args.Fax.Port.ToString();
    textBox2.Text = args.Fax.FaxStatus.ToString();
  }));
}

6 个答案:

答案 0 :(得分:5)

developerfusion.com提供:

If InvokeRequired Then
    BeginInvoke(New MethodInvoker(Function() Do
        textBox1.Text = args.Fax.Port.ToString()
        textBox2.Text = args.Fax.FaxStatus.ToString()
    End Function))
End If

答案 1 :(得分:5)

来自MSDN

Delegate Sub MyDelegate(myControl As Label, myArg2 As String)

Private Sub Button_Click(sender As Object, e As EventArgs)
   Dim myArray(1) As Object

   myArray(0) = New Label()
   myArray(1) = "Enter a Value"
   myTextBox.BeginInvoke(New MyDelegate(AddressOf DelegateMethod), myArray)
End Sub 

Public Sub DelegateMethod(myControl As Label, myCaption As String)
   myControl.Location = New Point(16, 16)
   myControl.Size = New Size(80, 25)
   myControl.Text = myCaption
   Me.Controls.Add(myControl)
End Sub 

所以

if (InvokeRequired)
{
  BeginInvoke(new MethodInvoker(delegate()
  {
    textBox1.Text = args.Fax.Port.ToString();
    textBox2.Text = args.Fax.FaxStatus.ToString();
  }));
}

Delegate Sub MyDelegate(faxPort As String, faxStatus As String)

If InvokeRequired Then
    Dim aArray(1) as Object
    aArray(0) = args.Fax.Port.ToString()
    aArray(1) = args.Fax.FaxStatus.ToString();
    BeginInvoke(New MyDelegate(AddressOf MySub), aArray)
End If

Sub MySub( faxPort as String, faxStatus as String)
    textBox1.Text = faxPort
    textBox2.Text = faxStatus
End Sub

我认为

答案 2 :(得分:4)

VB.NET 9和之前的版本不支持多行匿名函数。您需要编写一个单独的函数:

If InvokeRequired Then
    BeginInvoke(New MethodInvoker(AddressOf MySub))
End If

MySub:

Sub MySub()
    textBox1.Text = args.Fax.Port.ToString()
    textBox2.Text = args.Fax.FaxStatus.ToString()    
End Sub

更新:

为了澄清args参数,可以使用BeginInvoke的重载,在这种情况下,MethodInvoker委托不再适用。 Action(Of T)可行:

If InvokeRequired Then
    BeginInvoke(New Action(Of SomeType)(AddressOf MySub), args)
End If

Sub MySub(ByVal args as SomeType)
    textBox1.Text = args.Fax.Port.ToString()
    textBox2.Text = args.Fax.FaxStatus.ToString()    
End Sub

毕竟这样的简单事情让C#开发人员感到高兴:-)(请不要误解,我没有反对VB.NET)

答案 3 :(得分:3)

在Visual Studio 2010之前,VB.Net不支持多语句匿名函数。

您需要将匿名方法移动到一个单独的方法中,该方法将args作为参数,然后在其上调用Invoke

答案 4 :(得分:1)

由于评论不允许格式化,我将在此处发布我的工作解决方案。感谢所有插话的人 - 达林,SLaks,drachenstern,Jay。

Private Sub myControl_FaxStatus(ByVal sender As Object, ByVal args As DataTech.FaxManNet.FaxEventArgs)
  frmModemStatus.UpdateStatus(args.Fax)

  If InvokeRequired Then
    Dim aArray(1) As Object
    aArray(0) = args.Fax.Port.ToString()
    aArray(1) = args.Fax.FaxStatus.ToString()
    BeginInvoke(New MyDelegate(AddressOf UpdateStatusDisplay), aArray)
  End If

  Try
    TextBox1.Text = args.Fax.Port.ToString()
    TextBox2.Text = args.Fax.FaxStatus.ToString()
  Catch ex As Exception
    System.Diagnostics.Debug.WriteLine("ex = " & ex.ToString())
  End Try

End Sub

Delegate Sub MyDelegate(ByVal faxPort As String, ByVal faxStatus As String)

Private Sub UpdateStatusDisplay(ByVal faxPort As String, ByVal faxStatus As String)
  TextBox1.Text = faxPort
  TextBox2.Text = faxStatus
End Sub

答案 5 :(得分:0)

如果您需要此功能中的类似内容,只需将以下组件安装到Visual Studio中即可。 之后,您可以轻松地将c#转换为vb.net或vb.net转换为c#。 VS2010支持已包含在内。 http://codeconvert.codeplex.com/