[RESOLVED] CheckBoxes and Backgroundworker
I am trying to access the checked state of a CheckBox from a BackGroundWorker thread.
I received some help by utilizing properties and it seemed to work just fine. However, I'm using 10-15 CheckBoxes and I think using the InvokeRequired property would probably be best.
I've looked over JMC's thread that talks about how to access controls from the worker thread a bunch of different times and I can't seem to grasp the concepts.
When it comes to the InvokeRequired property, I suppose I understand why it is needed. But I'm not sure if I understand it completely. From the example that JMC provides:
vb.net Code:
Private Sub ResetTextBoxText()
If Me.TextBox1.InvokeRequired Then
Me.TextBox1.Invoke(New MethodInvoker(AddressOf ResetTextBoxText))
Else
Me.TextBox1.ResetText()
End If
End Sub
If I'm not mistaken, this tells us that if invocation is required to access the control, then we access via the MethodInvoker if not, then we just access to directly?
So, if I need to check the checked state of a check box, I know I can just put a conditional statement after the Else line if I can access it directly. But, if I can't how would I use MethodInvoker to check the checked state?
And then, when calling it from the worker thread, would I just call the sub?
Thanks for any information you all can provide.
Re: CheckBoxes and Backgroundworker
You dont need to do anything at all because the MethodInvoker is invoking that same method you are already looking at, except this time when it runs the InvokeRequired will be false so it will go straight to your Else part :)
Re: CheckBoxes and Backgroundworker
So if I have three forms, the CheckBox is on Form2 and the BackGroundWorker is on Form3, I would set the code in Form2, and then call the from the Worker thread in Form3?
Also, this is all I would need for my code?
vb Code:
Public Sub CheckBoxChecked()
If Me.CheckBox1.InvokeRequired Then
Me.CheckBox1.Invoke(New MethodInvoker(AddressOf CheckBoxChecked))
Else
If CheckBox1.Checked = True Then
MessageBox.Show("Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
MessageBox.Show("Failure!", "Failure!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End If
End Sub
I'm asking both questions because I tried it how I explained and I've come up with nothing. I'm going to keep playing around with it though.
Re: CheckBoxes and Backgroundworker
Yep that should work fine :)
1 Attachment(s)
Re: CheckBoxes and Backgroundworker
When I run the code, it works... kind of. It shows the MessageBox, which is good, but doesn't show the "Success" message when the CheckBox is checked. Which means that it can't determine the checked state of the box as "True."
This doesn't make a lot of sense to me. The sub is obviously working, because it always shows the false message.
I've uploaded my demo project for you to take a look at if you don't mind.
Thanks
Re: CheckBoxes and Backgroundworker
Quote:
I've uploaded my demo project for you to take a look at if you don't mind.
Where? I dont see anything
Re: CheckBoxes and Backgroundworker
Re: CheckBoxes and Backgroundworker
Its not working the way you want it to because you are declaring your own instances of each form when you show them but then in your background worker code you are referring to the default instance of Form2. As most people on here will tell you, its a good idea to just avoid using the default instances at all - if you want to find an open form that is the Form2 class you can just do something like this:
vb Code:
DirectCast(Application.OpenForms("Form2"), Form2).CheckBoxChecked()
You need to add some error handling to that so that it works if Form2 cannot be found etc but you get the idea. If you want a more robust way of doing it then the way I do it is to loop through each form in that OpenForms collection and test the Type against the type of the form, rather than just relying on the Name property (which is what that example above does)
Re: CheckBoxes and Backgroundworker
Gotcha.
Thanks for the help :D
Re: CheckBoxes and Backgroundworker
No problem, dont forget to mark the thread as resolved :)