Thread Question

SIMIN

Regular
Joined
Mar 10, 2008
Messages
92
Hello,
From inside a thread I wanna enable/disable a text box on my form, that's so easy to do, but my problem is that I have to enable it only if a radio button is checked, and if not checked I should not enable it.
Here is my code:
Visual Basic:
Delegate Sub SetEnabledCallback(ByVal Enabled As Boolean)
Private Sub SetEnabled(ByVal Enabled As Boolean)
    If Me.RadioButton1.InvokeRequired Then
        Dim d As New SetEnabledCallback(AddressOf SetEnabled)
        Me.Invoke(d, New Object() {Enabled})
    Else
        Me.TextBox1.Enabled = Me.RadioButton1.Checked
    End If
End Sub
From within my thread I use Me.SetEnable(True/False) but it won't work at one condition.
Please help me:(
 
From what I understand, you're trying to enable/disable a textbox, based on the condition of whether or not a radio button is selected.

You shouldn't be doing anything with RadioButton1 to get this, you don't even really need to base your thread off of RadioButton1 if you can work around it.

Here's a quick project I set up with alterations to your code:

Visual Basic:
Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Timer1.Enabled = True
	End Sub

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Dim t As New Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf SetEnabled))
		t.Start(RadioButton1.Checked)
	End Sub
	Delegate Sub SetEnabledCallback(ByVal Enabled As Object)
	Private Sub SetEnabled(ByVal Enabled As Object)
		If Me.TextBox1.InvokeRequired Then
			Dim d As New SetEnabledCallback(AddressOf SetEnabled)
			Me.Invoke(d, New Object() {Enabled})
		Else
			Me.TextBox1.Enabled = Enabled
		End If
	End Sub
End Class

As you can see, you should check the invocation of TextBox1 and send RadioButton1's value as the parameter. Now, if you didn't even want to send in the parameter, you could still access RadioButton1.Checked from the thread without any problems. You can read a value from a non-threaded object, but you can't assign a value to it. Hence why you can't enable/disable a textbox from a thread, but you could read what value is in it.

I've also noticed that when dealing with parameterized threads, it doesn't like you to tell it what kind of object you're using in your parameter, just that it's Object. Maybe I'm doing something incorrectly, but this is the way I do it. You can also verify that the object is of type Boolean if you decide later on to redistribute it for other developers.

Hope this helps!

~Derek
 
I don't like to get a timer involved for such code, I think there's a way without timer, directly to do this.
 
I don't like to get a timer involved for such code, I think there's a way without timer, directly to do this.

There definitely is. I didn't know how you were trying to implement your code. I was just showing how I implemented it. Don't focus on that; I was just showing how I got the parameter in there.

~Derek
 
The thread would need to check if the form has InvokeRequired = true not the radio button itself.

Could you not just set the status of the textbox from the radio button's click event rather than rely on the thread doing this though?
 
Back
Top