VB.Net label not showing

Simon Scott 306 Reputation points
2021-06-14T14:54:31.173+00:00

Afternoon all.

I'm hoping someone can help me with this annoyance.

Basically where i have the line  LblStatus.Text = "check IP address!", this only displays if i have exit sub after it. If i take that away, then this message never shows. I'm a bit stumped why it doesn't. I'm no expert in programming! I want the code to just go round in a loop every 60 seconds and do the IP address check. I'm sure it's a simple answer!

Here is my code, slightly shortened as it is a bit repetitive...

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

ticker:

Dim sString As String

        Dim strIPAddress As String

        Dim strhostname As String

        Dim Prefix As String

        strhostname = System.Net.Dns.GetHostName()

        strIPAddress = System.Net.Dns.GetHostByName(strhostname).AddressList(0).ToString()

        lblIPAddress.Text = "IP Address: " & strIPAddress

        Try

            Prefix = strhostname.Substring(0, 3)

            'Check the IP address of the local machine

            Dim ipArray() As String = Split(strIPAddress, ".")

If ipArray(1) = "44" And ipArray(2) = "10" Then

                                            LblStatus.Text = "Your PC is updating, please wait......."

                                            LblShip.Text = "This PC is in the Office"

                                            Using sr As New StreamReader("\bm-filesrv\Timesync$\timezone.ini")

                                                sString = sr.ReadToEnd()

                                            End Using

                                        Else

                                            Try

                                                LblShip.ForeColor = Color.Red

                                                LblStatus.ForeColor = Color.Red

                                                LblShip.Text = "Unable to identify  location"

                                                LblStatus.Text = "check IP address!"

                                                Exit Sub

                                                GoTo Timertick

                                            Catch ex As Exception

                                                LblStatus.Text = (ex.Message)

                                            End Try

goto ticker

end if

Appreciate any assistance with this.

Thanks
Simon

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Simon Scott 306 Reputation points
    2021-06-14T19:49:53.287+00:00

    Good evening,

    Apologies for my poor explanation!

    I have actually managed to fix the issue. I had declared a string but not set it to Nothing.

    This caused an issue further down the code, but is now resolved.

    Really appreciate your assistance.

    Thanks
    Simon

    1 person found this answer helpful.

  2. Dewayne Basnett 1,361 Reputation points
    2021-06-14T15:10:58.007+00:00

    The problem is that Forms timers run on the UI. To fix...

    First, make sure Timer1 is enabled and that the Interval is set to 60000. That will take care of the code running once every 60 seconds. Then get rid of all the GoTo's and code labels. The example you posted will look like this then,

        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            Dim sString As String
    
            Dim strIPAddress As String
    
            Dim strhostname As String
    
            Dim Prefix As String
    
            strhostname = System.Net.Dns.GetHostName()
    
            strIPAddress = System.Net.Dns.GetHostByName(strhostname).AddressList(0).ToString()
    
            lblIPAddress.Text = "IP Address: " & strIPAddress
    
            Try
    
                Prefix = strhostname.Substring(0, 3)
    
                'Check the IP address of the local machine
    
                Dim ipArray() As String = Split(strIPAddress, ".")
    
                If ipArray(1) = "44" And ipArray(2) = "10" Then
    
                    LblStatus.Text = "Your PC is updating, please wait......."
    
                    LblShip.Text = "This PC is in the Office"
    
                    Using sr As New StreamReader("\\bm-filesrv\Timesync$\timezone.ini")
    
                        sString = sr.ReadToEnd()
    
                    End Using
    
                Else
    
                    Try
    
                        LblShip.ForeColor = Color.Red
    
                        LblStatus.ForeColor = Color.Red
    
                        LblShip.Text = "Unable to identify  location"
    
                        LblStatus.Text = "check IP address!"
    
                    Catch ex As Exception
    
                        LblStatus.Text = (ex.Message)
    
                    End Try
    
                End If
            Catch
            End Try
    
        End Sub
    

  3. Dewayne Basnett 1,361 Reputation points
    2021-06-14T15:46:14.497+00:00

    I have no idea what you are trying to accomplish with this. Here is a starting point,

        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim hn As String = Net.Dns.GetHostName()
            Dim he As Net.IPHostEntry = Net.Dns.GetHostEntry(hn)
            Dim s As New System.Text.StringBuilder
            s.AppendFormat("Host: {0} has {1} IP addresses. DNS Hostname: {2}", hn, he.AddressList.Count, he.HostName)
            s.AppendLine()
            s.AppendLine("The addresses are")
            For Each ip As Net.IPAddress In he.AddressList
                s.AppendFormat("{0}  {1}", ip.AddressFamily, ip.ToString)
                s.AppendLine()
            Next
            Debug.WriteLine(s.ToString)
        End Sub
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.