私は文字列で何もしていないので、これは本当に私を混乱させます。
これは、デバッガーが私に返す詳細です。
System.FormatExceptionが処理されませんでしたMessage=入力文字列が正しい形式ではありませんでした。Source = System.Windows.Forms StackTrace:at System.Windows.Forms.Control.MarshaledInvoke(Control caller、Delegate method、Object [] args、Boolean Synchronous)at System.Windows.Forms.Control.Invoke(Delegate method、Object [ ] args)at Receiver.Class1.CrossThreadAddControl(Control ControlToAdd、Control BaseControl)in C:\ Users \ Jonathan \ Documents \ Visual Studio 2010 \ Projects \ Receiver \ Receiver \ Class1.vb:line 28 at Receiver.ContactList.AddContact(Contact user)C:\ Users \ Jonathan \ document \ visual studio 2010 \ Projects \ Receiver \ Receiver \ ContactList.vb:line 25 at Receiver.Form1.MySub(IAsyncResult ar)in C:\ Users \ Jonathan \ Documents \ Visual Studio System.Netの2010\Projects \ Receiver \ Receiver \ Form1.vb:line45。
基本的に、usercontrol(ContactList)にAddContactというサブがあり、3つの文字列を受け取り、それらを別のUSerControl(Contact)に入れてから、ContactをContactListに追加します。ContactListはメインフォームにあり、AddContactSubが開始されます。別のスレッドから、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セクションに何も書き込まない場合、それは続行され、中断することなく正しく機能します。