<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>VBForums - CodeBank - C#</title>
		<link>http://www.vbforums.com/</link>
		<description>Find cool or practical code examples using C#.</description>
		<language>en</language>
		<lastBuildDate>Sat, 25 May 2013 19:39:02 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://www.vbforums.com/images/misc/rss.png</url>
			<title>VBForums - CodeBank - C#</title>
			<link>http://www.vbforums.com/</link>
		</image>
		<item>
			<title>Match Two Game</title>
			<link>http://www.vbforums.com/showthread.php?722575-Match-Two-Game&amp;goto=newpost</link>
			<pubDate>Thu, 23 May 2013 05:54:58 GMT</pubDate>
			<description><![CDATA[I saw someone asking about this the other day so I thought that I'd throw a demo together.  It's a simple game that displays a 5x4 grid of cards containing 10 matching pairs.  The back of all cards is displayed initially and, after starting a game, the user clicks on a pair to reveal them.  If the pair matches then both cards are removed and if they don't match then they are turned over again.  The application times the game and displays the running time as well as the total time when the game is completed.  The game can also be paused and restarted.

I'm considering adding some more features to the game, e.g. a list of best times and the ability to load an arbitrary number of image files from an arbitrary folder.  We'll see if the mood strikes me.

The attached solution was created in VS 2010 and contains projects in both VB and C#.  If you're using VB Express 2010 or C# Express 2010 then only one of the projects will be loaded.  If you try to open the MainWindow form in the designer and it won't display, just close it and then open it in the code window first and you should be OK from there.]]></description>
			<content:encoded><![CDATA[<div>I saw someone asking about this the other day so I thought that I'd throw a demo together.  It's a simple game that displays a 5x4 grid of cards containing 10 matching pairs.  The back of all cards is displayed initially and, after starting a game, the user clicks on a pair to reveal them.  If the pair matches then both cards are removed and if they don't match then they are turned over again.  The application times the game and displays the running time as well as the total time when the game is completed.  The game can also be paused and restarted.<br />
<br />
I'm considering adding some more features to the game, e.g. a list of best times and the ability to load an arbitrary number of image files from an arbitrary folder.  We'll see if the mood strikes me.<br />
<br />
The attached solution was created in VS 2010 and contains projects in both VB and C#.  If you're using VB Express 2010 or C# Express 2010 then only one of the projects will be loaded.  If you try to open the MainWindow form in the designer and it won't display, just close it and then open it in the code window first and you should be OK from there.</div>


	<div style="padding:10px">

	

	

	

	
		<fieldset class="fieldset">
			<legend>Attached Files</legend>
			<ul>
			<li>
	<img class="inlineimg" src="http://www.vbforums.com/images/attach/zip.gif" alt="File Type: zip" />
	<a href="http://www.vbforums.com/attachment.php?attachmentid=100447&amp;d=1369288203">MatchTwoDemo.zip</a> 
(47.9 KB)
</li>
			</ul>
		</fieldset>
	

	</div>
]]></content:encoded>
			<category domain="http://www.vbforums.com/forumdisplay.php?44-CodeBank-C">CodeBank - C#</category>
			<dc:creator>jmcilhinney</dc:creator>
			<guid isPermaLink="true">http://www.vbforums.com/showthread.php?722575-Match-Two-Game</guid>
		</item>
		<item>
			<title>A More Random Random Class</title>
			<link>http://www.vbforums.com/showthread.php?722469-A-More-Random-Random-Class&amp;goto=newpost</link>
			<pubDate>Wed, 22 May 2013 08:42:54 GMT</pubDate>
			<description><![CDATA[VB version here (http://www.vbforums.com/showthread.php?722445-A-More-Random-Random-Class).

The .NET Framework includes the System.Random class, whose job it is to generate random numbers.  It does so using a sequence based on a seed value, which is based on the system time by default.  There are numerous possible seeds and predicting the exact time that a Random object will be created is not easy.  That, coupled with the fact that the distribution of numbers generated by the sequence satisfies various statistical requirements for randomness, means that the Random class is quite suitable for most of your random number needs.

The main issue with the Random class is that, given a particular seed, the sequence will always produce the same numbers.  That can actually be good for some testing scenarios but it does mean that the results could be manipulated.  Where very high levels of unpredictability are required, the Random class is not suitable.

I've seen a number of people use various methods to generate more random random numbers.  That's not really necessary though.  The .NET Framework also includes the System.Security.Cryptography.RNGCryptoServiceProvider class for generating random numbers.  It's output is sufficiently random for use in cryptography, so there's really no need for anything more random than that.

One issue with the RNGCryptoServiceProvider class, though, is that it populates byte arrays with random values.  That's not always completely convenient though, as the most common need in applications is a random int value within a specific range.  The Random class is nice and easy to use in that scenario, thanks to its Next method.  What if we could combine the ease of use of the Random class with the increased randomness of the RNGCryptoServiceProvider class?

As it happens, we can.  The Random class has a protected method named Sample that generates a double values in the range 0.0 <= x < 1.0.  The result of that method is then used in the other methods to generate int values in a range.  We can define our own class that inherits Random and overrides that Sample method to use RNGCryptoServiceProvider internally.  Here's one I prepared earlier:using System;
using System.Security.Cryptography;

/// <summary>
/// Represents a random number generator, a device that produces a sequence of numbers that meet cryptographic requirements for randomness.
/// </summary>
/// <remarks>
/// Seed values are meaningless because this class uses a cryptographic service provider internally rather than a pseudo-random sequence.
/// </remarks>
public class CryptoRandom : Random, IDisposable
{
    /// <summary>
    /// The internal random number generator.
    /// </summary>
    private readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

    /// <summary>
    /// Returns a random number between 0.0 and 1.0.
    /// </summary>
    /// <returns>
    /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0.
    /// </returns>
    /// <remarks>
    /// Uses a cryptographic service provider internally rather than a pseudo-random sequence.
    /// </remarks>
    protected override double Sample()
    {
        var data = new byte[8];
        ulong number;

        do
        {
            // Get 8 random bytes.
            rng.GetBytes(data);

            // Convert the bytes to an unsigned 64-bit number.
            number = BitConverter.ToUInt64(data, 0);
        }
        while (number == ulong.MaxValue); // The result must be less than 1.0

        // Divide the number by the largest possible unsigned 64-bit number to get a value in the range 0.0 <= N < 1.0.
        return number / ulong.MaxValue;
    }


#region IDisposable Support

    private bool disposedValue; // To detect redundant calls

    // IDisposable
    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposedValue)
        {
            if (disposing)
            {
                // Dispose the underlying cryptographic random number generator.
                rng.Dispose();
            }
        }

        this.disposedValue = true;
    }

    // This code added to correctly implement the disposable pattern.
    public void Dispose()
    {
        // Do not change this code.  Put cleanup code in Dispose(bool disposing) above.
        Dispose(true);
        GC.SuppressFinalize(this);
    }

#endregion
}You can create an instance of that class and use it in pretty much exactly the same way as you would a Random object.  The only difference is that you will need to call its Dispose method when you're done, in order to dispose the internal RNGCryptoServiceProvider object.  Sample usage:public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private readonly CryptoRandom rng = new CryptoRandom();

    private void button1_Click(object sender, EventArgs e)
    {
        // Generate 10 random numbers.
        var numbers = new int[10];

        for (int i = 0; i < numbers.Length; i++)
        {
            // Generate numbers in the range 0 <= n <= 1000
            numbers[i] = rng.Next(1001);
        }

        MessageBox.Show(string.Join(Environment.NewLine, numbers), "10 Random Numbers");
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        rng.Dispose();
    }
}]]></description>
			<content:encoded><![CDATA[<div>VB version <a rel="nofollow" href="http://www.vbforums.com/showthread.php?722445-A-More-Random-Random-Class" target="_blank">here</a>.<br />
<br />
The .NET Framework includes the System.Random class, whose job it is to generate random numbers.  It does so using a sequence based on a seed value, which is based on the system time by default.  There are numerous possible seeds and predicting the exact time that a Random object will be created is not easy.  That, coupled with the fact that the distribution of numbers generated by the sequence satisfies various statistical requirements for randomness, means that the Random class is quite suitable for most of your random number needs.<br />
<br />
The main issue with the Random class is that, given a particular seed, the sequence will always produce the same numbers.  That can actually be good for some testing scenarios but it does mean that the results could be manipulated.  Where very high levels of unpredictability are required, the Random class is not suitable.<br />
<br />
I've seen a number of people use various methods to generate more random random numbers.  That's not really necessary though.  The .NET Framework also includes the System.Security.Cryptography.RNGCryptoServiceProvider class for generating random numbers.  It's output is sufficiently random for use in cryptography, so there's really no need for anything more random than that.<br />
<br />
One issue with the RNGCryptoServiceProvider class, though, is that it populates byte arrays with random values.  That's not always completely convenient though, as the most common need in applications is a random int value within a specific range.  The Random class is nice and easy to use in that scenario, thanks to its Next method.  What if we could combine the ease of use of the Random class with the increased randomness of the RNGCryptoServiceProvider class?<br />
<br />
As it happens, we can.  The Random class has a protected method named Sample that generates a double values in the range 0.0 &lt;= x &lt; 1.0.  The result of that method is then used in the other methods to generate int values in a range.  We can define our own class that inherits Random and overrides that Sample method to use RNGCryptoServiceProvider internally.  Here's one I prepared earlier:<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">csharp Code:</div>
	<pre class="alt2" style="margin:0px; padding:px; border:1px inset; width:; max-height:372px;overflow:auto"><div dir="ltr" style="text-align:left;"><div class="csharp" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Security</span>.<span style="color: #0000FF;">Cryptography</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">/// Represents a random number generator, a device that produces a sequence of numbers that meet cryptographic requirements for randomness.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">/// Seed values are meaningless because this class uses a cryptographic service provider internally rather than a pseudo-random sequence.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">/// &lt;/remarks&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CryptoRandom : Random, IDisposable</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The internal random number generator.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> RNGCryptoServiceProvider rng = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> RNGCryptoServiceProvider<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Returns a random number between 0.0 and 1.0.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// A double-precision floating point number greater than or equal to 0.0, and less than 1.0.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/returns&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Uses a cryptographic service provider internally rather than a pseudo-random sequence.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/remarks&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">double</span> Sample<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; var data = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">8</span><span style="color: #000000;">&#93;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">ulong</span> number;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">do</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Get 8 random bytes.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rng.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>data<span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Convert the bytes to an unsigned 64-bit number.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number = BitConverter.<span style="color: #0000FF;">ToUInt64</span><span style="color: #000000;">&#40;</span>data, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>number == <span style="color: #FF0000;">ulong</span>.<span style="color: #0000FF;">MaxValue</span><span style="color: #000000;">&#41;</span>; <span style="color: #008080; font-style: italic;">// The result must be less than 1.0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Divide the number by the largest possible unsigned 64-bit number to get a value in the range 0.0 &lt;= N &lt; 1.0.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> number / <span style="color: #FF0000;">ulong</span>.<span style="color: #0000FF;">MaxValue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080;">#region IDisposable Support</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> disposedValue; <span style="color: #008080; font-style: italic;">// To detect redundant calls</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// IDisposable</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> disposing<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>!<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">disposedValue</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>disposing<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Dispose the underlying cryptographic random number generator.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rng.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">disposedValue</span> = <span style="color: #0600FF;">true</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This code added to correctly implement the disposable pattern.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Do not change this code. &nbsp;Put cleanup code in Dispose(bool disposing) above.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; GC.<span style="color: #0000FF;">SuppressFinalize</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080;">#endregion</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></div></div></pre>
</div>You can create an instance of that class and use it in pretty much exactly the same way as you would a Random object.  The only difference is that you will need to call its Dispose method when you're done, in order to dispose the internal RNGCryptoServiceProvider object.  Sample usage:<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">csharp Code:</div>
	<pre class="alt2" style="margin:0px; padding:px; border:1px inset; width:; max-height:372px;overflow:auto"><div dir="ltr" style="text-align:left;"><div class="csharp" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> partial <span style="color: #FF0000;">class</span> Form1 : Form</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> Form1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> CryptoRandom rng = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> CryptoRandom<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> button1_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Generate 10 random numbers.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; var numbers = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">10</span><span style="color: #000000;">&#93;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i = <span style="color: #FF0000;">0</span>; i &lt; numbers.<span style="color: #0000FF;">Length</span>; i++<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Generate numbers in the range 0 &lt;= n &lt;= 1000</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbers<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = rng.<span style="color: #0000FF;">Next</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1001</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; MessageBox.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Join</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">NewLine</span>, numbers<span style="color: #000000;">&#41;</span>, <span style="color: #808080;">&quot;10 Random Numbers&quot;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Form1_FormClosed<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, FormClosedEventArgs e<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; rng.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li></ol></div></div></pre>
</div></div>


	<div style="padding:10px">

	

	

	

	
		<fieldset class="fieldset">
			<legend>Attached Files</legend>
			<ul>
			<li>
	<img class="inlineimg" src="http://www.vbforums.com/images/attach/cs.gif" alt="File Type: cs" />
	<a href="http://www.vbforums.com/attachment.php?attachmentid=100389&amp;d=1369212158">CryptoRandom.cs</a> 
(2.5 KB)
</li>
			</ul>
		</fieldset>
	

	</div>
]]></content:encoded>
			<category domain="http://www.vbforums.com/forumdisplay.php?44-CodeBank-C">CodeBank - C#</category>
			<dc:creator>jmcilhinney</dc:creator>
			<guid isPermaLink="true">http://www.vbforums.com/showthread.php?722469-A-More-Random-Random-Class</guid>
		</item>
		<item>
			<title>C# - WebBrowser manipulation, setting/getting attributes, clicking elements, etc...</title>
			<link>http://www.vbforums.com/showthread.php?721619-C-WebBrowser-manipulation-setting-getting-attributes-clicking-elements-etc&amp;goto=newpost</link>
			<pubDate>Tue, 14 May 2013 14:08:42 GMT</pubDate>
			<description><![CDATA[I've been doing a lot of application development with WebBrowsers lately. I also notice the occasional thread that pops up asking how to do stuff regarding the 'net (login to a site, click something on a site, etc).

First thing is first, let's create a WebBrowser and navigate it to here:
Code:
---------
//new WebBrowser, or add the control to a form instead
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser;
//Hook the document completed event so we know when a page is completely loaded up
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
//Navigate to google
wb.Navigate("http://www.vbforums.com");

private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{

}
---------
Now, let's see how to work with the document of the webpage in order to fill in the correct values for logging in.

There are a couple of ways to do this. First off, if the element has an "id=***", we know that the same id can *not* exist anywhere else on the same page. So:
Code:
---------
//inside of the document completed event
//here's the html for the username: <input type="text" class="textbox default-value" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" />
//here's the html for the password: <input type="password" class="textbox" tabindex="102" name="vb_login_password" id="navbar_password" size="10" />
//here's the html for the button to click: <input type="submit" class="loginbutton" tabindex="104" value="Log in" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />

//fill in the value for the username element
((WebBrowser)sender).Document.GetElementById("navbar_username").SetAttribute("value", "username");
//fill in the value for the password element
((WebBrowser)sender).Document.GetElementById("navbar_password").SetAttribute("value", "password");
//click the login element
HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName("input");
for (int i = 0; i < htmlcol.Count; i++)
            {
                if (htmlcol[i].GetAttribute("value") == "Log in")
                {
                    htmlcol[i].InvokeMember("click");
                }
            }
---------
Of course, if an element doesn't have an "id=***", we can still get to the element. We just need to figure out what property/attribute we can use to identify them apart.
Code:
---------
//inside of the document completed event
//here's the html for the text to fill in to search for (without id): <input type="text" class="textbox default-value" name="vb_login_username" size="10" accesskey="u" tabindex="101" value="User Name" />
//here's the html for the password (without id: <input type="password" class="textbox" tabindex="102" name="vb_login_password" size="10" />
//here's the html for the button to click (without id): <input type="submit" class="loginbutton" tabindex="104" value="Log in" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />

//get a collection of the input elements on the page
HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName("input");
//loop the input elements
for(int i=0; i<htmlcol.Count; i++)
{
	if (htmlcol[i].Name == "vb_login_username") //this is the only element with a name of "vb_login_username"
	{
		htmlcol[i].SetAttribute("value", "username");
	}
        else if (htmlcol[i].Name == "vb_login_;password") //this is the only element with a name of "vb_login_username"
	{
		htmlcol[i].SetAttribute("value", "password");
	}
}

//click the login element

for (int i = 0; i < htmlcol.Count; i++)
            {
                if (htmlcol[i].GetAttribute("value") == "Log in")
                {
                    htmlcol[i].InvokeMember("click");
                }
            }
---------
You can use the above and modify it to any ID, or any element you can identify. This is how you can login to websites and the such.

*Remember, pretty much any element can accept clicks. If the element you need to click is an <img *** *** *** > element, just loop the "img" tag collections instead of "input" or "button" and call the same .InvokeMember("click");

------

There a lot of other elements we can manipulate as well. Radio buttons, checkboxes, and dropdown lists I have manipulated in my applications before. It's the same concept in general.

Dropdown lists:
//this is the dropdown list for the "Show threads from the ..." at the bottom of the codebank sub forum I'm currently looking at


Code:
---------
//inside a document completed event
//get a collection of the parent "select" elements first
HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName("select");
for(int i=0; i<htmlcol.Count; i++)
{
	if(htmlcol[i].Name = "daysprune") //if the select element is the dropdown we need to select something for
	{
		//create another element collection for the child elements of the "select" element
		HtmlElementCollection htmlcolchild = htmlcol[i].Children;
		for(int j=0; j<htmlcolchild.Count; j++)
		{
			//<option value="1" >Last Day</option>
				//...
			//<option value="-1" selected="selected">Beginning</option>

			//*** Notice the selected="selected" part

			//if we want to select the "Last Day" instead
			if(htmlcolchild[j].InnerText == "Last day") //inner text gets the text between the beginning/end of the element without the attributes
			{
				htmlcolchild[j].SetAttribute("selected", "selected") //set the selected attribute for the last day, instead of the "beginning"
				break;
			}
		}
	}
}

//to make the changes go through, we need to click the "show threads" element
//<input type="submit" class="button" value="Show Threads" />
htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName("input");
for(int i=0; i<htmlcol.Count; i++)
{
	if(htmlcol[i].GetAttribute("value") == "Show Threads")
	{
		htmlcol[i].InvokeMember("click");
		break;
	}
}
---------

I hope this helps people out! Some of it caused me great fustration when I learned it.]]></description>
			<content:encoded><![CDATA[<div>I've been doing a lot of application development with WebBrowsers lately. I also notice the occasional thread that pops up asking how to do stuff regarding the 'net (login to a site, click something on a site, etc).<br />
<br />
First thing is first, let's create a WebBrowser and navigate it to here:<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<hr /><code class="bbcode_code">//new WebBrowser, or add the control to a form instead<br />
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser;<br />
//Hook the document completed event so we know when a page is completely loaded up<br />
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);<br />
//Navigate to google<br />
wb.Navigate(&quot;http://www.vbforums.com&quot;);<br />
<br />
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)<br />
{<br />
<br />
}</code><hr />
</div>Now, let's see how to work with the document of the webpage in order to fill in the correct values for logging in.<br />
<br />
There are a couple of ways to do this. First off, if the element has an &quot;id=***&quot;, we know that the same id can <b>not</b> exist anywhere else on the same page. So:<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<hr /><code class="bbcode_code">//inside of the document completed event<br />
//here's the html for the username: &lt;input type=&quot;text&quot; class=&quot;textbox default-value&quot; name=&quot;vb_login_username&quot; id=&quot;navbar_username&quot; size=&quot;10&quot; accesskey=&quot;u&quot; tabindex=&quot;101&quot; value=&quot;User Name&quot; /&gt;<br />
//here's the html for the password: &lt;input type=&quot;password&quot; class=&quot;textbox&quot; tabindex=&quot;102&quot; name=&quot;vb_login_password&quot; id=&quot;navbar_password&quot; size=&quot;10&quot; /&gt;<br />
//here's the html for the button to click: &lt;input type=&quot;submit&quot; class=&quot;loginbutton&quot; tabindex=&quot;104&quot; value=&quot;Log in&quot; title=&quot;Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself.&quot; accesskey=&quot;s&quot; /&gt;<br />
<br />
//fill in the value for the username element<br />
((WebBrowser)sender).Document.GetElementById(&quot;navbar_username&quot;).SetAttribute(&quot;value&quot;, &quot;username&quot;);<br />
//fill in the value for the password element<br />
((WebBrowser)sender).Document.GetElementById(&quot;navbar_password&quot;).SetAttribute(&quot;value&quot;, &quot;password&quot;);<br />
//click the login element<br />
HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName(&quot;input&quot;);<br />
for (int i = 0; i &lt; htmlcol.Count; i++)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (htmlcol[i].GetAttribute(&quot;value&quot;) == &quot;Log in&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; htmlcol[i].InvokeMember(&quot;click&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</code><hr />
</div>Of course, if an element doesn't have an &quot;id=***&quot;, we can still get to the element. We just need to figure out what property/attribute we can use to identify them apart.<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<hr /><code class="bbcode_code">//inside of the document completed event<br />
//here's the html for the text to fill in to search for (without id): &lt;input type=&quot;text&quot; class=&quot;textbox default-value&quot; name=&quot;vb_login_username&quot; size=&quot;10&quot; accesskey=&quot;u&quot; tabindex=&quot;101&quot; value=&quot;User Name&quot; /&gt;<br />
//here's the html for the password (without id: &lt;input type=&quot;password&quot; class=&quot;textbox&quot; tabindex=&quot;102&quot; name=&quot;vb_login_password&quot; size=&quot;10&quot; /&gt;<br />
//here's the html for the button to click (without id): &lt;input type=&quot;submit&quot; class=&quot;loginbutton&quot; tabindex=&quot;104&quot; value=&quot;Log in&quot; title=&quot;Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself.&quot; accesskey=&quot;s&quot; /&gt;<br />
<br />
//get a collection of the input elements on the page<br />
HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName(&quot;input&quot;);<br />
//loop the input elements<br />
for(int i=0; i&lt;htmlcol.Count; i++)<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; if (htmlcol[i].Name == &quot;vb_login_username&quot;) //this is the only element with a name of &quot;vb_login_username&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; htmlcol[i].SetAttribute(&quot;value&quot;, &quot;username&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; else if (htmlcol[i].Name == &quot;vb_login_;password&quot;) //this is the only element with a name of &quot;vb_login_username&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; htmlcol[i].SetAttribute(&quot;value&quot;, &quot;password&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
}<br />
<br />
//click the login element<br />
<br />
for (int i = 0; i &lt; htmlcol.Count; i++)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (htmlcol[i].GetAttribute(&quot;value&quot;) == &quot;Log in&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; htmlcol[i].InvokeMember(&quot;click&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</code><hr />
</div>You can use the above and modify it to any ID, or any element you can identify. This is how you can login to websites and the such.<br />
<br />
*Remember, pretty much any element can accept clicks. If the element you need to click is an &lt;img *** *** *** &gt; element, just loop the &quot;img&quot; tag collections instead of &quot;input&quot; or &quot;button&quot; and call the same .InvokeMember(&quot;click&quot;);<br />
<br />
------<br />
<br />
There a lot of other elements we can manipulate as well. Radio buttons, checkboxes, and dropdown lists I have manipulated in my applications before. It's the same concept in general.<br />
<br />
Dropdown lists:<br />
//this is the dropdown list for the &quot;Show threads from the ...&quot; at the bottom of the codebank sub forum I'm currently looking at<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<hr /><code class="bbcode_code">//inside a document completed event<br />
//get a collection of the parent &quot;select&quot; elements first<br />
HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName(&quot;select&quot;);<br />
for(int i=0; i&lt;htmlcol.Count; i++)<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; if(htmlcol[i].Name = &quot;daysprune&quot;) //if the select element is the dropdown we need to select something for<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //create another element collection for the child elements of the &quot;select&quot; element<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HtmlElementCollection htmlcolchild = htmlcol[i].Children;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=0; j&lt;htmlcolchild.Count; j++)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&lt;option value=&quot;1&quot; &gt;Last Day&lt;/option&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&lt;option value=&quot;-1&quot; selected=&quot;selected&quot;&gt;Beginning&lt;/option&gt;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //*** Notice the selected=&quot;selected&quot; part<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if we want to select the &quot;Last Day&quot; instead<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(htmlcolchild[j].InnerText == &quot;Last day&quot;) //inner text gets the text between the beginning/end of the element without the attributes<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; htmlcolchild[j].SetAttribute(&quot;selected&quot;, &quot;selected&quot;) //set the selected attribute for the last day, instead of the &quot;beginning&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
}<br />
<br />
//to make the changes go through, we need to click the &quot;show threads&quot; element<br />
//&lt;input type=&quot;submit&quot; class=&quot;button&quot; value=&quot;Show Threads&quot; /&gt;<br />
htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName(&quot;input&quot;);<br />
for(int i=0; i&lt;htmlcol.Count; i++)<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; if(htmlcol[i].GetAttribute(&quot;value&quot;) == &quot;Show Threads&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; htmlcol[i].InvokeMember(&quot;click&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
}</code><hr />
</div><br />
I hope this helps people out! Some of it caused me great fustration when I learned it.</div>

]]></content:encoded>
			<category domain="http://www.vbforums.com/forumdisplay.php?44-CodeBank-C">CodeBank - C#</category>
			<dc:creator>detlion1643</dc:creator>
			<guid isPermaLink="true">http://www.vbforums.com/showthread.php?721619-C-WebBrowser-manipulation-setting-getting-attributes-clicking-elements-etc</guid>
		</item>
	</channel>
</rss>
