Hi,
I have a main form which starts up a thread as well as a 2nd form.
How do I make it so that I can access things such as labels on the 2nd form from the thread that was started on form1?
I have some sample code below... - Public Class Form1
-
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
Form2.Show()
-
Form2.BringToFront()
-
signal = New Class1
-
End Sub
-
Private signal As Class1
-
-
End Class
-
-
-
Public Class Class1
-
Public Sub New()
-
trd = New Threading.Thread(AddressOf task)
-
trd.IsBackground = True
-
trd.Start()
-
End Sub
-
Private trd As Threading.Thread
-
Private Sub task()
-
For i As Integer = 0 To 5
-
Threading.Thread.Sleep(5000)
-
Form2.Label1.Text = i.ToString
-
Next
-
End Sub
-
End Class
-
Any help would be much appreciated.
Thanks,
Wagz
7 1291
have you tried passing the instance of Form1 to both Form2 and signal ?
It appears that every time I'm in the created thread and call something from form2 that it creates a new instance of the form. How do I reference the form that form1 created from the created thread?
The best approach to this would be to create an event in Class1 that gets raised whenever you want to indicate progress from your thread. -
For i As Integer = 0 To 5
-
Threading.Thread.Sleep(5000)
-
' Form2.Label1.Text = i.ToString
-
' singal event here
-
Next
-
Then from your Form1 class you can handle the event and then set your label for form2 -
Private Sub SomeEventHandler
-
' If Me.InvokeRequired
-
' Me.Invoke(new EventHandler(SomeEventHandler))
-
' End If
-
'
-
' Set label for Form2
-
End Sub
-
Note that you need to invoke back to the main thread in order to set the label for Form2.
I hope this helps, it is pseudo code but should hopefully explain what you need to do.
Hi,
I have a main form which starts up a thread as well as a 2nd form.
How do I make it so that I can access things such as labels on the 2nd form from the thread that was started on form1?
I have some sample code below... - Public Class Form1
-
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
Form2.Show()
-
Form2.BringToFront()
-
signal = New Class1
-
End Sub
-
Private signal As Class1
-
-
End Class
-
-
-
Public Class Class1
-
Public Sub New()
-
trd = New Threading.Thread(AddressOf task)
-
trd.IsBackground = True
-
trd.Start()
-
End Sub
-
Private trd As Threading.Thread
-
Private Sub task()
-
For i As Integer = 0 To 5
-
Threading.Thread.Sleep(5000)
-
Form2.Label1.Text = i.ToString
-
Next
-
End Sub
-
End Class
-
Any help would be much appreciated.
Thanks,
Wagz
If I pass Form2 to Class1 when I create it and create a delegate to handle the changing the text then it works.
I passed it in to Class1 in the constructor, is there a better way to do this? In the future I'd like to be able to possibly add additional forms and therefore don't necessarily want to have to have to modify the class every time I create a new form.
Below is the code I used: - Public Class Class1
-
Public Sub New(ByVal Form2)
-
trd = New Threading.Thread(AddressOf task)
-
trd.IsBackground = True
-
trd.Start()
-
Me.Form2 = Form2
-
End Sub
-
Private Form2 As Form2
-
Private trd As Threading.Thread
-
Delegate Sub SetText(ByVal label As Windows.Forms.Label, ByVal text As String)
-
Private Sub task()
-
For i As Integer = 0 To 5
-
Threading.Thread.Sleep(5000)
-
Dim DelSetText As SetText = New SetText(AddressOf Form2.SetText)
-
Form2.Invoke(DelSetText, New Object(1) {Form2.Label1, i.ToString})
-
Next
-
End Sub
-
End Class
-
-
Public Class Form1
-
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
Form2.Show()
-
Form2.BringToFront()
-
signal = New Class1(Form2)
-
End Sub
-
Private signal As Class1
-
-
End Class
-
-
Public Class Form2
-
Public Sub SetText(ByVal label As Windows.Forms.Label, ByVal item As String)
-
label.Text = item
-
End Sub
-
End Class
jjvainav, I couldn't get your method to work.
I raised the event in Class1 and handled it in Form1, but when I go to change the text in Form2 it creates a new instance of Form2. - Public Class Form1
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
Form2.Show()
-
Form2.BringToFront()
-
signal = New Class1()
-
End Sub
-
Private WithEvents signal As Class1
-
Public Sub SetForm2Text(ByVal text As String) Handles signal.SetText
-
Form2.Label1.Text = text
-
End Sub
-
End Class
-
-
Public Class Class1
-
Public Sub New()
-
trd = New Threading.Thread(AddressOf task)
-
trd.IsBackground = True
-
trd.Start()
-
End Sub
-
Public Event SetText(ByVal text As String)
-
Private trd As Threading.Thread
-
Private Sub task()
-
For i As Integer = 0 To 5
-
Threading.Thread.Sleep(5000)
-
RaiseEvent SetText(i.ToString)
-
Next
-
End Sub
-
End Class
Try this: -
Public Class Form1
-
Private Delegate Sub SetTextCallback(ByVal text As String)
-
-
Private form2 As Form2
-
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
form2 = New Form2
-
form2.Show()
-
form2.BringToFront()
-
signal = New Class1()
-
End Sub
-
Private WithEvents signal As Class1
-
Public Sub SetForm2Text(ByVal text As String) Handles signal.SetText
-
If Me.InvokeRequired Then
-
Me.Invoke(New SetTextCallback(AddressOf SetForm2Text), text)
-
End If
-
-
form2.Label1.Text = text
-
End Sub
-
End Class
-
-
Public Class Class1
-
Public Sub New()
-
trd = New Threading.Thread(AddressOf task)
-
trd.IsBackground = True
-
trd.Start()
-
End Sub
-
Public Event SetText(ByVal text As String)
-
Private trd As Threading.Thread
-
Private Sub task()
-
For i As Integer = 0 To 5
-
Threading.Thread.Sleep(1000)
-
RaiseEvent SetText(i.ToString)
-
Next
-
End Sub
-
End Class
-
jjvainav, I couldn't get your method to work.
I raised the event in Class1 and handled it in Form1, but when I go to change the text in Form2 it creates a new instance of Form2. - Public Class Form1
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
Form2.Show()
-
Form2.BringToFront()
-
signal = New Class1()
-
End Sub
-
Private WithEvents signal As Class1
-
Public Sub SetForm2Text(ByVal text As String) Handles signal.SetText
-
Form2.Label1.Text = text
-
End Sub
-
End Class
-
-
Public Class Class1
-
Public Sub New()
-
trd = New Threading.Thread(AddressOf task)
-
trd.IsBackground = True
-
trd.Start()
-
End Sub
-
Public Event SetText(ByVal text As String)
-
Private trd As Threading.Thread
-
Private Sub task()
-
For i As Integer = 0 To 5
-
Threading.Thread.Sleep(5000)
-
RaiseEvent SetText(i.ToString)
-
Next
-
End Sub
-
End Class
All forms that you create inheri the base class Form.
If you can identify which forms will be sent (like you send Form1) to Class1
Also identify the methods that these forms will have in common,
Put them all into an interface, and pass the instance of the Form as the interface type you just created.
This way, you will only need to pull the interface type, and call the method, which will do the trick for you
Sign in to post your reply or Sign up for a free account.
Similar topics
by: bryhhh |
last post by:
I have am still learning C#, so please bear with me.
I have an application that needs to open a second form, but I need the
second form to process message loops, whilst the original form also...
|
by: Brian Keating EI9FXB |
last post by:
Hello again,
I've already placed a few posts on this topic.
This time i've a simple application that exhibits my problem,
I've placed sample solution 8k on my website should anyone be interested...
|
by: Ioannis Vranos |
last post by:
Why in this code the form *does not refresh* when it gets the focus/after some time?
#using <mscorlib.dll>
#using <system.windows.forms.dll>
#using <system.dll>
#using <system.drawing.dll>
|
by: Jervin Justin |
last post by:
Hi,
I've been having this problem for some time with Web Forms:
I have a web app that sends data to a service when the user presses a
button. Based on the data, the server will send several...
|
by: Nick |
last post by:
Hi there,
I'm creating a form using the NativeWindow class to process some custom
messages. Unfortunately as it's created on the main thread of the
application it doesnt recieve messages while...
|
by: morangolds |
last post by:
Hi,
I've been having a problem with C++ Windows Forms apps not "ending"
when you close the form window. I've searched about this problem all
over the place and most searches have lead me to...
|
by: titan nyquist |
last post by:
I have a class with data and methods that use it. Everything is
contained perfectly
THE PROBLEM: A separate thread has to call a method in the current
instantiation of this class. There is...
|
by: Lilith |
last post by:
This was easy in C++ (well, relatively.) Now I'm in the C#
environment and everything I try doesn't work.
I'm running a thread in which I have two accesses I need to components
on a form. In...
|
by: Franck |
last post by:
I have a form with a loading bar as marquee so i don't need to play
with the progress thing. It's just a form to show activity in other
way.
So what i need to do is start it at the beginning of...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |