Author Topic: [Tutorial]C# - Foreach Statements  (Read 1497 times)

Offline JokeBOx

  • Advanced Member
  • *
  • Topic Author
  • Posts: 206
  • Karma: +8/-27
  • HELLO!!!!!!!!!!!!!!!
  • Awards Winner of 1 Photoshop Challenge Contest [COMMON]
    • View Profile
    • Awards
[Tutorial]C# - Foreach Statements
« on: February 02, 2012, 19:06 »
I'm going to teach you to input or click something (i.e textbox or button) using Foreach statements.
Foreach statements are also good for input or clicking something without a id or name.

This is webBrowser only.

Ok, if the html source of a textbox is:

Code: [Select]
<input id="email" class="textbox_email" type="text" value="" name="email">
ok so our id is email and our name is email.

We are going to use id for now, you can use name if you want since there the same


These are attributes of our Html sources.
We need theses.

Theres also type, class, and value, but we don't thoses (for now)

now here is what we are going to do:

Code: [Select]
foreach (HtmlElement umad in webBrowser1.Document.All)
{

}


ok so we are saying for each umad (you can say whatever but I like saying umad ) as HtmlElement (the element/attribute source) in the webpage, do this in between the brackets.

Now we are going to do this:

Code: [Select]
foreach (HtmlElement umad in webBrowser1.Document.All)
{
if (umad.GetAttribute("id") == "email")
{

}

}

Ok, sounds fimilar amirite?
*cough*webBrowser1.Document.GetElementById("email");*cough*

Ok now we are going to set the value to what ever you or anyone inputs.

Code: [Select]
foreach (HtmlElement umad in webBrowser1.Document.All)
{
if (umad.GetAttribute("id") == "email")
{
umad.SetAttribute("value", txtEmail.Text);
}

}


Now if you want to click a button.

Assuming this is the html source

Code: [Select]
<input id="submit" class="form_buttonl" type="button" value="" name="submit">

You would just:

Code: [Select]
foreach (HtmlElement umad in webBrowser1.Document.All)
{
if (umad.GetAttribute("id") == "email")
{
umad.InvokeMember("click");
}

}

sounds fimilar amirite?
*cough*webBrowser1.Document.GetElementById("submit").Invo keMember("click");*cough*