I am trying to update a label from one form to another. the code is compiling fine but not updating?
Class MainWindow
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim frm As New Window1
frm.Show()
End Sub
End Class
second form:
Public Class Window1
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
My.Windows.MainWindow.Label1.Content = "dsfdsfsdf"
My.Windows.MainWindow.Label1.UpdateLayout()
End Sub
End Class
it doesn’t update the main form’s label… hope that makes it clearer
So here’s the code that you actually need:
Public Class Window1
End Class
I’m not sure what the
My.Windowscollection is, but theApplication.MainWindowgives you a reference to the window that is set as the start up object in the project properties (or the one you set in your app.xaml.cs file). Previously you were probably getting a reference to a different instance of theWindow1class, hence it was running and not throwing an exceptions, but since it wasn’t the actual visible instance of the window then you didn’t see any changes.