Visual Basic - Reading Label.Text Impossible?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by jonthedit, Feb 28, 2015.

  1. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    It seems I am either missing something or there is a serious flaw.

    I am setting Label1.Text to a variable then I am calling a sub to check what the text of the label is.
    If it is 0 then "yes" should appear
    If it is 1 then "no" should appear.
    Instead the Else function happens no matter what.
    It is like It does not tell there is a match.

    I have tried TextChange too
    I even added .ToString to the end, no dice.
    no luck

    Code:
    Option Explicit On
    Imports System.IO
    Imports System.Net.Sockets
    Public Class Form1
        Dim Listener As New TcpListener(8000)
        Dim Client As TcpClient
        Dim Message As String
        Private Sub _Load() Handles MyBase.Load
            Timer1.Start()
            TimerMLGCheck.Start()
            Listener.Start()
            FunTime()
        End Sub
        Private Sub _FormClosing() Handles Me.FormClosing
            Listener.Stop()
        End Sub
        Public Sub _Tick() Handles Timer1.Tick
     
            Dim nStart As Integer
            Dim nLast As Integer
     
            If Listener.Pending = True Then
                Message = ""
                Client = Listener.AcceptTcpClient()
                Dim Reader As New StreamReader(Client.GetStream())
     
                While Reader.Peek > -1
                    Message &= Convert.ToChar(Reader.Read()).ToString
                End While
     
                If Message.Contains("</>") Then
                    nStart = InStr(Message, "</>") + 4
                    nLast = InStr(Message, "<\>")
                    Message = Mid(Message, nStart, nLast - nStart)
                End If
                Label1.Text = Message
                ActivateHorn()
            End If
        End Sub
        Private Sub Label1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.TextChanged
            If Label1.Text.ToString = "0" Then
                MsgBox("yee")
            ElseIf Label1.Text.ToString = "1" Then
                MsgBox("job")
            End If
     
        End Sub
        Public Sub ActivateHorn()
            If Label1.Text.ToString = "0" Then
                MsgBox("yes")
            ElseIf Label1.Text.ToString = "1" Then
                MsgBox("no")
            Else
                BasicAirhorn()
            End If
        End Sub
        Sub BasicAirhorn()
            My.Computer.Audio.Play(My.Resources.Airhorn,
                AudioPlayMode.Background)
    End Class
    
     
  2. Pleng

    Pleng GBAtemp Advanced Maniac

    Member
    1,712
    860
    Sep 14, 2011
    Thailand
    Well it certainly should work.

    First things first, though. Get rid of the unnecessary ToString() cast.
    Secondly, put a breakpoint on the if clause, then hover your mouse over the Text property to make sure it's definitely been changed to what you were expecting.
     
  3. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    Well it does not. I got rid of the ToString the first time.
    I figured maybe because the data that produces the text is sent over TCP that maybe I needed to cast ToString.
    Still does not work regardless.
     
  4. sarkwalvein

    sarkwalvein Professional asshole at GBATemp

    Member
    GBAtemp Patron
    sarkwalvein is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    5,031
    5,259
    Jun 29, 2007
    Germany
    Niedersachsen
    What are you sending through TCP?
    The ASCII for "0" or the binary value 0?
    Perhaps try
    If Label1.Text.ToString = Convert.ToChar(0).ToString Then ...


    PS: I don't program anything basic related since the early 90s, so sorry for my poor replies.
     
  5. Marth16

    Marth16 Advanced Member

    Newcomer
    68
    18
    Sep 14, 2009
    Gambia, The
    Trying your Horn sub in a small test it does work.
    What is the value of the label when the sub is called?
     
  6. Pleng

    Pleng GBAtemp Advanced Maniac

    Member
    1,712
    860
    Sep 14, 2011
    Thailand
    You might have needed the cast when setting the label's value, but not when reading it back from the label.

    So what happens when you put a break point in the code and hover over the label in the code to view it's value?
     
  7. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    Maybe you do not understand what is happening.
    Please understand it acts as if the condition of = 0 or 1 is never met.
    Also Pleng, i do not understand "hover over the label" because I have not defined a result of what happens on a hover over. (i tried anyway, nothing happens)

     
  8. sarkwalvein

    sarkwalvein Professional asshole at GBATemp

    Member
    GBAtemp Patron
    sarkwalvein is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    5,031
    5,259
    Jun 29, 2007
    Germany
    Niedersachsen
    When he said hover over, I think he meant the following:
    In the debugger, after setting the break point, running the program and getting it to stop (at the breakpoint)...
    Hover the mouse over the expression "Label1.Text" to check its exact value. (over the code, in the debugger)
     
  9. Pleng

    Pleng GBAtemp Advanced Maniac

    Member
    1,712
    860
    Sep 14, 2011
    Thailand
    Learn how to use break points to debug your code. this tutorial looks like a good start - it's for Visual C# but the principles are the same. Put a break point on the following line:

    If Label1.Text.ToString = "0" Then

    When the code is hit, hover your mouse over the .Text part of the line, and see what the reported value is.
     
  10. sarkwalvein

    sarkwalvein Professional asshole at GBATemp

    Member
    GBAtemp Patron
    sarkwalvein is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    5,031
    5,259
    Jun 29, 2007
    Germany
    Niedersachsen
    Also, I think your problem is you have leading or trailing spaces around that 0 in your string.
    Check trimming the strings in your IF lines:

    Code:
        Public Sub ActivateHorn()
            If Label1.Text.ToString.Trim() = "0" Then
                MsgBox("yes")
            ElseIf Label1.Text.ToString.Trim() = "1" Then
                MsgBox("no")
            Else
                BasicAirhorn()
            End If
        End Sub