Results 1 to 24 of 24

Thread: Checkbox Checker!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Checkbox Checker!

    Hi All,

    I was wondering if i would be able to create a checkbox checker usign VB!

    All I want is a small form that has one button and when you click the button it will check all the chekboxes on the webpage that I am viewing?

    Is this possible?!

    Thanks guys,
    Chloe ~X~

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Checkbox Checker!

    I think the best way to accomplish this would be using javascript, since there is nothing meaningful to be done at server.

    Anyways you can do from VB (code behind) too:
    vb.net Code:
    1. Sub CheckAll(ByVal checked As Boolean)
    2.     For Each cont As Control In Me.Controls
    3.         If TypeOf cont Is CheckBox Then
    4.             CType(cont, CheckBox).Checked = checked
    5.         End If
    6.     Next
    7. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi Nick,

    Thanks for explaining this better for me! Thats exactly what i mean.

    What about if i include a web browser within my form and then the button that checks the checkboxes only has to check the boxes in the web page that is opened within my windows form?

    Can this be done?

    Oh by the way i'm a Girlly! :P

    Thanks,
    Chloe~X~

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi Guys,

    Here's the code that worked for me:

    Code:
    For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
                If input.GetAttribute("type") = "checkbox" Then
                    input.SetAttribute("checked", "true")
                End If
            Next
    Thanks,
    Chloe~X~

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Checkbox Checker!

    It's easy. Though I might not be able to give you exact code at present, here are the steps to follow which are very easy. There are many code samples on this forum on how to get the document object and traverse the DOM tree.

    1. Add a webbrowser control to your form.
    2. Load the webpage into the webbrowser control.
    3. Get a reference to the Document object of the loaded webpage.
    4. Traverse the DOM tree to find all checkboxes and tick them.

    EDIT: Aaahh!! I was looking at the cached page and didn't see the last post. Glad you resolved it yourself

    Pradeep
    Last edited by Pradeep1210; Dec 18th, 2008 at 02:21 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi Every1,

    I am using the below code to check all the checkboxes on a website that is laoded within the webbrowser control in my form:

    Code:
    For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
                If input.GetAttribute("type") = "checkbox" Then
                    input.SetAttribute("checked", "true")
                End If
            Next
    Does any1 know how i can amend this so that when i click the check button for a second time it will uncheck all the records? So basically if the button is clicked it will do the opposite to what the checkbox status currently is? i.e if it's checked then clicking the button will uncheck it, if it is unchecked then clicking the button will check it?

    Thanks,
    Chloe ~X~

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Checkbox Checker!

    Maybe something like this:
    vb.net Code:
    1. Private Sub CheckUncheckAll(ByVal newState As String)
    2.         For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
    3.             If input.GetAttribute("type") = "checkbox" Then
    4.                 input.SetAttribute("checked", newState)
    5.             End If
    6.         Next
    7.     End Sub
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         If Button1.Text = "Check All" Then
    11.             CheckUncheckAll("true")
    12.             Button1.Text = "Uncheck All"
    13.         Else
    14.             CheckUncheckAll("false")
    15.             Button1.Text = "Check All"
    16.         End If
    17.     End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Checkbox Checker!

    Maybe I misunderstood but from what I understand she wants the inverse of property to be set.

    So if she has 3 checkboxes where their state is: true, true, false it switches to false, false, true. Is this correct?

    This is untested as I don't deal with webrowsers, but I assume you can do this:

    vb.net Code:
    1. Private Sub InverseChecked()
    2.         For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
    3.             If input.GetAttribute("type") = "checkbox" Then
    4.                 If input.GetAttribute("checked") = "true" Then
    5.                     input.SetAttribute("checked", "false")
    6.                 Else
    7.                     input.SetAttribute("checked", "true")
    8.                 End If
    9.             End If
    10.         Next
    11.     End Sub

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi Guys,

    Thank you for your responses but i couldn't get either code to work for me?

    ForumAccount is correct, if the checkbox is checked, then clicking the Check button will uncheck the checkbox and if the checkbox is not checked, clicking the Check button will check the checkbox?

    This is bugging me now!
    Thanks
    Chloe ~X~

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hey guys,

    I just viewed the source of the webpage with the checkboxes on and ticked a few checkboxes and left some unchecked and there is no difference in the Input tags? Do you think this could be why this isn't working?

    Thanks,
    Chloe ~X~

  12. #12
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: Checkbox Checker!

    you are using javascript version or doing this stuff on server ??

    if you are doing this at server side you have to call your function which check or uncheck checkboxes in prerender event of page's life cycle
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi Su ki,

    I have a web browser control in my windows form.

    Could you please explain a little further about the prerender event of the page's life cycle? I dont know what this means?

    Thanks,
    Chloe ~X~

  14. #14
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Checkbox Checker!

    What exactly is not working with code in post #8/#9 ?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi Pradeep,

    With post #8 when i clicked the button it did not check the checkboxes.

    With post #9 when i clicked the button it checked the check boxes, but when i clicked it again to uncheck the checkboxes it did not work.

    Thanks,
    Chloe ~X~

  16. #16
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Checkbox Checker!

    For Pradeep's code, did you make sure you changed the text of the button to "Check All"? (Even though it shouldn't matter after 2 clicks.) Did you debug? Is it calling his function?

    For mine, the code is supposed to make the check state the opposite of what it is. Have you debugged through and seen what is happening on the If statement?

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi,

    I just tried Pradeeps code again and it checked all the boxes and the lbel change name correctly but it still didn't uncheck the checkboxes when it was click a second time?

    How do i debug through it? It must have called the function to check the checkboxes?

    Thanks again,
    Chloe ~X~

  18. #18
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Checkbox Checker!

    Place a breakpoint on the line you want code execution to stop on with F9. The code will run up until that red line, then you need to press F8 to step through each line. There's good debugging tutorials online that show you how to add watches, writing to the immediate window etc...

    Edit: do you have a sample url I can run tests on as well?

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi,

    Thanks i'll try that now.

    However, i just tried adding a serparate button uncheck the checkboxes using the below code, but when i click the button it checks the checkboxes instead of unchecking them!!

    Code:
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
                If input.GetAttribute("type") = "checkbox" Then
                    input.SetAttribute("checked", "false")
                End If
            Next
        End Sub
    Why is this?

    Thank you,
    Chloe ~X~

  20. #20
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Checkbox Checker!

    Quote Originally Posted by ChloeJacobs View Post
    Hi,

    I just tried Pradeeps code again and it checked all the boxes and the lbel change name correctly but it still didn't uncheck the checkboxes when it was click a second time?

    How do i debug through it? It must have called the function to check the checkboxes?

    Thanks again,
    Chloe ~X~
    Your program doesn't know how many times a button has been clicked automatically unless you have code to keep track of the clicks. In your case, this is very simple. You can use the Tag property of the button to store the information you need. For example, you initially store a boolean value of True to the button's tag. When the user clicks it, you set the checkboxes value to the value of the button's tag, and then change the button's tag to the opposite of what it currently is.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi Stanav,

    That sounds a bit to complicated for me! Could you please show me an example of how to do that?

    I just tried debugging this code:

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
                If input.GetAttribute("type") = "checkbox" Then
                    input.SetAttribute("checked", "false")
                End If
            Next
        End Sub
    And it skips over this line of code:

    Code:
    input.SetAttribute("checked", "false")
    Should it be skipping this line?

    thanks,
    Chloe ~X~

  22. #22
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Checkbox Checker!

    Try something like this
    Code:
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim btnTag As Object = Me.Button2.Tag
            Dim value As Boolean = True
            If btnTag IsNot Nothing Then
                value = CBool(btnTag)
            End If
            Me.SetAllCheckBoxes(value)
            Me.Button2.Tag = Not value
        End Sub
    
        Private Sub SetAllCheckBoxes(ByVal value As Boolean)
            For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
                If input.GetAttribute("type") = "checkbox" Then
                    input.SetAttribute("checked", value.ToString.ToLower())
                End If
            Next
        End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: Checkbox Checker!

    Hi stanav,

    Thank you for posting this code, but for some reason the checkboxes are still not unchecking?

    Do you think it is down to the website and not the code?

    Thanks,
    Chloe ~X~

  24. #24
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Checkbox Checker!

    Can you provide a sample file on which you are running the code?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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