Question : Read and Change Control Value from Thread

Im new to VB.NET and wish to use threading for one of the assignments. I do not know how delegate and invoke works so this actually is a very simple question for someone who knows anything about threading.

I want to read a text box value from a thread and then change this on the Windows Form.

Thats All.

Answer : Read and Change Control Value from Thread

Interesting...it doesn't complain at all about reading the TextBox from a thread other than it was created on!  =\

If you don't have anything crazy like MANY threads attempting to read/change the SAME control at the same time then you can turn OFF "cross thread checking" and do it like this:  (No Invokes/Delegate)

Invokes/Delegates do have their place!...what you're doing really dictates whether you should be using them or not...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim T As New Threading.Thread(AddressOf Worker)
        T.Start()
    End Sub
 
    Private Sub Worker()
        Dim curTxt As String
        Dim cbText As String
        Dim newTxt As String
        For i As Integer = 1 To 5
            curTxt = TextBox1.Text()
            cbText = ComboBox1.SelectedItem
            newTxt = curTxt & i.ToString & " "
            TextBox1.Text = newTxt
            System.Threading.Thread.Sleep(1000)
        Next
    End Sub
 
End Class
Random Solutions  
 
programming4us programming4us