How to set a checkbox to "checked" using mshtml? - c#

I can do most things I need to with mshtml, but I'm a bit stuck with how to set a checkbox input element to "checked". Here's the situation...
IHTMLElementCollection inputElements = (IHTMLElementCollection)doc.all.tags("input");
foreach (IHTMLElement el in inputElements)
{
string elementHtml = el.outerHTML;
string termsOfServiceIdentifier = "id=chkUTOS_ver2";
// select the Terms of Service checkbox
if (elementHtml.Contains(termsOfServiceIdentifier))
{
HTMLInputElement chkTOS = (HTMLInputElement)el;
chkTOS.#checked = true; // that's the solution. Thanks Wayne.
}
else
{
// do nothing - we're not interested in this element
}
}
Thanks in advance for any help!
Gregg

HTMLInputElement exposes the Checked property as a Boolean

In plain JavaScript, checkbox elements have a checked property. So [in plain JavaScript] you might write:
document.getElementById("myCheckbox").checked = true;
I don't know .NET or whatever you're using there, but they may do it in a similar way.
Steve

Related

Uncheck all checkBoxes with C#

I am trying to write a code which will uncheck all checkBoxes in my form when a button is clicked. I know that I could do
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
and so on, but I have about 35 checkBoxes, so I was hoping to use a simpler code. I keep finding stuff online that looks like this;
foreach (Control cBox in this.Controls)
{
if (cBox is CheckBox)
{
((CheckBox)cBox).Checked = false;
}
}
And I was hoping to use something like that, instead of writing checkBox1.Checked = false; about 70 times (because I have 2 buttons, both of which need to do various things and then uncheck all the boxes.
I saw another solution that involved a Stack Panel or some type of code that looked more like Javascript or HTML than C#, and also seemed to involved writing out each checkBox.Checked status as its own line, which doesn't save me any lines of code.
Any tips would be appreciated. Thanks all :)
Answering my own question with some info from my new pal #HandbagCrab:
foreach (Control cBox in tabPage1.Controls)
{
if (cBox is CheckBox)
{
((CheckBox)cBox).Checked = false;
}
}
Adding the tabPage1 fixed my issue. Before, I had been using this.Controls which limited access to controls only within that dependency. I was still able to control other things based on the checkBoxes by naming them individually like checkBox1.Checked = false;, but this was only because I called to them by name, rather than asking the code to look through all Controls.
You can use C# 7.0+ pattern matching:
foreach(Control control in controls)
{
if (control is CheckBox chk) chk.Checked = false;
}

programmatically check checkbox asp.net

Im trying to loop through a collection and create checkboxes for each item in the collection. I check if a bool is true and want that checkbox to be checked. But I cant seem to get this to work. What am I missing here?
var checkboxList = new CheckBoxList();
foreach (var answer in question.Answers)
{
if (answer.CorrectAnswer)
{
var cbItemOrd = new CheckBox
{
Checked = true,
Text = answer.AnswerText + " Correct Answer"
};
checkboxList.Items.Add( cbItemOrd.Text );
cbItemOrd.Checked = true;
}
else
{
checkboxList.Items.Add(answer.AnswerText);
}
}
div.Controls.Add(checkboxList);
I get the "correct answer" text out on the correct field so the loop works. If I need to use "FindControl" or something, how do I do that with programmatically created objects?
Thanks alot!

Remove empty space when visible property is set to false using JavaScript

I'm having a DropdownList and when its Selected Value is changed (for ex: 0 ) I need to set the visible property of a Panel to True and the visible property of another Panel to False.
and when another Value is selected I need to do Vice Versa Using JAVASCRIPT.
I'm currently achieving this but the space remains as it is. How can i remove the spaces also.
can anyone help me??
I'm attaching the code also.
function visible(val) {
var ddl = document.getElementById("ddl_IDProof");
var selectedFilterType = drpFilterType.options[ddl.selectedIndex].value;
if (selectedFilterType == "0") {
document.getElementById("pnl1").style.visibility = "visible";
document.getElementById("pnl2").style.visibility = "hidden";
}
else {
document.getElementById("pnl1").style.visibility = "hidden";
document.getElementById("pnl2").style.visibility = "visible";
}
}
Use display instead of visibility.
This will hide the entire element:
// Show pnl1 (maybe you have to use inline or inline-block insdead of block)
document.getElementById("pnl1").style.display = "block";
// Hide pnl2
document.getElementById("pnl2").style.display = "none";

GetElementById without id and name HOW?

I need to click the button "Add" in the post new wordpress form, this button is to add tags to the post , the trouble is that button don't have the value and id propertie. Is just like that
the html for the button
input type="button" class="button tagadd" value="Add" tabindex="3"
my tries
webBrowser1.Document.GetElementById("button tagadd").InvokeMember("click");
webBrowser1.Document.GetElementById("Add").InvokeMember("click");
"GetElementById without id"
:-)
Unless you can change the markup for the button
What you need now is to traverse the entire DOM and look for a button in a known place. I'd suggest adding jquery if not already exist to be able for easier dom manipulation/search.
If you add jquery you could do something like $(".tagadd").click()
You could try doing
webBrowser1.document.getElementsByClassName("tagadd")
EDIT: Here is a script to create the getElementsByClassName function if it's not available http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
There is also this http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx but I've never used it.
Add an ID. Even if you're dynamically generating the buttons this should be trivial.
If you're using jQuery,
$('.tagadd')
will return a collection of everything with the tagadd class applied. You can further filter this by the other classes (button, etc)
Use this:
onload=function(){
if (document.getElementsByClassName == undefined) {
document.getElementsByClassName = function(className)
{
var hasClassName = new RegExp("(?:^|\s)" + className + "(?:$|\s)");
var allElements = document.getElementsByTagName("*");
var results = [];
var element;
for (var i = 0; (element = allElements[i]) != null; i++) {
var elementClass = element.className;
if (elementClass
&& elementClass.indexOf(className) != -1
&& hasClassName.test(elementClass))
results.push(element);
}
return results;
}
}
}
and another
Some browsers provide the method getElementsByClassName() which lets you select by class without using jQuery (which is a bit heavy if this is all you need). I haven't tested this so I'm not sure how widely it's supported.
Did I mention that you should give everything an ID?
use TagName isteed for example
var elems = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
if (elem.GetAttribute("class") == "button tagadd")
{
elem.InvokeMember("click");
}
}

How to retrieve the scrollbar position of the webbrowser control in .NET

I tried using webBrowser1.Document.Body.ScrollTop and webBrowser1.Document.Body.ScrollLeft, but they don't work. They always return 0 and I can't access webBrowser1.Document.documentElement.ScrollTop and .ScrollLeft.
OK, I solved it:
Dim htmlDoc As HtmlDocument = wb.Document
Dim scrollTop As Integer = htmlDoc.GetElementsByTagName("HTML")(0).ScrollTop
To actually scroll, we found that the ScrollIntoView method worked nicely. For example, to scroll to the top-left of the page.
this.webBrowser.Document.Body.FirstChild.ScrollIntoView(true);
However, we were not successful in actually getting the scroll position (that said, we didn't spend long trying). If you are in control of the HTML content, you might consider using some javascript to copy the scroll position into a hidden element and then read that value out using the DOM.
ScrollTop and ScrollLeft merely allow an offset to be provided between the boundary of an element and its content. There appears to be no way to manipulate the scroll by those values. Instead, you have to use ScrollIntoView.
For anyone interested, here's the C# code equivalent to Marc's answer:
System.Windows.Forms.HtmlDocument htmlDoc = webBrowser.Document;
if (htmlDoc != null)
{
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
int scrollLeft = htmlDoc.GetElementsByTagName("HTML")[0].ScrollLeft;
}
I was able to query the scroll position using this
if (this.webBrowser.Document != null)
{
int scrollPosition = this webBrowser.Document.Body.ScrollTop;
}
You can check documentElement
IHTMLElement2 page =
(wb.Document.DomDocument as IHTMLDocument3).documentElement as IHTMLElement2;
int pos = page.scrollTop;
Accepted answer is VB. For C# WPF WebBrowser, I had to write the following. No idea if I really need all those casts or not. If one can get rid of any of those casts, that would be terrific.
using mshtml;
int? GetScrollTop(System.Windows.Controls.WebBrowser browser)
{
object doc = browser.Document;
HTMLDocument castDoc = doc as HTMLDocument;
IHTMLElementCollection elements = castDoc?.getElementsByTagName("HTML");
IEnumerator enumerator = elements?.GetEnumerator();
enumerator?.MoveNext();
var first = enumerator?.Current;
IHTMLElement2 castFirst = first as IHTMLElement2;
int? top = castFirst?.scrollTop;
return top;
}
I found kurt's answer almost worked but had to change the array reference as follows:
var document = (HTMLDocument)Browser.Document;
var scrollTop = (int)document.getElementsByTagName("HTML").item(0).ScrollTop;

Categories

Resources