Results 1 to 7 of 7

Thread: Odd Code Problem

  1. #1

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Odd Code Problem

    Hey Ive been racking my brains trying to figure out why my vb is acting like this and by this i mean

    Code:
        Private Sub DoListen()
            constate = "Awaiting Connection"
            Do
    
                Try
                    constate = "Connection Established"
                    Dim tcpClient As TcpClient = listener.AcceptTcpClient()
                    Dim networkStream As NetworkStream = tcpClient.GetStream()
                    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    
    
                    While networkStream.CanRead
                        Do
                            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                            Dim clientdata As String = Encoding.ASCII.GetString(bytes)
    
                            command = clientdata
    thats just part of the do listen sub, which works perfectly.

    but the problem is constate I have a timer which is enabled and started with a interval of 100 and the code on that is

    textbox1.text = constate

    but it doesnt change at all, before constate i was inserting textbox1.text = "connection....." but that didnt work either, only msgbox seemed to work, does anyone know what is wrong or waht would cause such a odd occurance?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Odd Code Problem

    Using a Timer for that is a hack anyway. When constate changes you should simply call a method that will explicitly update your TextBox:
    vb.net Code:
    1. Private Delegate Sub UpdateTextBoxCallback(ByVal text As String)
    2.  
    3. Private Sub UpdateTextBoxCallback(ByVal text As String)
    4.     If Me.InvokeRequired Then
    5.         Me.Invoke(New UpdateTextBoxCallback(AddressOf UpdateTextBoxCallback), text)
    6.     Else
    7.         Me.TextBox1.Text = text
    8.     End If
    9. End Sub
    Now you just do like this:
    vb.net Code:
    1. Me.UpdateTextBox("Connection Established")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Odd Code Problem

    I think jmcilhinney was a little fast there, the example above might give you some errors. Try this instead

    vb.net Code:
    1. Private Delegate Sub UpdateTextBoxCallback(ByVal text As String)
    2.  
    3. Private Sub UpdateTextBox(ByVal text As String)
    4.     If Me.InvokeRequired Then
    5.         Me.Invoke(New UpdateTextBoxCallback(AddressOf UpdateTextBox), text)
    6.     Else
    7.         Me.TextBox1.Text = text
    8.     End If
    9. End Sub
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Odd Code Problem

    Quote Originally Posted by _powerade_ View Post
    I think jmcilhinney was a little fast there, the example above might give you some errors. Try this instead

    vb.net Code:
    1. Private Delegate Sub UpdateTextBoxCallback(ByVal text As String)
    2.  
    3. Private Sub UpdateTextBox(ByVal text As String)
    4.     If Me.InvokeRequired Then
    5.         Me.Invoke(New UpdateTextBoxCallback(AddressOf UpdateTextBox), text)
    6.     Else
    7.         Me.TextBox1.Text = text
    8.     End If
    9. End Sub
    Good point. That's what happens when you type straight into the forum instead of into the IDE and then copy and paste.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Odd Code Problem

    Well, it felt good correcting one of the pro's
    Last edited by Arve K.; Nov 5th, 2009 at 08:45 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  6. #6

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Re: Odd Code Problem

    ahhh so thats the pro way to do it =) thankyou both works like a charm

  7. #7
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Odd Code Problem

    Remember to mark as resolved
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width