更新和更改组合框VB.Net的数据源

更新和更改组合框VB.Net的数据源,vb.net,binding,combobox,hashtable,Vb.net,Binding,Combobox,Hashtable,我正在创建一个多客户端服务器应用程序(聊天室)。我在哈希表中“存储”每个连接(实际上是连接对象)。我还想从组合框中查看并选择一个客户机。我设法将combobox绑定到哈希表,但当我尝试更新哈希表和combobox时,它会将每个项目更改为: System.Collections.DictionaryEntry 到 用户选择器是组合框 注册的\u客户端是哈希表(通过tcp连接到服务器的客户端) DB_clients是另一个哈希表(它从数据库中获取用户)。组合框将显示此哈希表中的用户 表格代码

我正在创建一个多客户端服务器应用程序(聊天室)。我在哈希表中“存储”每个连接(实际上是连接对象)。我还想从组合框中查看并选择一个客户机。我设法将combobox绑定到哈希表,但当我尝试更新哈希表和combobox时,它会将每个项目更改为:

System.Collections.DictionaryEntry

用户选择器是组合框
注册的\u客户端是哈希表(通过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
模块

    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\u InfoClient\u Info这样的结构
我尝试在这里重叠数据源

.user_selector.DataSource = Nothing ''clear first
.user_selector.DataSource = bind
只需删除.user\u selector.DataSource=Nothing部分。当我添加新元素时,它就起作用了,但是如果我删除一个用户呢?为什么会出现这样的错误? 问题在哪里