vb.net:listbox.items.add()在同一个类中引发异常 [英] vb.net: listbox.items.add() throws exception in same class

查看:674
本文介绍了vb.net:listbox.items.add()在同一个类中引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否理解这种情况,足以拿出正确的标题。我来自对VB6的一个温和的理解,必须爬上VB 2010的陡峭的学习曲线。



我正在尝试创建一个多客户端服务器程序,我的企业iPhone应用程序我在这里找到一个比较简单的例子: http://www.strokenine.com/blog /?p = 218 。我已经能够修改足够的代码,使其与我的应用程序一起使用,但是有一个问题:即使方法在窗体的类中被调用,我无法访问表单上的控件来添加项目。 (我也尝试过这个原始代码,它也是一样的,我不知道作者如何设法使它工作。)



这是代码段:

 公共类服务器带有控件的窗体位于此类中。 
Dim clients As New Hashtable'new database(hashtable)to hold the clients


Sub receive(ByVal msg As String,ByVal client As ConnectedClient)
Dim message ()As String = msg.Split(|)'创建一个包含消息元素的数组
选择案例消息(0)'由数组中的第一个元素处理
案例CHAT '如果是CHAT
TextBox3.Text& = client.name& 说:& &消息(1)& vbNewLine'将消息添加到聊天框
sendallbutone(message(1),client.name)'这将使用新消息
'更新所有客户端,并且不会将消息发送给接收的客户端它从:)
案例LOGIN'客户端连接
clients.Add(客户端,客户端名称)'添加客户端到我们的数据库(一个散列表)
ListBox1.Items。添加(client.name)'将客户端添加到列表框以显示新用户
结束选择

End Sub

在LOGIN的情况下,代码会尝试将登录ID添加到列表框。它抛出一个异常:System.InvalidOperationException类型的第一次机会异常发生在System.Windows.Forms.dll列表框(所有控件,对于这个问题)在同一个类,Server.vb和Server.vb [设计]。



数据来自另一个客户端登录时创建的类,这引发了切换回Server类的事件:

 公共类ConnectedClient 

公共事件gotmessage(ByVal消息As String,ByVal客户端作为ConnectedClient)'当我们得到一个客户端的消息
公共事件断开(ByVal客户端作为ConnectedClient)'当我们得到客户端断开连接

Sub read(ByVal ar As IAsyncResult)'时,这将被提升,这将处理所有消息收到
尝试
Dim sr As New StreamReader(cli.GetStream)'初始化一个新的流程器,它将从客户端的流中读取
Dim msg As String = sr.ReadLine()'创建一个新的变量将用于保存消息被读取
RaiseEvent gotmessage(msg,Me)'告诉服务器一条消息已被接收。我作为一个参数传递,代表
'它已经收到消息的当前客户端执行任何客户端特定的
'任务(如果需要)
cli.GetStream.BeginRead(New Byte() {0},0,0,AddressOf read,Nothing)'继续从流读取
Catch ex As Exception
尝试'如果在读取目的中发生错误,我们将再次尝试阅读如果我们仍然可以读取
Dim sr As New StreamReader(cli.GetStream)'初始化一个新的Streamreader,它将从客户端的流中读取
Dim msg As String = sr.ReadLine()'创建一个新变量这将用于保存消息被读取
RaiseEvent gotmessage(msg,Me)'告诉服务器一条消息已被接收。我作为一个参数传递,代表
'它已经收到消息的当前客户端执行任何客户端特定的
'任务(如果需要)
cli.GetStream.BeginRead(New Byte() {0},0,0,AddressOf read,Nothing)'继续从流中读取
如果我们仍然无法读取
RaiseEvent已断开连接(Me)'我们可以确定客户端已断开
结束尝试

结束尝试
结束子

我希望我对这一切很有意义。这一切似乎都来回弹起来,似乎很复杂。



我试过使用Me.listbox1和Server.listbox1等几种类似的结构,但是没有可以使用



我正在阅读很多关于Invoke和Delegates的信息,但是如果方法和控件在同一个类中,那么这是必要的吗?或者我对一个课程有一个根本的误解?



非常感谢任何帮助我可以得到。

私有子UpdateList(byval itemName作为字符串)
如果我.InvokeRequired Then
Me.Invoke(New UpdateListDelegate(AddressOf UpdateList),itemName)
Else
'UpdateList
'添加列表添加代码
ListBox1.Items.Add itemName)
End If
End Sub

添加上面,然后替换: / p>

  ListBox1.Items.Add(client.name)

to

  UpdateList(client.name)

它是否正常工作?检查语法,可能在输入时输入错字。


I'm not even sure I understand this situation enough to come up with a proper title. I come from a modest understanding of VB6 and having to climb a steep learning curve for VB 2010.

I am trying to create a multi-client server program that will communicate with my Enterprise iPhone app. I found a relatively simple example to build upon here: http://www.strokenine.com/blog/?p=218. I have been able to modify the code enough to make it work with my app, but with one glitch: I can't get access to the controls on the form to add items, even though the method is invoked within the form's class. (I tried this on the original code too, and it does the same thing. I don't know how the author managed to get it to work.)

Here's the code segment in question:

Public Class Server 'The form with the controls is on/in this class.
Dim clients As New Hashtable 'new database (hashtable) to hold the clients


    Sub recieved(ByVal msg As String, ByVal client As ConnectedClient)
    Dim message() As String = msg.Split("|") 'make an array with elements of the message recieved
    Select Case message(0) 'process by the first element in the array
        Case "CHAT" 'if it's CHAT
            TextBox3.Text &= client.name & " says: " & " " & message(1) & vbNewLine 'add the message to the chatbox
            sendallbutone(message(1), client.name) 'this will update all clients with the new message
            '                                       and it will not send the message to the client it recieved it from :)
        Case "LOGIN" 'A client has connected
            clients.Add(client, client.name) 'add the client to our database (a hashtable)
            ListBox1.Items.Add(client.name) 'add the client to the listbox to display the new user
    End Select

End Sub

Under Case "LOGIN" the code tries to add the login ID to the listbox. It throws an exception: "A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll" The listbox (all controls, for that matter) is in the same class, Server.vb and Server.vb [Design].

The data comes in from another class that is created whenever a client logs on, which raises the event that switches back to the Server class:

Public Class ConnectedClient

Public Event gotmessage(ByVal message As String, ByVal client As ConnectedClient)    'this is raised when we get a message from the client
Public Event disconnected(ByVal client As ConnectedClient)    'this is raised when we get the client disconnects

Sub read(ByVal ar As IAsyncResult) 'this will process all messages being recieved
    Try
        Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
        Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
        RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents 
        '                               the current client which it has recieved the message from to perform any client specific
        '                               tasks if needed
        cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
    Catch ex As Exception
        Try 'if an error occurs in the reading purpose, we will try to read again to see if we still can read
            Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
            Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
            RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents 
            '                               the current client which it has recieved the message from to perform any client specific
            '                               tasks if needed
            cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
        Catch ' IF WE STILL CANNOT READ
            RaiseEvent disconnected(Me)  'WE CAN ASSUME THE CLIENT HAS DISCONNECTED
        End Try

    End Try
End Sub

I hope I am making sense with all this. It all seems to bounce back and forth, it seems so convoluted.

I've tried using Me.listbox1 and Server.listbox1 and several other similar structures, but to no avail.

I'm reading a lot about Invoke and Delegates, but would that be necessary if the method and the control are in the same class? Or do I have a fundamental misperception of what a class is?

Many thanks for any help I can get.

解决方案

    Private Delegate Sub UpdateListDelegate(byval itemName as string)
    Private Sub UpdateList(byval itemName as string)
    If Me.InvokeRequired Then
        Me.Invoke(New UpdateListDelegate(AddressOf UpdateList), itemName)
    Else
        ' UpdateList
        ' add list add code
        ListBox1.Items.Add(itemName)
    End If
    End Sub

Add above, then replace:

   ListBox1.Items.Add(client.name)

to

   UpdateList(client.name)

Does it work? check the syntax, may have typo as I type it.

这篇关于vb.net:listbox.items.add()在同一个类中引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆