Reputation: 12831
I have 2 forms: Form1 and Form2.
In Form1 I open Form2 in a new Thread by clicking on Button1.
I want to set the title of Form2 but I have no Idea how and whetever it is possible. Here is the code from Form1 , Form2 is just an empty Form:
Public Class Form1
Dim TH As New Threading.Thread(AddressOf FormShow)
Private Shared Sub FormShow()
Dim FF As New Form2
FF.Show()
Do While FF.Visible
Threading.Thread.Sleep(100)
Application.DoEvents()
Loop
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TH.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'How to change the Form2 title?
'I need something like : TH.Form2.Text="NewTitle"?
End Sub
End Class
If I start the code and click on Button1 Form2 will open. But how can I change the window title in Form2 when I press Button2 on Form1? Any ideas? Thanks.
Upvotes: 1
Views: 3345
Reputation: 39122
From my comment above:
The correct approach is to move the WORK (not the Form) onto a different thread. Do a search for BackgroundWorker() control and you'll get a billion examples.
It is technically possible to do what you want in your original question, however:
Public Class Form1
Public Shared FF As New Form2
Dim TH As New Threading.Thread(AddressOf FormShow)
Private Sub FormShow()
Application.Run(FF)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TH.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Not IsNothing(FF) AndAlso Not FF.IsDisposed Then
FF.BeginInvoke(Sub(title)
FF.Text = title
End Sub, New Object() {"Hello"})
End If
End Sub
End Class
This is NOT the recoommended approach, though!
Upvotes: 3