这真的让我很困惑,因为我没有对字符串做任何事情。
这是调试器返回给我的详细信息:
System.FormatException 未处理 Message=输入字符串的格式不正确。Source=System.Windows.Forms StackTrace:在 System.Windows.Forms.Control.MarshaledInvoke(控制调用者,委托方法,对象 [] 参数,布尔同步)在 System.Windows.Forms.Control.Invoke(委托方法,对象 [ ] args) 在 Receiver.Class1.CrossThreadAddControl(Control ControlToAdd, Control BaseControl) 在 C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\Receiver\Receiver\Class1.vb: Receiver.ContactList.AddContact(Contact) 的第 28 行用户)在 C:\Users\Jonathan\documents\visual studio 2010\Projects\Receiver\Receiver\ContactList.vb:line 25 at Receiver.Form1.MySub(IAsyncResult ar) in C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\Receiver\Receiver\Form1.vb:System.Net 的第 45 行。
基本上在 usercontrol(ContactList) 中有一个名为 AddContact 的 sub,它接受 3 个字符串并将它们放入另一个 USerControl (Contact),然后将联系人添加到 ContactList 联系人列表在主窗体上,并且 AddContact Sub 启动来自不同的线程,这就是需要 Invoke 的原因。
Public Class ContactList
Sub AddContact(ByVal user As Contact)
If Me.Controls.Count = 0 Then
user.Location = New Drawing.Point(0, 0)
Else
user.Location = New Drawing.Point(0, Me.Controls.Count * 20)
End If
user.Width = Me.Width
user.Displayname = user.Username
For Each UC As Control In Me.Controls
If TypeOf UC Is Contact Then
If CType(UC, Contact).Username = user.Username Then
user.Displayname = user.Username & "@" & user.PCname
End If
End If
Next
Class1.CrossThreadAddControl(user, Me)
End Sub
End Class
它是带有 2 个星号的行(在代码中不是实际的),这显然是导致问题的原因
Shared Sub CrossThreadAddControl(ByVal ControlToAdd As Control, ByVal BaseControl As Control)
If BaseControl.InvokeRequired Then
Dim d As New AddUserD(AddressOf AddUser)
**BaseControl.Invoke(d, ControlToAdd, BaseControl)**
End If
End Sub
Delegate Sub AddUserD(ByVal ControlToAdd As Control, ByVal BaseControl As Control)
Shared Sub AddUser(ByVal ControlToAdd As Control, ByVal BaseControl As Control)
BaseControl.Controls.Add(ControlToAdd)
End Sub
那么任何想法为什么它说输入字符串的格式不正确?(哦,如果我捕获了异常(使用 Try 和 Catch)并且不在 catch 部分中编写任何内容,它只会继续并且它可以正常工作而不会中断。