Question Textbox is not displaying value

crystaluz

Well-known member
Joined
Feb 17, 2009
Messages
55
Programming Experience
Beginner
Hello there... I use these codes to print out the location of the device. The problem is, the value is not appearing in the textbox (txtLatitude) when the form is loaded. But when I replace the textbox with MsgBox(Latitude) it works perfectly. I just don't understand why the value is not appearing in the textbox. Any suggestion? Thank you...

Imports System.Device.Location
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim myLocation As New Form1()
        myLocation.GetLocationDataEvent()


    End Sub

    Private WithEvents watcher As GeoCoordinateWatcher
    Public Sub GetLocationDataEvent()
        watcher = New System.Device.Location.GeoCoordinateWatcher()
        AddHandler watcher.PositionChanged, AddressOf watcher_PositionChanged
        watcher.Start()

    End Sub

    Private Sub watcher_PositionChanged(ByVal sender As Object, ByVal e As GeoPositionChangedEventArgs(Of GeoCoordinate))
        PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude)
        ' Stop receiving updates after the first one.
        watcher.Stop()
    End Sub

    Private Sub PrintPosition(ByVal Latitude As Double, ByVal Longitude As Double)

               txtLatitude.text = Latitude

    End Sub

End Class
 
The value IS appearing in the TextBox. It's just that the form containing the TextBox is not the form you're looking at. Here is your issue:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim myLocation As New Form1()
    myLocation.GetLocationDataEvent()
End Sub
When your Form1 form loads, it immediately creates a new Form1 and tells it to get and display the location data. That new Form1 form never gets displayed so you never see the data.

Think about it. Why would a Form1 object that just got loaded create a new Form1 object? How many Form1 objects do you want? If you only want one then why is the first one creating a second one?
 
Back
Top