Results 1 to 10 of 10

Thread: [RESOLVED] CheckBoxes and Backgroundworker

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Resolved [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:
    1. Private Sub ResetTextBoxText()
    2.     If Me.TextBox1.InvokeRequired Then
    3.         Me.TextBox1.Invoke(New MethodInvoker(AddressOf ResetTextBoxText))
    4.     Else
    5.         Me.TextBox1.ResetText()
    6.     End If
    7. 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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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:
    1. Public Sub CheckBoxChecked()
    2.         If Me.CheckBox1.InvokeRequired Then
    3.             Me.CheckBox1.Invoke(New MethodInvoker(AddressOf CheckBoxChecked))
    4.         Else
    5.             If CheckBox1.Checked = True Then
    6.                 MessageBox.Show("Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    7.             Else
    8.                 MessageBox.Show("Failure!", "Failure!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    9.             End If
    10.         End If
    11.     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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: CheckBoxes and Backgroundworker

    Yep that should work fine
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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
    Attached Files Attached Files
    Last edited by weirddemon; Sep 16th, 2009 at 05:46 PM.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: CheckBoxes and Backgroundworker

    I've uploaded my demo project for you to take a look at if you don't mind.
    Where? I dont see anything
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: CheckBoxes and Backgroundworker

    I forgot to upload it
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. 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)
    Last edited by chris128; Sep 16th, 2009 at 05:55 PM. Reason: "because" isnt two separate words apparently :D
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: CheckBoxes and Backgroundworker

    Gotcha.

    Thanks for the help
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: CheckBoxes and Backgroundworker

    No problem, dont forget to mark the thread as resolved
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width