Visual Basic - Reading Label.Text Impossible?

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,030
Country
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
 

Pleng

Custom Title
Member
Joined
Sep 14, 2011
Messages
2,468
Trophies
2
XP
2,932
Country
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.
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,030
Country
Bangladesh
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.

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.
 

sarkwalvein

There's hope for a Xenosaga port.
Member
Joined
Jun 29, 2007
Messages
8,601
Trophies
2
Age
41
Location
Niedersachsen
XP
11,551
Country
Germany
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.
 

Marth16

Well-Known Member
Newcomer
Joined
Sep 14, 2009
Messages
68
Trophies
0
XP
202
Country
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?
 

Pleng

Custom Title
Member
Joined
Sep 14, 2011
Messages
2,468
Trophies
2
XP
2,932
Country
Thailand
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.

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?
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,030
Country
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)

 

sarkwalvein

There's hope for a Xenosaga port.
Member
Joined
Jun 29, 2007
Messages
8,601
Trophies
2
Age
41
Location
Niedersachsen
XP
11,551
Country
Germany
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)



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)
 

Pleng

Custom Title
Member
Joined
Sep 14, 2011
Messages
2,468
Trophies
2
XP
2,932
Country
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.
 

sarkwalvein

There's hope for a Xenosaga port.
Member
Joined
Jun 29, 2007
Messages
8,601
Trophies
2
Age
41
Location
Niedersachsen
XP
11,551
Country
Germany
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
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    Veho @ Veho: Delayed onset muscle fuckery is kicking my butt.