我正在创建一个多客户端服务器应用程序(聊天室)。我在散列表中“存储”每个连接(实际上是连接对象)。我还想从组合框中看到并选择一个客户端。我设法将组合框绑定到哈希表,但是当我尝试更新哈希表然后更新组合框时,它会将每个项更改为:
System.Collections.DictionaryEntry
user_selector 是组合框
registered_clients 是哈希表(客户端通过tcp连接到服务器)
DB_clients 是另一个哈希表(它从数据库中获取用户)。组合框将显示来自此哈希表的用户。
表格代码
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
form = Me ''The variable is used in the module
conn_listener.Start()
conn_listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf new_client), conn_listener)
End Sub
Private Sub new_client(ByVal ar As IAsyncResult)
total_logged_clients += 1
temp_client = New Client(conn_listener.EndAcceptTcpClient(ar))
AddHandler temp_client.Connected, AddressOf Connected
AddHandler temp_client.Disconnected, AddressOf Disconnected
AddHandler temp_client.New_Message, AddressOf New_Message
Update_Log_Data("New user found & added. Waiting for details...")
conn_listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf new_client), conn_listener) ''Loop
End Sub
Private Sub Connected(ByVal user As Client_Info)
total_registered_clients += 1
registered_clients.Add(user.ID, temp_client) '' Register each client based on its ID (in the hashtable)
Update_Log_Data("Confirmed client:" & user.Computer_Name & "," & user.ID)
RaiseEvent Update_GUI() ''Function from the module
End Sub
Private Sub Disconnected(ByVal user As Client_Info, ByVal reason As System.Exception)
Dim class_for_disposal As Client
total_logged_clients -= 1
If Not user.Connection_Status = Enum_Connection_Status.NO_INFO Then
total_registered_clients -= 1
End If
''Dispose this class
If registered_clients.ContainsKey(user.ID) Then
class_for_disposal = registered_clients.Item(user.ID)
registered_clients.Remove(user.ID)
class_for_disposal.Dispose()
End If
Update_Log_Data("Deconnected:" & user.Computer_Name & "," & user.ID & " because:" & reason.Message, 1)
RaiseEvent Update_GUI()
End Sub
Private Sub user_selector_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles user_selector.SelectionChangeCommitted
Dim temp_selection As New DictionaryEntry
temp_selection = user_selector.SelectedItem
If registered_clients.ContainsKey(temp_selection.Key) Then
'' If the selected user is in the hastable then
''set "selected_user" to the object created by the user
'' With this object I can sendand receive from the selected user.
selected_user = registered_clients.Item(temp_selection.Key)
Else
selected_user = Nothing
End If
''Update_Data_GUI()
RaiseEvent Update_GUI()
End Sub
模块
Private Sub Update_GUI() Handles form.Update_GUI
''Called often by controls
Interface_DB_clients()
Interface_bottom_conn_status()
End Sub
Private Sub Interface_bottom_conn_status()
With form
If .bottom_band.InvokeRequired Then
.Invoke(New Repeat(AddressOf Interface_bottom_conn_status))
Else
If selected_user Is Nothing Then
.bottom_client_status.ForeColor = Color.Red
.bottom_client_status.Text = "Offline"
Else
.bottom_client_status.ForeColor = Color.Green
.bottom_client_status.Text = "Online"
End If
End If
End With
End Sub
Private Sub Interface_DB_clients()
Dim preserve_item As DictionaryEntry
Dim bind As New BindingSource
With form
If .user_selector.InvokeRequired Then
.Invoke(New Repeat(AddressOf Interface_DB_clients))
Else
'' Here it's being made the refresh
preserve_item = .user_selector.SelectedItem
bind.DataSource = DB_clients
.user_selector.DataSource = Nothing ''clear first
.user_selector.DataSource = bind
.user_selector.ValueMember = "Value"
'' Selected old value
.user_selector.SelectedItem = preserve_item
End If
End With
End Sub
我为客户创建了特殊课程,这就是为什么你可以看到像user id 这样的属性以及 Header_Info 和 Client_Info 等结构的原因。
我尝试重叠数据源,这里
.user_selector.DataSource = Nothing ''clear first
.user_selector.DataSource = bind
只需删除 .user_selector.DataSource = Nothing 部分。当我添加新元素时它会起作用,但如果删除用户该怎么办?为什么它会显示错误? 问题出在哪儿?