Greetings,

If i as for example have this:

This is my main window

Class MainWindow 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim Other As New Window1
        Other.Show()
    End Sub
End Class

This is the window that opens when button is clicked

Public Class Window1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim Main As New MainWindow
        Main.TextBox1.Text = TextBox1.Text
    End Sub
End Class

So what i want it to do is that the textbox1.text in the MainWindow, loads the "Value" from Textbox1.text in the Window1

Dani AI

Generated

Short diagnosis and recommended patterns.

The original code creates a brand‑new MainWindow from the child window, so the visible main window is never updated (that explains "nothing seems to happen"). Suggestions to directly set the other window’s TextBox or to hand a TextBox reference will work, but they tightly couple windows and make maintenance harder. A more robust WPF approach is to share state via a ViewModel (data binding) or to use a simple public property / dialog pattern when full MVVM is overkill.

Example (recommended): share a simple ViewModel instance so both windows bind to the same property. This keeps UI logic out of window code and makes updates automatic.

Public Class SharedViewModel
    Implements System.ComponentModel.INotifyPropertyChanged

    Private _textValue As String
    Public Property TextValue As String
        Get : Return _textValue
        End Get
        Set(ByVal value As String)
            If value <> _textValue Then
                _textValue = value
                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("TextValue"))
            End If
        End Set
    End Property

    Public Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class

Bind TextBoxes to TextValue (TwoWay). Create one SharedViewModel and set it as the DataContext for both windows before showing the child window — changes in either textbox propagate immediately.

A simpler alternative: expose a public property on the child window that gets/sets its textbox text, then set that property from the main window (or use ShowDialog and read the property back). That avoids creating a new main window instance.

Small note about earlier code examples: ’s constructor sample has an apparent variable-name typo when assigning the passed parameter to the field — ensure the constructor assigns the parameter actually passed. Troubleshooting checklist: confirm x:Name is set on TextBoxes, avoid instantiating a new MainWindow to update an existing one, check the Output window for binding errors, and prefer shared ViewModel for clean, maintainable WPF code.

Recommended Answers

All 4 Replies

Hmmm, seems like your using VB2008/10.

Well, heres an idea:

AnotherWindow.Textbox1.Text = MainWindow.Textbox1.Text

Or you can use With, say:

With AnotherWindow
    .Textbox1.Text = MainWindow.Textbox1.Text
End With

Is this?

Can't seem to get it work.. nothing seems to happen

Class MainWindow 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim Other As New Window1(TextBox1)
        Other.Show()
    End Sub
End Class

Public Class Window1
   Private DestTB As TextBox
   Public Sub New(ByVal DestTB As TextBox)
      InitializeComponent()
      Me.DestTB = sourceTB
   End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Me.DestTB.Text = TextBox1.Text
    End Sub
End Class
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        With Form2
            .txtform2.Text = txtform1.Text
            .Show()
        End With

    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.