|
-
Dec 17th, 2008, 05:59 AM
#1
Thread Starter
Hyperactive Member
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~
-
Dec 17th, 2008, 06:14 AM
#2
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:
Sub CheckAll(ByVal checked As Boolean) For Each cont As Control In Me.Controls If TypeOf cont Is CheckBox Then CType(cont, CheckBox).Checked = checked End If Next End Sub
Pradeep
-
Dec 17th, 2008, 06:44 AM
#3
Re: Checkbox Checker!
I think what he means is that he wants to create a program that checks every checkbox on a webpage that is on a separate browser (firefox, IE) outside of his control... No?
-
Dec 17th, 2008, 09:32 AM
#4
Thread Starter
Hyperactive Member
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~
-
Dec 17th, 2008, 10:41 AM
#5
Thread Starter
Hyperactive Member
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~
-
Dec 18th, 2008, 02:16 AM
#6
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.
-
May 12th, 2009, 10:32 AM
#7
Thread Starter
Hyperactive Member
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~
-
May 12th, 2009, 11:35 AM
#8
Re: Checkbox Checker!
Maybe something like this:
vb.net Code:
Private Sub CheckUncheckAll(ByVal newState As String) For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input") If input.GetAttribute("type") = "checkbox" Then input.SetAttribute("checked", newState) End If Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Button1.Text = "Check All" Then CheckUncheckAll("true") Button1.Text = "Uncheck All" Else CheckUncheckAll("false") Button1.Text = "Check All" End If End Sub
-
May 12th, 2009, 12:29 PM
#9
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:
Private Sub InverseChecked() For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input") If input.GetAttribute("type") = "checkbox" Then If input.GetAttribute("checked") = "true" Then input.SetAttribute("checked", "false") Else input.SetAttribute("checked", "true") End If End If Next End Sub
-
May 13th, 2009, 03:01 AM
#10
Thread Starter
Hyperactive Member
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~
-
May 13th, 2009, 03:11 AM
#11
Thread Starter
Hyperactive Member
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~
-
May 13th, 2009, 03:32 AM
#12
Hyperactive Member
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
-
May 13th, 2009, 09:39 AM
#13
Thread Starter
Hyperactive Member
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~
-
May 13th, 2009, 09:46 AM
#14
Re: Checkbox Checker!
What exactly is not working with code in post #8/#9 ?
-
May 13th, 2009, 10:03 AM
#15
Thread Starter
Hyperactive Member
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~
-
May 13th, 2009, 10:08 AM
#16
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?
-
May 13th, 2009, 10:21 AM
#17
Thread Starter
Hyperactive Member
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~
-
May 13th, 2009, 10:31 AM
#18
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?
-
May 13th, 2009, 10:33 AM
#19
Thread Starter
Hyperactive Member
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~
-
May 13th, 2009, 10:39 AM
#20
Re: Checkbox Checker!
 Originally Posted by ChloeJacobs
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 -
-
May 13th, 2009, 10:47 AM
#21
Thread Starter
Hyperactive Member
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~
-
May 13th, 2009, 10:58 AM
#22
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 -
-
May 18th, 2009, 08:06 AM
#23
Thread Starter
Hyperactive Member
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~
-
May 18th, 2009, 08:26 AM
#24
Re: Checkbox Checker!
Can you provide a sample file on which you are running the code?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|