<?xml version="1.0" encoding="utf-8"?>
				<!-- generator="e107" -->
				<!-- content type="News" -->
				<rss  version="2.0" 
					xmlns:content="http://purl.org/rss/1.0/modules/content/" 
					xmlns:atom="http://www.w3.org/2005/Atom"
					xmlns:dc="http://purl.org/dc/elements/1.1/"
					xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"

				>
				<channel>
				<title>Midnight blogging : News</title>
				<link>/myblog/</link>
				<description></description>

<language>en-gb</language>
				<copyright>This site is powered by e107, which is released under the terms of the GNU GPL License.</copyright>
				<managingEditor>yiannis@nospam.com (yiangos)</managingEditor>
				<webMaster>yiannis@nospam.com (yiangos)</webMaster>
				<pubDate>Mon, 21 May 2012 00:31:04 +0300</pubDate>
				<lastBuildDate>Mon, 21 May 2012 00:31:04 +0300</lastBuildDate>
				<docs>http://backend.userland.com/rss</docs>
				<generator>e107 (http://e107.org)</generator>
				<sy:updatePeriod>hourly</sy:updatePeriod>
				<sy:updateFrequency>1</sy:updateFrequency>

				<ttl>60</ttl>
<atom:link href="http://yiannis.vavouranakis.gr/myblog/e107_plugins/rss_menu/rss.php?news.2" rel="self" type="application/rss+xml" />

					<image>
					<title>Midnight blogging : News</title>
					<url>http://yiannis.vavouranakis.gr/myblog/e107_images/button.png</url>
					<link>/myblog/</link>
					<width>88</width>
					<height>31</height>
					<description></description>
					</image>
<item>
<title>Back online</title>
<link>http://yiannis.vavouranakis.gr/myblog/news.php?item.45.1</link>
<description><![CDATA[My subscription with lunarpages expired sometime in August. Since we're practically broke (hence the LONG pause in the blog, no free time due to working overtime and double jobs to make ends meet), I decided to shut down my hosting with them, and move the site to a friend's and partner's server.<br /><br />We'll see how this goes.<br /><br />]]></description>
<content:encoded><![CDATA[My subscription with lunarpages expired sometime in August. Since we're practically broke (hence the LONG pause in the blog, no free time due to working overtime and double jobs to make ends meet), I decided to shut down my hosting with them, and move the site to a friend's and partner's server.<br /><br />We'll see how this goes.<br /><br />]]></content:encoded>
<category domain='http://yiannis.vavouranakis.gr/myblog/news.php?cat.1'>Misc</category>
<dc:creator>yiangos</dc:creator>
<pubDate>Tue, 07 Sep 2010 15:13:46 +0300</pubDate>
<guid isPermaLink="true">http://yiannis.vavouranakis.gr/myblog/news.php?item.45.1</guid>
</item>

<item>
<title>Glad to be of help.</title>
<link>http://yiannis.vavouranakis.gr/myblog/news.php?item.44.1</link>
<description><![CDATA[Remember <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.8.1' rel='external' >this guy</a>? Well, I just finished helping him make his website look spiffy. Anyone reading this blog, go to <a class='bbcode' href='http://www.henningludvigsen.com' rel='external' >http://www.henningludvigsen.com</a> and show him some love. He's a REALLY talented artist!<br />]]></description>
<content:encoded><![CDATA[Remember <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.8.1' rel='external' >this guy</a>? Well, I just finished helping him make his website look spiffy. Anyone reading this blog, go to <a class='bbcode' href='http://www.henningludvigsen.com' rel='external' >http://www.henningludvigsen.com</a> and show him some love. He's a REALLY talented artist!<br />]]></content:encoded>
<category domain='http://yiannis.vavouranakis.gr/myblog/news.php?cat.1'>Misc</category>
<dc:creator>yiangos</dc:creator>
<pubDate>Mon, 28 Dec 2009 23:26:30 +0200</pubDate>
<guid isPermaLink="true">http://yiannis.vavouranakis.gr/myblog/news.php?item.44.1</guid>
</item>

<item>
<title>Querystring obfuscation. my take.</title>
<link>http://yiannis.vavouranakis.gr/myblog/news.php?item.43.1</link>
<description><![CDATA[At one point, it was required to obfuscate the data in the querystring. Hiding it (i.e. using different methods to pass data to a page) was not an option, as the pages needed to be able to stand alone (e.g. give a fully functional link for an offer banner). So I built an obfuscation scheme.<br />My first attempt was to use a gzip stream on the entire querystring and base64-encode the resulting binary data. However, I found out through trial and error that this method has a lower boundary. The resulting string is never less than 200 characters long. The queristrings I was trying to obfuscate were typically 40-50 characters long (including parameter names, equal signs and ampersands). Moreover, the base64 encoding introduced charaxters that could never appear in a querystring value (equal signs). So I dumped that idea, as it seemed too much of an overkill to obfuscate a 40 character string and come up with a 200 character string as a result. I had to find a different algorithm.<br /><br />The algorithm I came up with, is quite simple and straight forward, yet to my eyes beautiful in its simplicity. <br /><br />First of all, I noted that most (if not all) data passed via GET are either strings or numbers (there's a caveat here, which I'll cover at the end). <br /><br />Next there was a decision to make, i.e. whether to include parameter names in the final string or not. I opted not to include them, and rather use some application wide structure (e.g. cache) to store the parameter names. Since the obfuscation/parsing of the querystring would occur within the same class, it would be easy to maintain the correspondence between values in the querystring and names in the list structure kept at application level.<br /><br />So, having made that decision, I started designing the algorithm.<br /><br />To obfuscate, we distinguish between numbers and strings. <br />If a parameter value is a number, then convert the value to hex. <br />If the parameter value is a string, then convert it to a byte array, then convert each byte to its hex representation (2-digit). <br />Prepend the now obfuscated value with a letter (other than a-f which are used in hex) to tell whether the value that follows is a number or a string, e.g. use U for numbers and L for strings.<br />Finally, concatenate all values (with their distinguishing letter) using a third letter as a delimiter (e.g. M) and using the order imposed by the list that contains the parameter names.<br /><br />To parse an obfuscated string, we reverse the steps:<br />First we split on the delimiter letter. <br />For each value, we check the first letter. If it signifies number, then convert the value directly from hex to decimal, and you're done. If it signifies string, then convert each pair of characters to a number (essentially a byte), and thus populate a byte array. Finally encode the bytearray using the encoding used to create  the bytearray during obfuscation and you're done.<br />Each value is finally assigned to its corresponding parameter name.<br /><br />code to come...]]></description>
<content:encoded><![CDATA[At one point, it was required to obfuscate the data in the querystring. Hiding it (i.e. using different methods to pass data to a page) was not an option, as the pages needed to be able to stand alone (e.g. give a fully functional link for an offer banner). So I built an obfuscation scheme.<br />My first attempt was to use a gzip stream on the entire querystring and base64-encode the resulting binary data. However, I found out through trial and error that this method has a lower boundary. The resulting string is never less than 200 characters long. The queristrings I was trying to obfuscate were typically 40-50 characters long (including parameter names, equal signs and ampersands). Moreover, the base64 encoding introduced charaxters that could never appear in a querystring value (equal signs). So I dumped that idea, as it seemed too much of an overkill to obfuscate a 40 character string and come up with a 200 character string as a result. I had to find a different algorithm.<br /><br />The algorithm I came up with, is quite simple and straight forward, yet to my eyes beautiful in its simplicity. <br /><br />First of all, I noted that most (if not all) data passed via GET are either strings or numbers (there's a caveat here, which I'll cover at the end). <br /><br />Next there was a decision to make, i.e. whether to include parameter names in the final string or not. I opted not to include them, and rather use some application wide structure (e.g. cache) to store the parameter names. Since the obfuscation/parsing of the querystring would occur within the same class, it would be easy to maintain the correspondence between values in the querystring and names in the list structure kept at application level.<br /><br />So, having made that decision, I started designing the algorithm.<br /><br />To obfuscate, we distinguish between numbers and strings. <br />If a parameter value is a number, then convert the value to hex. <br />If the parameter value is a string, then convert it to a byte array, then convert each byte to its hex representation (2-digit). <br />Prepend the now obfuscated value with a letter (other than a-f which are used in hex) to tell whether the value that follows is a number or a string, e.g. use U for numbers and L for strings.<br />Finally, concatenate all values (with their distinguishing letter) using a third letter as a delimiter (e.g. M) and using the order imposed by the list that contains the parameter names.<br /><br />To parse an obfuscated string, we reverse the steps:<br />First we split on the delimiter letter. <br />For each value, we check the first letter. If it signifies number, then convert the value directly from hex to decimal, and you're done. If it signifies string, then convert each pair of characters to a number (essentially a byte), and thus populate a byte array. Finally encode the bytearray using the encoding used to create  the bytearray during obfuscation and you're done.<br />Each value is finally assigned to its corresponding parameter name.<br /><br />code to come...]]></content:encoded>
<category domain='http://yiannis.vavouranakis.gr/myblog/news.php?cat.1'>Misc</category>
<dc:creator>yiangos</dc:creator>
<pubDate>Thu, 27 Aug 2009 02:24:34 +0300</pubDate>
<guid isPermaLink="true">http://yiannis.vavouranakis.gr/myblog/news.php?item.43.1</guid>
</item>

<item>
<title>Towards Steganography III: a class that manipulates RGB colors</title>
<link>http://yiannis.vavouranakis.gr/myblog/news.php?item.42.2</link>
<description><![CDATA[In my two previous posts (<a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.40' rel='external' >here</a> and <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.41' rel='external' >here</a>), I showed how to create two of the components needed for my <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.39' rel='external' >steganography project</a>. This post is about the part where we manipulate the least significant bits of the RGB components of a pixel.<br /><br />This has two parts: A "getter" part, where we get the values of the least significant bits, and a "setter" part, where we change these bits so that they represent a particular number.<br /><br />Each color component is actually a byte. So, getting the least significant bit of the R,G or B component of a pixel's color, is the same as getting the least significant bit of a byte. This is quite easy as an algorithm: simply grab the byte, and bitwise AND it with 1. It's pretty easy to see that a bitwise AND between 1 and any (positive) number results in 1 (if the number is odd) or 0 (if the number is even). In VB.NET, the result of a bitwise operation between two integers is an integer of the same type (in our case, a byte), so we need to further cast the result (1 or 0) to boolean. The function is as follows:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;Protected Shared <span style="color: #0600FF;">Function</span> GetComponentLastBit<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> num <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Return <span style="color: #0600FF;">CBool</span><span style="color: #000000;">&#40;</span>num <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />That's ok if we have the color component. However, we're more likely to have just the pixel's color. The following function comes in handy:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;Protected Shared <span style="color: #0600FF;">Function</span> GetColorComponent<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> pixelColor <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span>, <span style="color: #FF8000;">ByVal</span> colorComponent <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> returnValue <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Select</span> <span style="color: #0600FF;">Case</span> colorComponent</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Case</span> <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; returnValue = pixelColor.<span style="color: #0000FF;">R</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Case</span> <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; returnValue = pixelColor.<span style="color: #0000FF;">G</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Case</span> <span style="color: #FF0000;">2</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; returnValue = pixelColor.<span style="color: #0000FF;">B</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Select</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Return returnValue</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />This simply gets a color, and returns one of its three components, according to the second parameter. For that parameter, 0 stands for Red, 1 stands for Green and 2 stands for Blue.<br /><br />Putting things together, we finally get this function:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> Shared <span style="color: #0600FF;">Function</span> GetNumFromColor<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> pixelColor <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> returnValue <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">To</span> <span style="color: #FF0000;">2</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returnValue += <span style="color: #0600FF;">CByte</span><span style="color: #000000;">&#40;</span>GetComponentLastBit<span style="color: #000000;">&#40;</span>GetColorComponent<span style="color: #000000;">&#40;</span>pixelColor, i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> &lt;&lt; i</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Return returnValue</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br />A lot of things take place here. First of all, we get a color as a parameter. From the previous function, we've already mapped 0 to Red, 1 to Green and 2 to Blue. Therefore, we iterate over these three values, and so we're able to get the last bit of each color component by means of the GetComponentLastBit() method. This information is returned as a boolean. We cast the boolean as byte (a True value corresponds to 11111111, or 256, while a false value corresponds to 0) and grab its least significant bit (the "And 1" part). So now we have either 1 (for true) or 0 (for false). We need to place this value at the corresponding position of a 3-bit word. Since we've already chosen implicitly an order (0-Red, 1-Green, 2-Blue), we follow that for consistency, and assign the red bit to the least significant bit of the 3-bit word, the green bit to the middle one and the blue bit to the most significant bit od the 3-bit word. That part is taken care of by the left shift (<<). Adding the three values, we get a 3-bit word. In other words, we get a number from 0 to 7 inclusive.<br /><br />Apparently, this function will also come in handy in the decryption process.<br /><br />This sums up the "getter" part.<br /><br />The "setter" part, has three equivalent functions. First, a function that toggles the last bit of a specific color component (i.e. of a byte):<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;Protected Shared <span style="color: #0600FF;">Function</span> ToggleLastBit<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> num <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> GetComponentLastBit<span style="color: #000000;">&#40;</span>num<span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return num - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return num + <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br />Not much to note here. If the least significant bit (LSB) is 1, then the result of a toggle would be that the LSB becomes 0 and all other bits remain as they were. Changing the LSB from 1 to 0 without any other changes is the same as subtracting 1 from the original number. If the LSB is 0, then changing it to 1 without any other changes is the same as adding 1 to the original number. Since the LSB was 0, there will be no carry, so no other bit is affected and therefore an overflow can't occur either.<br /><br />The previous function blindly toggles the LSB of any byte it receives as its parameter. To toggle on demand, we use the following function<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;Protected Shared <span style="color: #0600FF;">Function</span> SetColorComponent<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> num <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span>, <span style="color: #FF8000;">ByVal</span> setFlag <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> setFlag <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return ToggleLastBit<span style="color: #000000;">&#40;</span>num<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return num</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br />All this function does, is toggle the LSB if the flag (the second argument) is True. Now, what we need, is a way to set or unset this flag. <br /><br />The flag we just mentioned, when set, states that "this particular component doesn't have its last bit set to the desired value, and needs to be toggled". In other words, this flag should be set when the corresponding bits of the 3-bit word we get from the "getter" part of the original color are different to the 3-bit word we need to store. This is easy to know. If we use XOR between the 3-bit word of the original and the 3-bit word we need to store, then the bits of the result that are 1 are the ones that correspond to the components that need their LSB toggled. So, putting it all together:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> Shared <span style="color: #0600FF;">Function</span> SetNumToColor<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> pixelColor <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span>, <span style="color: #FF8000;">ByVal</span> number <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> colorNumber <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span> = GetNumFromColor<span style="color: #000000;">&#40;</span>pixelColor<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> number &lt; <span style="color: #FF0000;">8</span> <span style="color: #804040;">And</span> number &gt;= <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'Find which components need toggling</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colorNumber = colorNumber Xor number</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'Colornumber is now a number from 0 to 7, where the bits equal to 1 </span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'represent the color components that need to toggle their last bit, with red in the</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'least significant bit and blue in the most significant bit</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'To get the Red bit, we rightshift 0 times and then bitwise AND this number with 1.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'To get the Green bit, we rightshift 1 times and then bitwise AND this number with 1.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'To get the Blue bit, we rightshift 2 times and then bitwise AND this number with 1.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span>.<span style="color: #0000FF;">FromArgb</span><span style="color: #000000;">&#40;</span> _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; pixelColor.<span style="color: #0000FF;">A</span>, _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; SetColorComponent<span style="color: #000000;">&#40;</span>pixelColor.<span style="color: #0000FF;">R</span>, <span style="color: #0600FF;">CBool</span><span style="color: #000000;">&#40;</span>colorNumber &gt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; SetColorComponent<span style="color: #000000;">&#40;</span>pixelColor.<span style="color: #0000FF;">G</span>, <span style="color: #0600FF;">CBool</span><span style="color: #000000;">&#40;</span>colorNumber &gt;&gt; <span style="color: #FF0000;">1</span> <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; SetColorComponent<span style="color: #000000;">&#40;</span>pixelColor.<span style="color: #0000FF;">B</span>, <span style="color: #0600FF;">CBool</span><span style="color: #000000;">&#40;</span>colorNumber &gt;&gt; <span style="color: #FF0000;">2</span> <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return pixelColor</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br />One thing to note here, is that in the resulting color, we use the original alpha channel unchanged. That means that we can use semi-transparent images without really any issues. The transparency will carry through unaffected.<br /><br />This is the end of the "setter" part. The attached zip contains these functions as parts of a class in a separate project.<br /><br />]]></description>
<content:encoded><![CDATA[In my two previous posts (<a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.40' rel='external' >here</a> and <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.41' rel='external' >here</a>), I showed how to create two of the components needed for my <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.39' rel='external' >steganography project</a>. This post is about the part where we manipulate the least significant bits of the RGB components of a pixel.<br /><br />This has two parts: A "getter" part, where we get the values of the least significant bits, and a "setter" part, where we change these bits so that they represent a particular number.<br /><br />Each color component is actually a byte. So, getting the least significant bit of the R,G or B component of a pixel's color, is the same as getting the least significant bit of a byte. This is quite easy as an algorithm: simply grab the byte, and bitwise AND it with 1. It's pretty easy to see that a bitwise AND between 1 and any (positive) number results in 1 (if the number is odd) or 0 (if the number is even). In VB.NET, the result of a bitwise operation between two integers is an integer of the same type (in our case, a byte), so we need to further cast the result (1 or 0) to boolean. The function is as follows:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;Protected Shared <span style="color: #0600FF;">Function</span> GetComponentLastBit<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> num <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Return <span style="color: #0600FF;">CBool</span><span style="color: #000000;">&#40;</span>num <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />That's ok if we have the color component. However, we're more likely to have just the pixel's color. The following function comes in handy:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;Protected Shared <span style="color: #0600FF;">Function</span> GetColorComponent<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> pixelColor <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span>, <span style="color: #FF8000;">ByVal</span> colorComponent <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> returnValue <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Select</span> <span style="color: #0600FF;">Case</span> colorComponent</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Case</span> <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; returnValue = pixelColor.<span style="color: #0000FF;">R</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Case</span> <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; returnValue = pixelColor.<span style="color: #0000FF;">G</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Case</span> <span style="color: #FF0000;">2</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; returnValue = pixelColor.<span style="color: #0000FF;">B</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Select</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Return returnValue</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />This simply gets a color, and returns one of its three components, according to the second parameter. For that parameter, 0 stands for Red, 1 stands for Green and 2 stands for Blue.<br /><br />Putting things together, we finally get this function:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> Shared <span style="color: #0600FF;">Function</span> GetNumFromColor<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> pixelColor <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> returnValue <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">To</span> <span style="color: #FF0000;">2</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returnValue += <span style="color: #0600FF;">CByte</span><span style="color: #000000;">&#40;</span>GetComponentLastBit<span style="color: #000000;">&#40;</span>GetColorComponent<span style="color: #000000;">&#40;</span>pixelColor, i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> &lt;&lt; i</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Return returnValue</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br />A lot of things take place here. First of all, we get a color as a parameter. From the previous function, we've already mapped 0 to Red, 1 to Green and 2 to Blue. Therefore, we iterate over these three values, and so we're able to get the last bit of each color component by means of the GetComponentLastBit() method. This information is returned as a boolean. We cast the boolean as byte (a True value corresponds to 11111111, or 256, while a false value corresponds to 0) and grab its least significant bit (the "And 1" part). So now we have either 1 (for true) or 0 (for false). We need to place this value at the corresponding position of a 3-bit word. Since we've already chosen implicitly an order (0-Red, 1-Green, 2-Blue), we follow that for consistency, and assign the red bit to the least significant bit of the 3-bit word, the green bit to the middle one and the blue bit to the most significant bit od the 3-bit word. That part is taken care of by the left shift (<<). Adding the three values, we get a 3-bit word. In other words, we get a number from 0 to 7 inclusive.<br /><br />Apparently, this function will also come in handy in the decryption process.<br /><br />This sums up the "getter" part.<br /><br />The "setter" part, has three equivalent functions. First, a function that toggles the last bit of a specific color component (i.e. of a byte):<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;Protected Shared <span style="color: #0600FF;">Function</span> ToggleLastBit<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> num <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> GetComponentLastBit<span style="color: #000000;">&#40;</span>num<span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return num - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return num + <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br />Not much to note here. If the least significant bit (LSB) is 1, then the result of a toggle would be that the LSB becomes 0 and all other bits remain as they were. Changing the LSB from 1 to 0 without any other changes is the same as subtracting 1 from the original number. If the LSB is 0, then changing it to 1 without any other changes is the same as adding 1 to the original number. Since the LSB was 0, there will be no carry, so no other bit is affected and therefore an overflow can't occur either.<br /><br />The previous function blindly toggles the LSB of any byte it receives as its parameter. To toggle on demand, we use the following function<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;Protected Shared <span style="color: #0600FF;">Function</span> SetColorComponent<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> num <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span>, <span style="color: #FF8000;">ByVal</span> setFlag <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> setFlag <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return ToggleLastBit<span style="color: #000000;">&#40;</span>num<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return num</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br />All this function does, is toggle the LSB if the flag (the second argument) is True. Now, what we need, is a way to set or unset this flag. <br /><br />The flag we just mentioned, when set, states that "this particular component doesn't have its last bit set to the desired value, and needs to be toggled". In other words, this flag should be set when the corresponding bits of the 3-bit word we get from the "getter" part of the original color are different to the 3-bit word we need to store. This is easy to know. If we use XOR between the 3-bit word of the original and the 3-bit word we need to store, then the bits of the result that are 1 are the ones that correspond to the components that need their LSB toggled. So, putting it all together:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> Shared <span style="color: #0600FF;">Function</span> SetNumToColor<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> pixelColor <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span>, <span style="color: #FF8000;">ByVal</span> number <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> colorNumber <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Byte</span> = GetNumFromColor<span style="color: #000000;">&#40;</span>pixelColor<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> number &lt; <span style="color: #FF0000;">8</span> <span style="color: #804040;">And</span> number &gt;= <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'Find which components need toggling</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colorNumber = colorNumber Xor number</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'Colornumber is now a number from 0 to 7, where the bits equal to 1 </span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'represent the color components that need to toggle their last bit, with red in the</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'least significant bit and blue in the most significant bit</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'To get the Red bit, we rightshift 0 times and then bitwise AND this number with 1.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'To get the Green bit, we rightshift 1 times and then bitwise AND this number with 1.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">'To get the Blue bit, we rightshift 2 times and then bitwise AND this number with 1.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Color</span>.<span style="color: #0000FF;">FromArgb</span><span style="color: #000000;">&#40;</span> _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; pixelColor.<span style="color: #0000FF;">A</span>, _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; SetColorComponent<span style="color: #000000;">&#40;</span>pixelColor.<span style="color: #0000FF;">R</span>, <span style="color: #0600FF;">CBool</span><span style="color: #000000;">&#40;</span>colorNumber &gt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; SetColorComponent<span style="color: #000000;">&#40;</span>pixelColor.<span style="color: #0000FF;">G</span>, <span style="color: #0600FF;">CBool</span><span style="color: #000000;">&#40;</span>colorNumber &gt;&gt; <span style="color: #FF0000;">1</span> <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; SetColorComponent<span style="color: #000000;">&#40;</span>pixelColor.<span style="color: #0000FF;">B</span>, <span style="color: #0600FF;">CBool</span><span style="color: #000000;">&#40;</span>colorNumber &gt;&gt; <span style="color: #FF0000;">2</span> <span style="color: #804040;">And</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> _</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return pixelColor</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br />One thing to note here, is that in the resulting color, we use the original alpha channel unchanged. That means that we can use semi-transparent images without really any issues. The transparency will carry through unaffected.<br /><br />This is the end of the "setter" part. The attached zip contains these functions as parts of a class in a separate project.<br /><br />]]></content:encoded>
<category domain='http://yiannis.vavouranakis.gr/myblog/news.php?cat.2'>Tech related</category>
<dc:creator>yiangos</dc:creator>
<pubDate>Wed, 22 Jul 2009 01:14:00 +0300</pubDate>
<guid isPermaLink="true">http://yiannis.vavouranakis.gr/myblog/news.php?item.42.2</guid>
</item>

<item>
<title>Towards Steganography II: a pseudo-random number generator</title>
<link>http://yiannis.vavouranakis.gr/myblog/news.php?item.41.2</link>
<description><![CDATA[I've shown the inner workings of the first essential component for my <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.39' rel='external' >steganography project</a> in a previous post <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.40' rel='external' >here</a>. The second essential part is a pseudo-random number generator (PRNG) that's seedable.<br />I could use the .NET framework built-in PRNG, but since I have no control over it, I can never be certain that Microsoft will always keep the same algorithm for its PRNG. Now, seeing how crucial the PRNG is in the scheme, I believe the way to go is to implement a custom one.<br /><br />I know that PRNG's are supposed to be split into two categories, the ones that are safe to use in cryptography and the ones that aren't. Also, by searching, I've found that there is a very good PRNG that's safe for cryptography; the Yarrow PRNG. However, I can't really make out the C/C++ implementation. The ISAAC PRNG, which seemed like a nice equivalent, I was also unable to use for some reason. My attempts to use a cryptographically safe PRNG stopped there.<br /><br />In the end, I wrote down  a vb.net implementation of the central PRNG I found <a class='bbcode' href='http://www.honeylocust.com/javascript/randomizer.html' rel='external' >here</a>. It is definitely a very "weak" PRNG, in that it is not safe for cryptography: it is a well known algorithhm, that produces a number sequence that's relatively easy to predict. Apparently, I'll factor this out in a separate library, so that if I manage to get a different PRNG, I can simply replace the dll - or have them both working side -by-side and choose which I want to use each time.<br /><br />The PRNG lies within a class named "Shuffler". This class has two constructors. One that seeds the PRNG with a number generated using the current datetime, and one that takes a seed as argument. The class has only one public method: <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> Shuffle<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> rangemax <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span>, <span style="color: #FF8000;">ByVal</span> totalnum <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li></ol></div></div><br />This method produces an array with totalnum <em class='bbcode italic'>different</em> integers between zero and rangemax.<br /><br />The zip contains the full solution for this part of the project.<br />]]></description>
<content:encoded><![CDATA[I've shown the inner workings of the first essential component for my <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.39' rel='external' >steganography project</a> in a previous post <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.40' rel='external' >here</a>. The second essential part is a pseudo-random number generator (PRNG) that's seedable.<br />I could use the .NET framework built-in PRNG, but since I have no control over it, I can never be certain that Microsoft will always keep the same algorithm for its PRNG. Now, seeing how crucial the PRNG is in the scheme, I believe the way to go is to implement a custom one.<br /><br />I know that PRNG's are supposed to be split into two categories, the ones that are safe to use in cryptography and the ones that aren't. Also, by searching, I've found that there is a very good PRNG that's safe for cryptography; the Yarrow PRNG. However, I can't really make out the C/C++ implementation. The ISAAC PRNG, which seemed like a nice equivalent, I was also unable to use for some reason. My attempts to use a cryptographically safe PRNG stopped there.<br /><br />In the end, I wrote down  a vb.net implementation of the central PRNG I found <a class='bbcode' href='http://www.honeylocust.com/javascript/randomizer.html' rel='external' >here</a>. It is definitely a very "weak" PRNG, in that it is not safe for cryptography: it is a well known algorithhm, that produces a number sequence that's relatively easy to predict. Apparently, I'll factor this out in a separate library, so that if I manage to get a different PRNG, I can simply replace the dll - or have them both working side -by-side and choose which I want to use each time.<br /><br />The PRNG lies within a class named "Shuffler". This class has two constructors. One that seeds the PRNG with a number generated using the current datetime, and one that takes a seed as argument. The class has only one public method: <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> Shuffle<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> rangemax <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span>, <span style="color: #FF8000;">ByVal</span> totalnum <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li></ol></div></div><br />This method produces an array with totalnum <em class='bbcode italic'>different</em> integers between zero and rangemax.<br /><br />The zip contains the full solution for this part of the project.<br />]]></content:encoded>
<category domain='http://yiannis.vavouranakis.gr/myblog/news.php?cat.2'>Tech related</category>
<dc:creator>yiangos</dc:creator>
<pubDate>Tue, 07 Jul 2009 02:19:27 +0300</pubDate>
<guid isPermaLink="true">http://yiannis.vavouranakis.gr/myblog/news.php?item.41.2</guid>
</item>

<item>
<title>Towards steganography I: a Brainfuck code generator and interpreter in VB.Net</title>
<link>http://yiannis.vavouranakis.gr/myblog/news.php?item.40.1</link>
<description><![CDATA[In order to develop the steganography project <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.39' rel='external' >I described earlier</a>, I'll need a class that can convert any given text to a Brainfuck program, and also to "run" such a program to reproduce the original text. It turns out, this is not that difficult.<br />The easier part is the interpreter. A Brainfuck program source code can have only 8 different characters, each of which stands for a different command. Also, Brainfuck assumes that there is a memory organized in 1 byte slots, an input stream and an output stream. Finally, there needs to be a command pointer, that points to the command being executed.<br /><br />The output stream will be replaced by a string.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected _outStream <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li></ol></div></div><br /><br />I won't deal with the input stream here, as the interpreter I need will only be used on programs that print "hard coded" strings. <br /><br />The 1-byte memory slots will be simulated by an arraylist of integers. A 64-bit integer variable will play the role of the pointer, and another 64-bit integer will play the role of the command pointer.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> Protected _mem <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> ArrayList</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;Protected _memptr <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;Protected _progptr <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span></div></li></ol></div></div><br /><br />The increase and decrease commands amount to acting upon a specific item in the arrayList of integers.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">CLng</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> + <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;_progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">'...Decrease value</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">CLng</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />The "move pointer" commands amount to increasing or decreasing the pointer variable.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> _memptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">If</span> _mem.<span style="color: #0000FF;">Count</span> = _memptr <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _mem.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">'...decrease pointer</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_memptr -= <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />The print command amounts to adding another character to the end of the string The character will be the one with charcter code equal to the value of the element in the "memory" arraylist pointed by the "pointer" variable.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_outStream &= <span style="color: #0600FF;">ChrW</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />The two flow control commands are somewhat trickier.  I need to implement a bracket matching subroutine, and I also need code to execute whenever the command pointer hits one of these, as well as keep track of all left brackets I've passed through. So here goes: <br /><br />The bracket matching subroutine will be run whenever the command pointer hits a left bracket, and its job will be to find the corresponding right bracket. To that end, we need to keep track of opening and closing brackets. So, we need a counter for counting currently open brackets. This will be an integer variable initialized at 1 (we already have an oen bracket, the one that made the original call to the subroutine). The subroutine will then scan the program code, <em class='bbcode italic'>starting at the command pointer position</em>, increase its counter whenever it encounters a left bracket, and decrease its counter whenever it encounters a right bracket. When the counter reaches 0,  that's where the initial left bracket has its matching right bracket. The subroutine should return the position of that right bracket.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> findMatchingBracket<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> CodeStr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> currentPosition <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> openBrackets = <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">While</span> openBrackets &gt; <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; currentPosition += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CodeStr<span style="color: #000000;">&#40;</span>currentPosition<span style="color: #000000;">&#41;</span> = <span style="color: #808080;">&quot;&#091;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openBrackets += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ElseIf CodeStr<span style="color: #000000;">&#40;</span>currentPosition<span style="color: #000000;">&#41;</span> = <span style="color: #808080;">&quot;]&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openBrackets -= <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">While</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return currentPosition</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />To keep track of all the left brackets, all I need is a stack.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected LoopStack <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> Stack<span style="color: #000000;">&#40;</span>Of <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span></div></li></ol></div></div><br /><br />So, when the command pointer encounters a left bracket, the program should first of all, push its position on top of the stack. Then it will check the value at the memory slot pointed by the memory pointer. If zero, call the aforementioned subroutine, otherwise, simply move along.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">LoopStack.<span style="color: #0000FF;">Push</span><span style="color: #000000;">&#40;</span>_progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">If</span> _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _progptr = findMatchingBracket<span style="color: #000000;">&#40;</span>CodeStr, _progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li></ol></div></div><br /><br />When the command pointer encounters a right bracket, the program should first pop a value from the stack. Since we're talking about bracket matching, the position popped will undoubtedyt be the position of its matching left bracket. Then check the value at the memory slot pointed by the memory pointer. If nonzero, move the command pointer to the position popped. Otherwise, simply move along.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">Dim</span> tempptr <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span> = LoopStack.<span style="color: #0000FF;">Pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">If</span> _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _progptr = tempptr</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li></ol></div></div><br /><br />I include checking for an input command, but this will be a no-op. The interpreter will ignore it and move along. <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #008080; font-style: italic;">'Implement user input</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />Also, if the character encountered is not one of these 8, the interpreter will simply move along without doing anything.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />Here's the entire interpreter function:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> EvalBF<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> CodeStr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> token <span style="color: #FF8000;">As</span> Char</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _memptr = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _mem.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _outStream = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">While</span> _progptr &lt; CodeStr.<span style="color: #0000FF;">Length</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; token = CodeStr<span style="color: #000000;">&#40;</span>_progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">Select</span> <span style="color: #0600FF;">Case</span> token</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;&gt;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _memptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> _mem.<span style="color: #0000FF;">Count</span> = _memptr <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _mem.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;&lt;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _memptr -= <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;+&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">CLng</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> + <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;-&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">CLng</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;.&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _outStream &= <span style="color: #0600FF;">ChrW</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;,&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">'Implement user input</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;&#091;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; LoopStack.<span style="color: #0000FF;">Push</span><span style="color: #000000;">&#40;</span>_progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr = findMatchingBracket<span style="color: #000000;">&#40;</span>CodeStr, _progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;]&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> tempptr <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span> = LoopStack.<span style="color: #0000FF;">Pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr = tempptr</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">End</span> <span style="color: #0600FF;">Select</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">While</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return _outStream</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />The code generator requires a bit more thought. As there is more than one way to write a program in Brainfuck that prints a string, we need to have a consistent way of finding the shortest (or an adequately short) source code. It seems that one easy way of writing a program like the one we want (that simply prints a hard-coded message) is to prepopulate some slots with some values, and then increase or decrease the slot with value that's closest to the character code we want to print. Then print the character and start looking for the closest slot to the next letter, and so on. <br /><br />Apparently, the number of prepopulated slots will have a dramatic effect on the length of the code. Oh, what the heck, let's waste some cpu cycles and memory, and generate programs for the same output string, using up to 32 initial slots, and choose the one with the shortest code.<br /><br />So, first of all, we need a function that outputs the same characted a specified number of times. If you've seen a Brainfuck program, you'll know that there are lots of "+", "-", ">" and "<" sequences, so this helper function will come in quite handy:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> strTimes<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> <span style="color: #0600FF;">str</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> times <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> endstr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">To</span> times - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; endstr += <span style="color: #0600FF;">str</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return endstr</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />The above function will be used once we know which slot to alter, and how much to alter it. So, we need a function that finds which slot is the closest to the value we need to store:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> findClosestBin<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> <span style="color: #0600FF;">val</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> bins<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> currbin <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> minBin <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> b <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">1</span> <span style="color: #FF8000;">To</span> bins.<span style="color: #0000FF;">Length</span> - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span> - bins<span style="color: #000000;">&#40;</span>b<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> &lt; Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span> - bins<span style="color: #000000;">&#40;</span>minBin<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minBin = b</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">If</span> Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span> - bins<span style="color: #000000;">&#40;</span>minBin<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> &gt; Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span> - bins<span style="color: #000000;">&#40;</span>currbin<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Return currbin</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return minBin</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /> A few things to note here: <ul class='bbcode'><li class='bbcode'><em class='bbcode italic'>val</em> is the integer value we wish to store.</li><li class='bbcode'>bins() is the array that holds the slots we're using.</li><li class='bbcode'>currbin is the slot currently pointed at by the pointer.</li></ul> So what this function essentially does, is pretty simple: it iterates over all slots (the bins() array) and checks to see which slot is the closest (either higher or lower) to the desired value (val). It then returns that slot's position.<br /><br />Next thing we need, is a way to know ehether we need to increase or decrease the value at the slot with the closest value to the desired:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> printDifference<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> diff <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> poschar <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> negchar <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> <span style="color: #0600FF;">str</span> = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> b <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">While</span> b &lt; Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span>diff<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> diff &gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">str</span> &= poschar</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">str</span> &= negchar</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; b += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">While</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return <span style="color: #0600FF;">str</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />Indeed, this function returns the increase operator "+" if the value at the slot is lower than the desired, and the decrease operator "-" if the value at the slot is higher.<br /><br />Finally, we need a helper function that prepopulates a specified number of slots. What we'll do, is use populate the first of the slots with a high enough value, and then fill the next ones with increasing values. To this end, we'll use a loop on the value of the first slot, and add an increasing amount to each of the other slots, while decreasing the value of the first one. At the end, we restore its original value:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> writeBins<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> numBins <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> diff <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> <span style="color: #0600FF;">str</span> = strTimes<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;+&quot;</span>, diff<span style="color: #000000;">&#41;</span> + <span style="color: #808080;">&quot;&#091;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">1</span> <span style="color: #FF8000;">To</span> numBins - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">str</span> &= <span style="color: #808080;">&quot;&gt;&quot;</span> + strTimes<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;+&quot;</span>, i + <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">str</span> &= strTimes<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&lt;&quot;</span>, numBins - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> + <span style="color: #808080;">&quot;-]&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">str</span> &= strTimes<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;+&quot;</span>, diff<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return <span style="color: #0600FF;">str</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />We now have all we need, except for the optimal number of slots to prepopulate. As I said, we'll find that by brute force.<br /><br />We start by creating a function that generates the Brainfuck program we need, using a specific number of slots to prepopulate (this number being passed as argument): <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> Dumbtext2BF<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> textstr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> numBins <span style="color: #FF8000;">As</span> Short<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">If</span> numBins &lt;= <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Throw</span> <span style="color: #FF8000;">New</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;error: too few bins&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> bins<span style="color: #000000;">&#40;</span>numBins - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> diff <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = Math.<span style="color: #0000FF;">Floor</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">127</span> / numBins<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">To</span> numBins - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; bins<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span> = <span style="color: #000000;">&#40;</span>i + <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> * diff</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> codestr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = writeBins<span style="color: #000000;">&#40;</span>numBins, diff<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> counter <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> currbin <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> newbin <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> c <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">While</span> <span style="color: #000000;">&#40;</span>counter &lt; textstr.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; c = <span style="color: #0600FF;">AscW</span><span style="color: #000000;">&#40;</span>textstr<span style="color: #000000;">&#40;</span>counter<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; newbin = findClosestBin<span style="color: #000000;">&#40;</span>c, bins, currbin<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; codestr &= printDifference<span style="color: #000000;">&#40;</span>newbin - currbin, <span style="color: #808080;">&quot;&gt;&quot;</span>, <span style="color: #808080;">&quot;&lt;&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; codestr &= printDifference<span style="color: #000000;">&#40;</span>c - bins<span style="color: #000000;">&#40;</span>newbin<span style="color: #000000;">&#41;</span>, <span style="color: #808080;">&quot;+&quot;</span>, <span style="color: #808080;">&quot;-&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; codestr &= <span style="color: #808080;">&quot;.&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; currbin = newbin</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; bins<span style="color: #000000;">&#40;</span>newbin<span style="color: #000000;">&#41;</span> = c</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; counter += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">While</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return codestr</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />Pretty straight forward: after the initial check that the number of slots (numbins) is positive, we prepopulate the slots starting with the integer division of 128 (last character code for ascii) to the number of slots.<br /><br />We then get the message and assign values one character at a time. Each character we assign successfully, we also print, thus "freeing" the slot it was "occupying".We go on like that until there are no more characters in the message.<br /><br />We're almost done. In a huge waste of cpu cycles, we use brute force  to find the shortest generated code for the same string: <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> Text2BF<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> textstr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> bestbin <span style="color: #FF8000;">As</span> Short = <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> results<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> beststr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> resultstr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">1</span> <span style="color: #FF8000;">To</span> <span style="color: #FF0000;">32</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> <span style="color: #0600FF;">str</span> = Dumbtext2BF<span style="color: #000000;">&#40;</span>textstr, i<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; results<span style="color: #000000;">&#40;</span>i - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">str</span>.<span style="color: #0000FF;">length</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> results<span style="color: #000000;">&#40;</span>i - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> &lt; results<span style="color: #000000;">&#40;</span>bestbin - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beststr = <span style="color: #0600FF;">str</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bestbin = i</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; resultstr &= i &amp; <span style="color: #808080;">&quot;: &quot;</span> &amp; results<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span> &amp; <span style="color: #808080;">&quot;\n&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return beststr</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />The zip contains the entire Brainfuck project.<br />]]></description>
<content:encoded><![CDATA[In order to develop the steganography project <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.39' rel='external' >I described earlier</a>, I'll need a class that can convert any given text to a Brainfuck program, and also to "run" such a program to reproduce the original text. It turns out, this is not that difficult.<br />The easier part is the interpreter. A Brainfuck program source code can have only 8 different characters, each of which stands for a different command. Also, Brainfuck assumes that there is a memory organized in 1 byte slots, an input stream and an output stream. Finally, there needs to be a command pointer, that points to the command being executed.<br /><br />The output stream will be replaced by a string.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected _outStream <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li></ol></div></div><br /><br />I won't deal with the input stream here, as the interpreter I need will only be used on programs that print "hard coded" strings. <br /><br />The 1-byte memory slots will be simulated by an arraylist of integers. A 64-bit integer variable will play the role of the pointer, and another 64-bit integer will play the role of the command pointer.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> Protected _mem <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> ArrayList</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;Protected _memptr <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;Protected _progptr <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span></div></li></ol></div></div><br /><br />The increase and decrease commands amount to acting upon a specific item in the arrayList of integers.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">CLng</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> + <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;_progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">'...Decrease value</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">CLng</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />The "move pointer" commands amount to increasing or decreasing the pointer variable.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> _memptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">If</span> _mem.<span style="color: #0000FF;">Count</span> = _memptr <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _mem.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">'...decrease pointer</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_memptr -= <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />The print command amounts to adding another character to the end of the string The character will be the one with charcter code equal to the value of the element in the "memory" arraylist pointed by the "pointer" variable.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_outStream &= <span style="color: #0600FF;">ChrW</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />The two flow control commands are somewhat trickier.  I need to implement a bracket matching subroutine, and I also need code to execute whenever the command pointer hits one of these, as well as keep track of all left brackets I've passed through. So here goes: <br /><br />The bracket matching subroutine will be run whenever the command pointer hits a left bracket, and its job will be to find the corresponding right bracket. To that end, we need to keep track of opening and closing brackets. So, we need a counter for counting currently open brackets. This will be an integer variable initialized at 1 (we already have an oen bracket, the one that made the original call to the subroutine). The subroutine will then scan the program code, <em class='bbcode italic'>starting at the command pointer position</em>, increase its counter whenever it encounters a left bracket, and decrease its counter whenever it encounters a right bracket. When the counter reaches 0,  that's where the initial left bracket has its matching right bracket. The subroutine should return the position of that right bracket.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> findMatchingBracket<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> CodeStr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> currentPosition <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> openBrackets = <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">While</span> openBrackets &gt; <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; currentPosition += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CodeStr<span style="color: #000000;">&#40;</span>currentPosition<span style="color: #000000;">&#41;</span> = <span style="color: #808080;">&quot;&#091;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openBrackets += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ElseIf CodeStr<span style="color: #000000;">&#40;</span>currentPosition<span style="color: #000000;">&#41;</span> = <span style="color: #808080;">&quot;]&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openBrackets -= <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">While</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return currentPosition</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />To keep track of all the left brackets, all I need is a stack.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected LoopStack <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> Stack<span style="color: #000000;">&#40;</span>Of <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span></div></li></ol></div></div><br /><br />So, when the command pointer encounters a left bracket, the program should first of all, push its position on top of the stack. Then it will check the value at the memory slot pointed by the memory pointer. If zero, call the aforementioned subroutine, otherwise, simply move along.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">LoopStack.<span style="color: #0000FF;">Push</span><span style="color: #000000;">&#40;</span>_progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">If</span> _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _progptr = findMatchingBracket<span style="color: #000000;">&#40;</span>CodeStr, _progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li></ol></div></div><br /><br />When the command pointer encounters a right bracket, the program should first pop a value from the stack. Since we're talking about bracket matching, the position popped will undoubtedyt be the position of its matching left bracket. Then check the value at the memory slot pointed by the memory pointer. If nonzero, move the command pointer to the position popped. Otherwise, simply move along.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">Dim</span> tempptr <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span> = LoopStack.<span style="color: #0000FF;">Pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">If</span> _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _progptr = tempptr</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li></ol></div></div><br /><br />I include checking for an input command, but this will be a no-op. The interpreter will ignore it and move along. <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #008080; font-style: italic;">'Implement user input</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />Also, if the character encountered is not one of these 8, the interpreter will simply move along without doing anything.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">_progptr += <span style="color: #FF0000;">1</span></div></li></ol></div></div><br /><br />Here's the entire interpreter function:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> EvalBF<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> CodeStr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> token <span style="color: #FF8000;">As</span> Char</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _memptr = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _mem.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _outStream = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">While</span> _progptr &lt; CodeStr.<span style="color: #0000FF;">Length</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; token = CodeStr<span style="color: #000000;">&#40;</span>_progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">Select</span> <span style="color: #0600FF;">Case</span> token</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;&gt;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _memptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> _mem.<span style="color: #0000FF;">Count</span> = _memptr <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _mem.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;&lt;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _memptr -= <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;+&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">CLng</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> + <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;-&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">CLng</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;.&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _outStream &= <span style="color: #0600FF;">ChrW</span><span style="color: #000000;">&#40;</span>_mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;,&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">'Implement user input</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;&#091;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; LoopStack.<span style="color: #0000FF;">Push</span><span style="color: #000000;">&#40;</span>_progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr = findMatchingBracket<span style="color: #000000;">&#40;</span>CodeStr, _progptr<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;]&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> tempptr <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span> = LoopStack.<span style="color: #0000FF;">Pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> _mem<span style="color: #000000;">&#40;</span>_memptr<span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr = tempptr</div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">Case</span> <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; &nbsp; &nbsp; _progptr += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">End</span> <span style="color: #0600FF;">Select</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">While</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return _outStream</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />The code generator requires a bit more thought. As there is more than one way to write a program in Brainfuck that prints a string, we need to have a consistent way of finding the shortest (or an adequately short) source code. It seems that one easy way of writing a program like the one we want (that simply prints a hard-coded message) is to prepopulate some slots with some values, and then increase or decrease the slot with value that's closest to the character code we want to print. Then print the character and start looking for the closest slot to the next letter, and so on. <br /><br />Apparently, the number of prepopulated slots will have a dramatic effect on the length of the code. Oh, what the heck, let's waste some cpu cycles and memory, and generate programs for the same output string, using up to 32 initial slots, and choose the one with the shortest code.<br /><br />So, first of all, we need a function that outputs the same characted a specified number of times. If you've seen a Brainfuck program, you'll know that there are lots of "+", "-", ">" and "<" sequences, so this helper function will come in quite handy:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> strTimes<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> <span style="color: #0600FF;">str</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> times <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> endstr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">To</span> times - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; endstr += <span style="color: #0600FF;">str</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return endstr</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />The above function will be used once we know which slot to alter, and how much to alter it. So, we need a function that finds which slot is the closest to the value we need to store:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> findClosestBin<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> <span style="color: #0600FF;">val</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> bins<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> currbin <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> minBin <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> b <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">1</span> <span style="color: #FF8000;">To</span> bins.<span style="color: #0000FF;">Length</span> - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span> - bins<span style="color: #000000;">&#40;</span>b<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> &lt; Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span> - bins<span style="color: #000000;">&#40;</span>minBin<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minBin = b</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">If</span> Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span> - bins<span style="color: #000000;">&#40;</span>minBin<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> &gt; Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span> - bins<span style="color: #000000;">&#40;</span>currbin<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Return currbin</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return minBin</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /> A few things to note here: <ul class='bbcode'><li class='bbcode'><em class='bbcode italic'>val</em> is the integer value we wish to store.</li><li class='bbcode'>bins() is the array that holds the slots we're using.</li><li class='bbcode'>currbin is the slot currently pointed at by the pointer.</li></ul> So what this function essentially does, is pretty simple: it iterates over all slots (the bins() array) and checks to see which slot is the closest (either higher or lower) to the desired value (val). It then returns that slot's position.<br /><br />Next thing we need, is a way to know ehether we need to increase or decrease the value at the slot with the closest value to the desired:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> printDifference<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> diff <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> poschar <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> negchar <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> <span style="color: #0600FF;">str</span> = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> b <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">While</span> b &lt; Math.<span style="color: #0600FF;">Abs</span><span style="color: #000000;">&#40;</span>diff<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> diff &gt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">str</span> &= poschar</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">str</span> &= negchar</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; b += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">While</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return <span style="color: #0600FF;">str</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />Indeed, this function returns the increase operator "+" if the value at the slot is lower than the desired, and the decrease operator "-" if the value at the slot is higher.<br /><br />Finally, we need a helper function that prepopulates a specified number of slots. What we'll do, is use populate the first of the slots with a high enough value, and then fill the next ones with increasing values. To this end, we'll use a loop on the value of the first slot, and add an increasing amount to each of the other slots, while decreasing the value of the first one. At the end, we restore its original value:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> writeBins<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> numBins <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> diff <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> <span style="color: #0600FF;">str</span> = strTimes<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;+&quot;</span>, diff<span style="color: #000000;">&#41;</span> + <span style="color: #808080;">&quot;&#091;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">1</span> <span style="color: #FF8000;">To</span> numBins - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">str</span> &= <span style="color: #808080;">&quot;&gt;&quot;</span> + strTimes<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;+&quot;</span>, i + <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">str</span> &= strTimes<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&lt;&quot;</span>, numBins - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> + <span style="color: #808080;">&quot;-]&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">str</span> &= strTimes<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;+&quot;</span>, diff<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return <span style="color: #0600FF;">str</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />We now have all we need, except for the optimal number of slots to prepopulate. As I said, we'll find that by brute force.<br /><br />We start by creating a function that generates the Brainfuck program we need, using a specific number of slots to prepopulate (this number being passed as argument): <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Protected <span style="color: #0600FF;">Function</span> Dumbtext2BF<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> textstr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> numBins <span style="color: #FF8000;">As</span> Short<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">If</span> numBins &lt;= <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Throw</span> <span style="color: #FF8000;">New</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;error: too few bins&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> bins<span style="color: #000000;">&#40;</span>numBins - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> diff <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = Math.<span style="color: #0000FF;">Floor</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">127</span> / numBins<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">To</span> numBins - <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; bins<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span> = <span style="color: #000000;">&#40;</span>i + <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> * diff</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> codestr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = writeBins<span style="color: #000000;">&#40;</span>numBins, diff<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> counter <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> currbin <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> newbin <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">0</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> c <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">While</span> <span style="color: #000000;">&#40;</span>counter &lt; textstr.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; c = <span style="color: #0600FF;">AscW</span><span style="color: #000000;">&#40;</span>textstr<span style="color: #000000;">&#40;</span>counter<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; newbin = findClosestBin<span style="color: #000000;">&#40;</span>c, bins, currbin<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; codestr &= printDifference<span style="color: #000000;">&#40;</span>newbin - currbin, <span style="color: #808080;">&quot;&gt;&quot;</span>, <span style="color: #808080;">&quot;&lt;&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; codestr &= printDifference<span style="color: #000000;">&#40;</span>c - bins<span style="color: #000000;">&#40;</span>newbin<span style="color: #000000;">&#41;</span>, <span style="color: #808080;">&quot;+&quot;</span>, <span style="color: #808080;">&quot;-&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; codestr &= <span style="color: #808080;">&quot;.&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; currbin = newbin</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; bins<span style="color: #000000;">&#40;</span>newbin<span style="color: #000000;">&#41;</span> = c</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; counter += <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">While</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return codestr</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />Pretty straight forward: after the initial check that the number of slots (numbins) is positive, we prepopulate the slots starting with the integer division of 128 (last character code for ascii) to the number of slots.<br /><br />We then get the message and assign values one character at a time. Each character we assign successfully, we also print, thus "freeing" the slot it was "occupying".We go on like that until there are no more characters in the message.<br /><br />We're almost done. In a huge waste of cpu cycles, we use brute force  to find the shortest generated code for the same string: <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> Text2BF<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> textstr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> bestbin <span style="color: #FF8000;">As</span> Short = <span style="color: #FF0000;">1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> results<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> beststr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> resultstr <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> = <span style="color: #FF0000;">1</span> <span style="color: #FF8000;">To</span> <span style="color: #FF0000;">32</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> <span style="color: #0600FF;">str</span> = Dumbtext2BF<span style="color: #000000;">&#40;</span>textstr, i<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; results<span style="color: #000000;">&#40;</span>i - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> = <span style="color: #0600FF;">str</span>.<span style="color: #0000FF;">length</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> results<span style="color: #000000;">&#40;</span>i - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> &lt; results<span style="color: #000000;">&#40;</span>bestbin - <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beststr = <span style="color: #0600FF;">str</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bestbin = i</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; resultstr &= i &amp; <span style="color: #808080;">&quot;: &quot;</span> &amp; results<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span> &amp; <span style="color: #808080;">&quot;\n&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #FF8000;">Next</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Return beststr</div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></li></ol></div></div><br /><br />The zip contains the entire Brainfuck project.<br />]]></content:encoded>
<category domain='http://yiannis.vavouranakis.gr/myblog/news.php?cat.1'>Misc</category>
<dc:creator>yiangos</dc:creator>
<pubDate>Mon, 06 Jul 2009 14:52:57 +0300</pubDate>
<guid isPermaLink="true">http://yiannis.vavouranakis.gr/myblog/news.php?item.40.1</guid>
</item>

<item>
<title>Steganography, Brainfuck and other related issues</title>
<link>http://yiannis.vavouranakis.gr/myblog/news.php?item.39.2</link>
<description><![CDATA[I've always been fascinated with encryption. And, by always, I mean since the age of 7. That's when I "invented" my first encryption method. Of course, it was neither a new, nor a hard-to-break method (I shifted all vowels by one to the "right" and all consonants by one to the left), but still, it was an encryption method.<br /><br /><br />Since then, I've regularly tred myself both in inventing encryption methods and in deciphering encrypted messages. I can't say I was very successful. Most of my methods were mere letter shifts - or eventually amounted to such - and also I preserved white space. On the other hand, I usually managed to crack simple emcryption patterns.<br /><br />Then along came the computer. My first encounter with real encryption was in 2004, when, as part of my job, I ported an ASP site to PHP. That's when I came across the <a class='bbcode' href='http://en.wikipedia.org/wiki/RC4' rel='external' >ARCFOUR </a>cipher. I was so excited! It was the first time I ever saw this kind of encryption method. I tried to understand its inner workings, but failed miserably. Eventually, I gave up and simply translated to PHP statement to statement. <br /><br />However, the dice had been thrown. When I found some spare time, I sat down determined to figure this thing out. After all, it was only a bunch of commands - how hard could this be, really? Well, it was quite hard, if you've never seen tis kind of thing before. But eventually I made it. I even transfered the method to other languages (i.e. my favourite at the time, python). In any case, the ARCFOUR still remains my method of choice when it comes to using quick-and-dirty encryption.<br /><br />And then, I came across <a class='bbcode' href='http://www.phpclasses.org/browse/package/3312.html' rel='external' >this class</a>. Hiding messages (or even files) in images, in such a way that the resulting image appears relatively untouched, now <em class='bbcode italic'>that</em> was a different ballgame altogether. I tried to work the class, but failed, I never quite got it to function properly.<br /><br />Time passed, and things got slow in the area. Until one day, when a colleague came with a joke, about a language so difficult that even a simple "Hello world" program would be forbiddingly hard to write. That got us into the fun of esoteric programming languages, most of which were simply made for the fun of it. We really laughed with their names, and of course the one that stood out was <a class='bbcode' href='http://en.wikipedia.org/wiki/Brainfuck' rel='external' >Brainfuck</a>. The name alone is enough to cause a chuckle, let alone seeing what the source code for an actual program looks like. <br /><br />At the time, I dismissed Brainfuck as another funny invention, but nothing more than that. So, the day before yesterday, while searching for something irrelevant, I found <a class='bbcode' href='http://www.java2s.com/Code/JavaScript/Security/BrainfuckConverterInterpreterinJavaScript.htm' rel='external' >this little gem</a>. Interestingly, they have enlisted the Brainfuck interpreter under security. That got me thinking. Although in itself, a brainuck program for printing a hard-coded string is actually quite longer than the string itself, the fact that Brainfuck only uses 8 symbols for any program can make things quite interesting: You only need 3 bits to encode each of the source code's commands. Actually, a program that simply prints a message uses only 7 of the 8 symbols (the eighth is there to allow user input), so one of the eight available slots in the three bits is free to use for other purposes. After quite some thinking, I came up with quite a plan for a steganography program. Here's what you'll need:<br /><ul class='bbcode'><li class='bbcode'>a message to encrypt. Of course, noone forbids you to hide an already encrypted message.<br /></li><li class='bbcode'>a 24-bit (or less) color image to use for medium. Any type (jpg, png, gif...) will do. The resulting image however, will be in png, so size (width x height) matters in two ways: a very small image can't hold a very large message (i.e. whose corresponding Brainfuck source code contains more commands than the number of pixels in the image), and a very large image will be huge when converted to png, and therefore harder to send to the recipient.<br /></li><li class='bbcode'>A pseudo-random number generator that can be seeded. Usually, these produce floating point numbers from -1 to 1 or from 0 to 1. We'll be assuming that our generator can produce integers from 0 to the number of pixels in the image (exclusive). That's not too hard to achieve using the floating point numbers produced by the generator, the width and the height of the image in pixels.<br /></li><li class='bbcode'>A table that maps the seven required Brainfuck commands to numbers 0 to 6, and an "End-Of-Message" mark to 7.<br /></li></ul><br />And here's how to do it, in human-talk:<br /><ul class='bbcode'><li class='bbcode'>Convert the message to a Brainfuck program.<br /></li><li class='bbcode'>Seed a pseudo-random number generator (PRNG) with a value derived from the image (e.g. width+height).<br /></li><li class='bbcode'>get a value from the generator. Each value represents a pixel in such a way that if the top-left pixel is pixel 0, then the pixel N is at the [(N-N%width)/width] line and [N%width] column.<br /></li><li class='bbcode'>Get the pixel pointed by the generator.<br /></li><li class='bbcode'>Get the least significant bit (LSB) of the value of every color component for that pixel. You now have a 3-bit word. <br /></li><li class='bbcode'> Check the integer value that corresponds to that 3-bit word and compare it to the 3-bit value of the Brainfuck command (you get that from the mapping table) you want to hide in the pixel. To make things even more obscure, instead of using the actual mapping table value, you can use a pseudo-randomly generated rotation of that table for each Brainfuck command. That's relatively easy to do: use another seeded PRNG, that produces values from 0 to 7, and use the XOR of a generated number and the integers in the mapping table.<br /></li><li class='bbcode'>Flip the bits you got from the pixel so that they match the value you need to hide.<br /></li><li class='bbcode'> Repeat this process with the next command, and the next generated number, making sure that there are no repetitions in the generated numbers for the selection of pixels.<br /></li><li class='bbcode'>When you're done with all the commands, generate another number, and store an "End-of-Message" signal in the LSB's of the corresponding pixel.<br /></li><li class='bbcode'>Save the image as png. Png is lossless, so all the bit-flipping we did is preserved.<br /></li></ul><br />The important thing about this method is that the original image is not needed to decrypt the message. Here's how you decrypt.<br /><ul class='bbcode'><li class='bbcode'>Create two PRNG's and seed them both with the (known) value you extract from the image (in our case, width+height). One PRNG will generate values between 0 (inclusive) and the number of pixels in the image (exclusive) while the other will generate numbers between 0 and 7 (both inclusive).<br /></li><li class='bbcode'>Start generating numbers<br /></li><li class='bbcode'>For each new (i.e. not seen before) number you get from the "first" PRNG, go to the corresponding pixel<br /></li><li class='bbcode'>Get the three LSB's from the color components of this pixel<br /></li><li class='bbcode'>XOR this value with the value you get from the second PRNG. Since (a XOR b) XOR b = a, this makes sure we get the correct final value. Match this value to a Brainfuck command, using the mapping table. Store this command.<br /></li><li class='bbcode'>Carry on until you hit an "End-of-Message" signal.<br /></li><li class='bbcode'>Execute the Brainfuck program you got.<br /></li><li class='bbcode'>The result is the original message.<br /></li></ul><br /><br />This scheme could be used e.g. to store a serial number in a background (or logo) image of an application, and thus serve for license validation.<br /><br />The attachment contains a .NET 3.5 windorms application that does just this.]]></description>
<content:encoded><![CDATA[I've always been fascinated with encryption. And, by always, I mean since the age of 7. That's when I "invented" my first encryption method. Of course, it was neither a new, nor a hard-to-break method (I shifted all vowels by one to the "right" and all consonants by one to the left), but still, it was an encryption method.<br /><br /><br />Since then, I've regularly tred myself both in inventing encryption methods and in deciphering encrypted messages. I can't say I was very successful. Most of my methods were mere letter shifts - or eventually amounted to such - and also I preserved white space. On the other hand, I usually managed to crack simple emcryption patterns.<br /><br />Then along came the computer. My first encounter with real encryption was in 2004, when, as part of my job, I ported an ASP site to PHP. That's when I came across the <a class='bbcode' href='http://en.wikipedia.org/wiki/RC4' rel='external' >ARCFOUR </a>cipher. I was so excited! It was the first time I ever saw this kind of encryption method. I tried to understand its inner workings, but failed miserably. Eventually, I gave up and simply translated to PHP statement to statement. <br /><br />However, the dice had been thrown. When I found some spare time, I sat down determined to figure this thing out. After all, it was only a bunch of commands - how hard could this be, really? Well, it was quite hard, if you've never seen tis kind of thing before. But eventually I made it. I even transfered the method to other languages (i.e. my favourite at the time, python). In any case, the ARCFOUR still remains my method of choice when it comes to using quick-and-dirty encryption.<br /><br />And then, I came across <a class='bbcode' href='http://www.phpclasses.org/browse/package/3312.html' rel='external' >this class</a>. Hiding messages (or even files) in images, in such a way that the resulting image appears relatively untouched, now <em class='bbcode italic'>that</em> was a different ballgame altogether. I tried to work the class, but failed, I never quite got it to function properly.<br /><br />Time passed, and things got slow in the area. Until one day, when a colleague came with a joke, about a language so difficult that even a simple "Hello world" program would be forbiddingly hard to write. That got us into the fun of esoteric programming languages, most of which were simply made for the fun of it. We really laughed with their names, and of course the one that stood out was <a class='bbcode' href='http://en.wikipedia.org/wiki/Brainfuck' rel='external' >Brainfuck</a>. The name alone is enough to cause a chuckle, let alone seeing what the source code for an actual program looks like. <br /><br />At the time, I dismissed Brainfuck as another funny invention, but nothing more than that. So, the day before yesterday, while searching for something irrelevant, I found <a class='bbcode' href='http://www.java2s.com/Code/JavaScript/Security/BrainfuckConverterInterpreterinJavaScript.htm' rel='external' >this little gem</a>. Interestingly, they have enlisted the Brainfuck interpreter under security. That got me thinking. Although in itself, a brainuck program for printing a hard-coded string is actually quite longer than the string itself, the fact that Brainfuck only uses 8 symbols for any program can make things quite interesting: You only need 3 bits to encode each of the source code's commands. Actually, a program that simply prints a message uses only 7 of the 8 symbols (the eighth is there to allow user input), so one of the eight available slots in the three bits is free to use for other purposes. After quite some thinking, I came up with quite a plan for a steganography program. Here's what you'll need:<br /><ul class='bbcode'><li class='bbcode'>a message to encrypt. Of course, noone forbids you to hide an already encrypted message.<br /></li><li class='bbcode'>a 24-bit (or less) color image to use for medium. Any type (jpg, png, gif...) will do. The resulting image however, will be in png, so size (width x height) matters in two ways: a very small image can't hold a very large message (i.e. whose corresponding Brainfuck source code contains more commands than the number of pixels in the image), and a very large image will be huge when converted to png, and therefore harder to send to the recipient.<br /></li><li class='bbcode'>A pseudo-random number generator that can be seeded. Usually, these produce floating point numbers from -1 to 1 or from 0 to 1. We'll be assuming that our generator can produce integers from 0 to the number of pixels in the image (exclusive). That's not too hard to achieve using the floating point numbers produced by the generator, the width and the height of the image in pixels.<br /></li><li class='bbcode'>A table that maps the seven required Brainfuck commands to numbers 0 to 6, and an "End-Of-Message" mark to 7.<br /></li></ul><br />And here's how to do it, in human-talk:<br /><ul class='bbcode'><li class='bbcode'>Convert the message to a Brainfuck program.<br /></li><li class='bbcode'>Seed a pseudo-random number generator (PRNG) with a value derived from the image (e.g. width+height).<br /></li><li class='bbcode'>get a value from the generator. Each value represents a pixel in such a way that if the top-left pixel is pixel 0, then the pixel N is at the [(N-N%width)/width] line and [N%width] column.<br /></li><li class='bbcode'>Get the pixel pointed by the generator.<br /></li><li class='bbcode'>Get the least significant bit (LSB) of the value of every color component for that pixel. You now have a 3-bit word. <br /></li><li class='bbcode'> Check the integer value that corresponds to that 3-bit word and compare it to the 3-bit value of the Brainfuck command (you get that from the mapping table) you want to hide in the pixel. To make things even more obscure, instead of using the actual mapping table value, you can use a pseudo-randomly generated rotation of that table for each Brainfuck command. That's relatively easy to do: use another seeded PRNG, that produces values from 0 to 7, and use the XOR of a generated number and the integers in the mapping table.<br /></li><li class='bbcode'>Flip the bits you got from the pixel so that they match the value you need to hide.<br /></li><li class='bbcode'> Repeat this process with the next command, and the next generated number, making sure that there are no repetitions in the generated numbers for the selection of pixels.<br /></li><li class='bbcode'>When you're done with all the commands, generate another number, and store an "End-of-Message" signal in the LSB's of the corresponding pixel.<br /></li><li class='bbcode'>Save the image as png. Png is lossless, so all the bit-flipping we did is preserved.<br /></li></ul><br />The important thing about this method is that the original image is not needed to decrypt the message. Here's how you decrypt.<br /><ul class='bbcode'><li class='bbcode'>Create two PRNG's and seed them both with the (known) value you extract from the image (in our case, width+height). One PRNG will generate values between 0 (inclusive) and the number of pixels in the image (exclusive) while the other will generate numbers between 0 and 7 (both inclusive).<br /></li><li class='bbcode'>Start generating numbers<br /></li><li class='bbcode'>For each new (i.e. not seen before) number you get from the "first" PRNG, go to the corresponding pixel<br /></li><li class='bbcode'>Get the three LSB's from the color components of this pixel<br /></li><li class='bbcode'>XOR this value with the value you get from the second PRNG. Since (a XOR b) XOR b = a, this makes sure we get the correct final value. Match this value to a Brainfuck command, using the mapping table. Store this command.<br /></li><li class='bbcode'>Carry on until you hit an "End-of-Message" signal.<br /></li><li class='bbcode'>Execute the Brainfuck program you got.<br /></li><li class='bbcode'>The result is the original message.<br /></li></ul><br /><br />This scheme could be used e.g. to store a serial number in a background (or logo) image of an application, and thus serve for license validation.<br /><br />The attachment contains a .NET 3.5 windorms application that does just this.]]></content:encoded>
<category domain='http://yiannis.vavouranakis.gr/myblog/news.php?cat.2'>Tech related</category>
<dc:creator>yiangos</dc:creator>
<pubDate>Mon, 06 Jul 2009 02:47:43 +0300</pubDate>
<guid isPermaLink="true">http://yiannis.vavouranakis.gr/myblog/news.php?item.39.2</guid>
</item>

<item>
<title>Button popularity</title>
<link>http://yiannis.vavouranakis.gr/myblog/news.php?item.38.2</link>
<description><![CDATA[It seems that my <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.37' rel='external' >custom server-side button control</a> is gaining momentum in the office. Yay!<br />]]></description>
<content:encoded><![CDATA[It seems that my <a class='bbcode' href='http://yiannis.vavouranakis.gr/myblog/news.php?extend.37' rel='external' >custom server-side button control</a> is gaining momentum in the office. Yay!<br />]]></content:encoded>
<category domain='http://yiannis.vavouranakis.gr/myblog/news.php?cat.2'>Tech related</category>
<dc:creator>yiangos</dc:creator>
<pubDate>Wed, 27 May 2009 00:55:49 +0300</pubDate>
<guid isPermaLink="true">http://yiannis.vavouranakis.gr/myblog/news.php?item.38.2</guid>
</item>

<item>
<title>Custom button ASP.NET 2.0 server control</title>
<link>http://yiannis.vavouranakis.gr/myblog/news.php?item.37.2</link>
<description><![CDATA[At work, I found a frequently occurring scenario: the designers would come up with some button design that's just impossible to represent in an efficient manner with ASP.NET 2.0 controls.<br />A simple example is as follows:<br /><img src='http://yiannis.vavouranakis.gr/myblog/e107_images/newspost_images/aquabutton.png' class='bbcode' alt=''  /><br />This is really a background image of an aqua-style button, and a text rendered on top of it. <br /><br />You'd say that an imageButton control could easily tackle this one, and in general I'd agree. Some markup like the following would be sufficient:<br /><br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;asp:ImageButton ID=&quot;button&quot; runat=&quot;server&quot; ImageUrl=&quot;~/PathToImages/ButtonImage.png&quot; /&gt;</div></li></ol></div></div><br /><br />However, what happens if the site is multilingual (something that happens quite a lot)? Then you'd have to resort to a solution, where the ImageUrl for the ImageButton is not set declaratively, but programmatically. So, instead of the above, you'd have<br /><br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;asp:ImageButton ID=&quot;button&quot; runat=&quot;server&quot; /&gt;</div></li></ol></div></div><br /><br />and then in the codebehind page, you'd have to set the image url explicitly, depending on the language:<br /><br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">Dim</span> PageLanguage <span style="color: #FF8000;">as</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">'Code that sets the PageLanguage variable to the current language</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">button.<span style="color: #0000FF;">ImageUrl</span>=<span style="color: #808080;">&quot;~/PathToImages/ButtonImage&quot;</span> &amp; PageLanguage &amp; <span style="color: #808080;">&quot;png&quot;</span></div></li></ol></div></div><br /><br />You'd say that this is not really much, but imagine having 3, 4 or more buttons on the same page. Even worse, imagine having buttons in a Repeater (for Item selection) or in a gridView. Things can get pretty messy quite fast.<br /><br />Then there's another way of doing it. Actually have an imageButton without the text, and a linkbutton with the text, and posittion one on top of the other. This tackles the problem of having to define all the states of the "button" programmatically, but adds extra complexity with the event handlers and such; the two "buttons" must actually use the same method for their click event. Moreover, it won't always be easy to position one on top of the other.<br /><br />At first, I followed the former solution. But since I kept seeing the same thing happen over and over again, I thought that there must be another way of doing this. After a bit of google digging that resulted next to nothing, and quite a few trial-and-error attempts, I ended up with the following specs for my solution:<br /><ul class='bbcode'><li class='bbcode'>It must implement the IButtonControl interface. This goes without saying, as the IButtonControl interface exists purely as a guideline for control developers, as to which methods <em class='bbcode italic'>must</em> be implemented for a control to behave as a button. However, as we'll see, if we stick to that, then we only get a bare-bones implementation of a button functionality.</li><li class='bbcode'>It must implement the IPostBackEventHandler interface, as this will be a control that posts back and raises events</li><li class='bbcode'>It must override all the rendering methods, and display the following:<ul class='bbcode'><li class='bbcode'>A table with one row</li><li class='bbcode'>Within the row, three cells</li><li class='bbcode'>the left cell must only display an image (the left part of the image for the button, up to the text)</li><li class='bbcode'>The middle cell must have an image as background and contain the button Text</li><li class='bbcode'>the right cell must display only an image (the part of the image that spans from the end of the text to the end of the button</li></ul></li><li class='bbcode'>It should be somehow themable, preferably through CSS</li></ul><br />So, I ended up building this control from scratch. It's far from complete, but it's a good stating point.<br /><br />Let's go over the thing. Mind you, the code-highlighter seems to be a bit broken (doesn't render round brackets nice), so I've attached the vb file at the end.<br /><br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Class</span> customButton</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Inherits</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">WebControl</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Implements</span> IButtonControl</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Implements</span> IPostBackEventHandler</div></li></ol></div></div><br />Nothing big here, except for the decision to inherit from WebControl, and not from Control. Since we're talking about ASP.NET, the WebControl base class seemed like a reasonable choice. here. This headermeans that we have some methods to override, namely the following from IButtonControl:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Event</span> Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">Click</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Event</span> <span style="color: #0600FF;">Command</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">CommandEventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0600FF;">Command</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CausesValidation<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">CausesValidation</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CommandArgument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">CommandArgument</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CommandName<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">CommandName</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> PostBackUrl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">PostBackUrl</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> Text<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">Text</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> ValidationGroup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">ValidationGroup</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br />From the IPostBackEventHandler, interface, we get one method:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> RaisePostBackEvent<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> eventArgument <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">IPostBackEventHandler</span>.<span style="color: #0000FF;">RaisePostBackEvent</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />As we'll soon see, these are far from enough to have a control that acts like a button in all situations.<br /><br />So then. Let's start off by giving all those properties inherited from IButtonControl some body. We follow the same pattern in all of them: define a protected member variable, and use the property as a getter-and-setter method for this variable. For example, we define<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected _CausesValidation <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> = <span style="color: #0600FF;">True</span></div></li></ol></div></div><br /><br />and fill out the equivalent property like so:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CausesValidation<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">CausesValidation</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Return _CausesValidation</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; _CausesValidation = value</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br /><br />All the other properties defined in IButtonControl are defined in the same manner.<br /><br />The RaisePostBackEvent is somewhat trickier. We do not have a single event that we might raise, but two: Click and Command. Let's see how a button behaves:<br /><br />Apparently, if a CommandName is defined, then the Command event is raised whenever we click on the button, otherwise the default Click event is raised. So, we create the following two helper methods:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected <span style="color: #FF8000;">Overridable</span> <span style="color: #0600FF;">Sub</span> OnClick<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> EventArgs<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">RaiseEvent</span> Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span>, e<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Protected <span style="color: #FF8000;">Overridable</span> <span style="color: #0600FF;">Sub</span> OnCommand<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> CommandEventArgs<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">RaiseEvent</span> <span style="color: #0600FF;">Command</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span>, e<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br /><br />And use them in the implemented method:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> RaisePostBackEvent<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> eventArgument <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">IPostBackEventHandler</span>.<span style="color: #0000FF;">RaisePostBackEvent</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CommandName &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #804040;">And</span> <span style="color: #804040;">Not</span> CommandName <span style="color: #FF8000;">Is</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #804040;">And</span> CommandName &lt;&gt; <span style="color: #FF8000;">String</span>.<span style="color: #FF8000;">Empty</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">Dim</span> e <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> CommandEventArgs<span style="color: #000000;">&#40;</span>CommandName, CommandArgument<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; OnCommand<span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; OnClick<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> EventArgs<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />What we just did, is we checked for a commandName and if that is present, we chose to raise the Command event (after we created a CommandEventArgs object to pass to the event handler). If no commanName exists, then this is probably not a command button, so we simply raise the click event.<br /><br />We're missing one major fanctionality of the Button control: the "onClientClick" property. Essentially, this allows the developer to assign arbitrary javascript code to the button click event, that's executed prior to the postback, and may even cancel it, if the last statement of the client-side code is a "return false" statement. Presumably, we'll simply concatenate the contents of this property with whatever javascript is needed for the postback, when we render the control. However, this is where things get a bit tricky; the developer may forget (or intentionally leave out) the trailing semicolon in the client-side code they assign to the control with this property. Therefore, it's up to the control to ensure that there is a semicolon at the end of the client-side code, if that code exists. To that end, we define a member to hold whatever the developer assigns,<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected _OnClientClick <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li></ol></div></div><br /> and create the relevant property: <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> OnClientClick<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Return _OnClientClick</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">Dim</span> <span style="color: #0600FF;">val</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #0600FF;">Trim</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">If</span> <span style="color: #0600FF;">Right</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #808080;">&quot;;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">val</span> &= <span style="color: #808080;">&quot;;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; _OnClientClick = <span style="color: #0600FF;">val</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br />What we are doing here, is that we make sure there is always a trailing semicolon, if something is set as client-side script to invoke upon clicking. This property will be used afterwards.<br /><br />This sums up all the properties and methods that define <em class='bbcode italic'>functionality</em>. We go on to <em class='bbcode italic'>displaying</em> the control. As I initially said, the "button" comprises of a table with one row and three columns. In other words, three cells one next to the other. The middle cell holds whatever text we wish to display on the button. These three cells should be themeable through CSS, therefore each needs a CSS class assigned to it. To that end, we define three plain properties for the CSS class name of each one of the three cells. As an example, here's the definition of the member variable and property for the CSS classname of the left cell:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected _CssClassLeft <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li></ol></div></div><br />and <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CssClassLeft<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Return _CssClassLeft</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; _CssClassLeft = value</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br />In the same way, we define the CssClassMiddle and CssClassRight member variables and properties. However, since it would be cumbersome to define each one of these whenever the developer needs a button, we've short-circuited the CssClass property inherited from WebControl. In essence, if the CssClass value is not an empty string, we expect to find the following three CSS classes:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CssClass &amp; <span style="color: #808080;">&quot;Left&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CssClass &amp; <span style="color: #808080;">&quot;Middle&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CssClass &amp; <span style="color: #808080;">&quot;Right&quot;</span></div></li></ol></div></div><br />To better show how we are using the CssClass property, we define an "alias" for it, called CssClassFamily:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CssClassFamily<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Return CssClass</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; CssClass = value</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br /><br />Time to focus on the rendering methods inherited from WebControl. We have five of them: RenderBeginTag, RenderContents, RenderEndTag, Render and AddAttributesToRender. <br /><br />We begin with the AddAttributesToRender. This is a very important method. The basic implementation gathers all attributes created by the .NET framework (such as the id of the control) based on the markup in the page where the control is defined, as well as all the extra attributes defined by the developer (such as extra styling, onmouseover and onmouseout actions, etc), and assigns them to the first tag that we render for the control (after we call this method of course). We need the basic functionality, so the first thing we do in the overriden AddAttributesToRender, is call the base method:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected Overrides <span style="color: #0600FF;">Sub</span> AddAttributesToRender<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> writer <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">HtmlTextWriter</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Style.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;width&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">MyBase</span>.<span style="color: #0000FF;">AddAttributesToRender</span><span style="color: #000000;">&#40;</span>writer<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Cellpadding</span>, <span style="color: #808080;">&quot;0&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Cellspacing</span>, <span style="color: #808080;">&quot;0&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Border</span>, <span style="color: #808080;">&quot;0&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddStyleAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;cursor&quot;</span>, <span style="color: #808080;">&quot;pointer&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Onclick</span>, <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">OnClientClick</span> &amp; Page.<span style="color: #0000FF;">ClientScript</span>.<span style="color: #0000FF;">GetPostBackEventReference</span><span style="color: #000000;">&#40;</span>_PostBackOptions<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />Note the very first line: all these attributes will go to the first tag that we open for rendering the control. That tag is the table tag. However, as we do not know whether the left and the right cell have some custom width defined in their CSS classes, we choose not to give the table a width, and so we remove any reference to width from the style collection of attributes passed to the control at design time. We'll be using the "Width" attribute later on though. <br /><br />So, after we pass on any attributes from the control design, we go on to define a few more. Cellpadding, cellspacing, and  border are pretty much self-explanatory. We also want to let the user (the viewer of the webpage) to know that this is a clickable element, so we turn the mouse into a hand whenever it is over the table. <br /><br />The last line deserves some extra comments. This is the line that defines what happens when someone clicks anywhere on the area defined by the table. Note that the first thing we put in the value part of the "onclick" property of the rendered table, is just the "OnClientClick"  property of the control, which is either the emtpy string, or javascript code ending in a single semicolon. We then add the output of the GetPostBackEventReference method of the ClientScript object of the page. The output of this method, is a string that's the javascript needed to do the postback to the server. Note that we are using a so-far unseen member: _PostBackOptions. This is defined as:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected _PostBackOptions <span style="color: #FF8000;">As</span> PostBackOptions</div></li></ol></div></div><br />and there is a method that instantiates it:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected <span style="color: #0600FF;">Sub</span> CreatePostBackOptions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _PostBackOptions = <span style="color: #FF8000;">New</span> PostBackOptions<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span>, _CommandArgument, _PostBackUrl, <span style="color: #0600FF;">False</span>, <span style="color: #0600FF;">False</span>, <span style="color: #0600FF;">True</span>, <span style="color: #0600FF;">True</span>, _CausesValidation, _ValidationGroup<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />Apparently, this method <em class='bbcode italic'>must</em> be called <em class='bbcode italic'><strong class='bbcode bold'>before</strong></em> calling the AddAttributesToRender, or we'll be facing a null reference exception.<br /><br />We are now ready to start rendering:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> Overrides <span style="color: #0600FF;">Sub</span> RenderBeginTag<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> writer <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">HtmlTextWriter</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreatePostBackOptions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddAttributesToRender<span style="color: #000000;">&#40;</span>writer<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Table</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'Table</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Tr</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'tr</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> LeftClass <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CssClassLeft &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; LeftClass = CssClassLeft</div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; LeftClass = CssClassFamily &amp; <span style="color: #808080;">&quot;Left&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0600FF;">Class</span>, LeftClass<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Id</span>, <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">ClientID</span> &amp; <span style="color: #808080;">&quot;_td1&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Td</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Me</span>.<span style="color: #0600FF;">Width</span>.<span style="color: #0000FF;">Value</span> &lt;= <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Me</span>.<span style="color: #0600FF;">Width</span> = Unit.<span style="color: #0000FF;">Pixel</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">100</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> MiddleClass <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CssClassMiddle &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; MiddleClass = CssClassMiddle</div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; MiddleClass = CssClassFamily &amp; <span style="color: #808080;">&quot;Middle&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0600FF;">Class</span>, MiddleClass<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0600FF;">Width</span>, <span style="color: #0600FF;">CStr</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">CInt</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span>.<span style="color: #0600FF;">Width</span>.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> &amp; <span style="color: #808080;">&quot;px&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Id</span>, <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">ClientID</span> &amp; <span style="color: #808080;">&quot;_td2&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Td</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td2</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br /><br />Despite its length, this method is pretty straight forward: we instantiate the postbackOptions object, gather all attributes for the table, and then open the table (thereby assigning all the attributes and styles). We go on to open the row, within which we'll place the three cells. <br /><br />Then, we check which classname we'll use for the left cell. If the developer has defined a custom CSS classname for the left cell, we use that, otherwise we use the generic family name+ "Left". We also apply a unique id to the cell, to facilitate any custom client side code that might affect the styling of this cell (e.g. a mouseover/mouseout function that changes the classname). Finally, we open -and close- the left cell. <br /><br />Then we do the same things about CSS classes with the middle cell, but we also apply an extra attribute: width (the one we deliberately took out of the attributes for the whole table). The reason we did this, is mainly better manageability: assigning the width to the entire table may cause some unwanted side-effects to the rendering of the three cells (e.g. squeeze them). Assigning the width to the middle cell only, however, is easier to grasp and view in terms of the final rendered page.<br /><br />The method ends, leaving three tags open: the enclosing table, the row and the middle cell.<br /><br />Filling up the middle cell is what the RenderContents method does:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected Overrides <span style="color: #0600FF;">Sub</span> RenderContents<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> output <span style="color: #FF8000;">As</span> HtmlTextWriter<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span style="color: #0600FF;">Write</span><span style="color: #000000;">&#40;</span>Text<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />It simply prints out the Text assigned to the button.<br /><br />Finally, the RenderEndTag method closes the middle tag, renders the right tag (performing the same checks and assigning the same types of attributes as with the left tag), and then goes on to close the row and the table tag:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> Overrides <span style="color: #0600FF;">Sub</span> RenderEndTag<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> writer <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">HtmlTextWriter</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td2</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> RightClass <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CssClassRight &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; RightClass = CssClassRight</div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; RightClass = CssClassFamily &amp; <span style="color: #808080;">&quot;Right&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0600FF;">Class</span>, RightClass<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Id</span>, <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">ClientID</span> &amp; <span style="color: #808080;">&quot;_td3&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Td</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td3</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'tr</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'table</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />Finaly, the Render method does just that: render the control.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected Overrides <span style="color: #0600FF;">Sub</span> Render<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> writer <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">HtmlTextWriter</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #804040;">Not</span> Page <span style="color: #FF8000;">Is</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Page.<span style="color: #0000FF;">VerifyRenderingInServerForm</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">MyBase</span>.<span style="color: #0000FF;">Render</span><span style="color: #000000;">&#40;</span>writer<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />Note that before we render, we check that the button is within a form with a runat="server" attribute. The actual rendering is left to the base class render method.<br /><br />All in all, the button does its job. It has quite a few things that need imroving though: Here's a few things I can think of off the top of my head:<br /><ul class='bbcode'><li class='bbcode'>Allow for individual styling of each of the three cells, not just CSS classes</li><li class='bbcode'>There is no management for any mouse-over state, except for the mouse turning into a hand. It would be nice if the button handled the mouseover and mouseout events intrinsically by changing its style (i.e. change CSS class names). Something along the line of this: <br /><a class='bbcode' href='http://www.aspfree.com/c/a/ASP.NET/ASPNET-Custom-Server-Controls-Cute-Image-Hover-Button/' rel='external' >http://www.aspfree.com/c/a/ASP.NET/ASPNET-Custom-Server-Controls-Cute-Image-Hover-Button/</a> </li></ul><br />]]></description>
<content:encoded><![CDATA[At work, I found a frequently occurring scenario: the designers would come up with some button design that's just impossible to represent in an efficient manner with ASP.NET 2.0 controls.<br />A simple example is as follows:<br /><img src='http://yiannis.vavouranakis.gr/myblog/e107_images/newspost_images/aquabutton.png' class='bbcode' alt=''  /><br />This is really a background image of an aqua-style button, and a text rendered on top of it. <br /><br />You'd say that an imageButton control could easily tackle this one, and in general I'd agree. Some markup like the following would be sufficient:<br /><br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;asp:ImageButton ID=&quot;button&quot; runat=&quot;server&quot; ImageUrl=&quot;~/PathToImages/ButtonImage.png&quot; /&gt;</div></li></ol></div></div><br /><br />However, what happens if the site is multilingual (something that happens quite a lot)? Then you'd have to resort to a solution, where the ImageUrl for the ImageButton is not set declaratively, but programmatically. So, instead of the above, you'd have<br /><br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;asp:ImageButton ID=&quot;button&quot; runat=&quot;server&quot; /&gt;</div></li></ol></div></div><br /><br />and then in the codebehind page, you'd have to set the image url explicitly, depending on the language:<br /><br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">Dim</span> PageLanguage <span style="color: #FF8000;">as</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">'Code that sets the PageLanguage variable to the current language</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">button.<span style="color: #0000FF;">ImageUrl</span>=<span style="color: #808080;">&quot;~/PathToImages/ButtonImage&quot;</span> &amp; PageLanguage &amp; <span style="color: #808080;">&quot;png&quot;</span></div></li></ol></div></div><br /><br />You'd say that this is not really much, but imagine having 3, 4 or more buttons on the same page. Even worse, imagine having buttons in a Repeater (for Item selection) or in a gridView. Things can get pretty messy quite fast.<br /><br />Then there's another way of doing it. Actually have an imageButton without the text, and a linkbutton with the text, and posittion one on top of the other. This tackles the problem of having to define all the states of the "button" programmatically, but adds extra complexity with the event handlers and such; the two "buttons" must actually use the same method for their click event. Moreover, it won't always be easy to position one on top of the other.<br /><br />At first, I followed the former solution. But since I kept seeing the same thing happen over and over again, I thought that there must be another way of doing this. After a bit of google digging that resulted next to nothing, and quite a few trial-and-error attempts, I ended up with the following specs for my solution:<br /><ul class='bbcode'><li class='bbcode'>It must implement the IButtonControl interface. This goes without saying, as the IButtonControl interface exists purely as a guideline for control developers, as to which methods <em class='bbcode italic'>must</em> be implemented for a control to behave as a button. However, as we'll see, if we stick to that, then we only get a bare-bones implementation of a button functionality.</li><li class='bbcode'>It must implement the IPostBackEventHandler interface, as this will be a control that posts back and raises events</li><li class='bbcode'>It must override all the rendering methods, and display the following:<ul class='bbcode'><li class='bbcode'>A table with one row</li><li class='bbcode'>Within the row, three cells</li><li class='bbcode'>the left cell must only display an image (the left part of the image for the button, up to the text)</li><li class='bbcode'>The middle cell must have an image as background and contain the button Text</li><li class='bbcode'>the right cell must display only an image (the part of the image that spans from the end of the text to the end of the button</li></ul></li><li class='bbcode'>It should be somehow themable, preferably through CSS</li></ul><br />So, I ended up building this control from scratch. It's far from complete, but it's a good stating point.<br /><br />Let's go over the thing. Mind you, the code-highlighter seems to be a bit broken (doesn't render round brackets nice), so I've attached the vb file at the end.<br /><br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Class</span> customButton</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Inherits</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">WebControl</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Implements</span> IButtonControl</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Implements</span> IPostBackEventHandler</div></li></ol></div></div><br />Nothing big here, except for the decision to inherit from WebControl, and not from Control. Since we're talking about ASP.NET, the WebControl base class seemed like a reasonable choice. here. This headermeans that we have some methods to override, namely the following from IButtonControl:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Event</span> Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">Click</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Event</span> <span style="color: #0600FF;">Command</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">CommandEventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0600FF;">Command</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CausesValidation<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">CausesValidation</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CommandArgument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">CommandArgument</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CommandName<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">CommandName</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> PostBackUrl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">PostBackUrl</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> Text<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">Text</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> ValidationGroup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">ValidationGroup</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br />From the IPostBackEventHandler, interface, we get one method:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> RaisePostBackEvent<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> eventArgument <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">IPostBackEventHandler</span>.<span style="color: #0000FF;">RaisePostBackEvent</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />As we'll soon see, these are far from enough to have a control that acts like a button in all situations.<br /><br />So then. Let's start off by giving all those properties inherited from IButtonControl some body. We follow the same pattern in all of them: define a protected member variable, and use the property as a getter-and-setter method for this variable. For example, we define<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected _CausesValidation <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> = <span style="color: #0600FF;">True</span></div></li></ol></div></div><br /><br />and fill out the equivalent property like so:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CausesValidation<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">IButtonControl</span>.<span style="color: #0000FF;">CausesValidation</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Return _CausesValidation</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; _CausesValidation = value</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br /><br />All the other properties defined in IButtonControl are defined in the same manner.<br /><br />The RaisePostBackEvent is somewhat trickier. We do not have a single event that we might raise, but two: Click and Command. Let's see how a button behaves:<br /><br />Apparently, if a CommandName is defined, then the Command event is raised whenever we click on the button, otherwise the default Click event is raised. So, we create the following two helper methods:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected <span style="color: #FF8000;">Overridable</span> <span style="color: #0600FF;">Sub</span> OnClick<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> EventArgs<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">RaiseEvent</span> Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span>, e<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Protected <span style="color: #FF8000;">Overridable</span> <span style="color: #0600FF;">Sub</span> OnCommand<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> CommandEventArgs<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">RaiseEvent</span> <span style="color: #0600FF;">Command</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span>, e<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br /><br />And use them in the implemented method:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> RaisePostBackEvent<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> eventArgument <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">Implements</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">IPostBackEventHandler</span>.<span style="color: #0000FF;">RaisePostBackEvent</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CommandName &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #804040;">And</span> <span style="color: #804040;">Not</span> CommandName <span style="color: #FF8000;">Is</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #804040;">And</span> CommandName &lt;&gt; <span style="color: #FF8000;">String</span>.<span style="color: #FF8000;">Empty</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">Dim</span> e <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> CommandEventArgs<span style="color: #000000;">&#40;</span>CommandName, CommandArgument<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; OnCommand<span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; OnClick<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> EventArgs<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />What we just did, is we checked for a commandName and if that is present, we chose to raise the Command event (after we created a CommandEventArgs object to pass to the event handler). If no commanName exists, then this is probably not a command button, so we simply raise the click event.<br /><br />We're missing one major fanctionality of the Button control: the "onClientClick" property. Essentially, this allows the developer to assign arbitrary javascript code to the button click event, that's executed prior to the postback, and may even cancel it, if the last statement of the client-side code is a "return false" statement. Presumably, we'll simply concatenate the contents of this property with whatever javascript is needed for the postback, when we render the control. However, this is where things get a bit tricky; the developer may forget (or intentionally leave out) the trailing semicolon in the client-side code they assign to the control with this property. Therefore, it's up to the control to ensure that there is a semicolon at the end of the client-side code, if that code exists. To that end, we define a member to hold whatever the developer assigns,<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected _OnClientClick <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li></ol></div></div><br /> and create the relevant property: <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> OnClientClick<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Return _OnClientClick</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">Dim</span> <span style="color: #0600FF;">val</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #0600FF;">Trim</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">If</span> <span style="color: #0600FF;">Right</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">val</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #808080;">&quot;;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; &nbsp; &nbsp; <span style="color: #0600FF;">val</span> &= <span style="color: #808080;">&quot;;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #0600FF;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; _OnClientClick = <span style="color: #0600FF;">val</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br />What we are doing here, is that we make sure there is always a trailing semicolon, if something is set as client-side script to invoke upon clicking. This property will be used afterwards.<br /><br />This sums up all the properties and methods that define <em class='bbcode italic'>functionality</em>. We go on to <em class='bbcode italic'>displaying</em> the control. As I initially said, the "button" comprises of a table with one row and three columns. In other words, three cells one next to the other. The middle cell holds whatever text we wish to display on the button. These three cells should be themeable through CSS, therefore each needs a CSS class assigned to it. To that end, we define three plain properties for the CSS class name of each one of the three cells. As an example, here's the definition of the member variable and property for the CSS classname of the left cell:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected _CssClassLeft <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> = <span style="color: #808080;">&quot;&quot;</span></div></li></ol></div></div><br />and <br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CssClassLeft<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Return _CssClassLeft</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; _CssClassLeft = value</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br />In the same way, we define the CssClassMiddle and CssClassRight member variables and properties. However, since it would be cumbersome to define each one of these whenever the developer needs a button, we've short-circuited the CssClass property inherited from WebControl. In essence, if the CssClass value is not an empty string, we expect to find the following three CSS classes:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CssClass &amp; <span style="color: #808080;">&quot;Left&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CssClass &amp; <span style="color: #808080;">&quot;Middle&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CssClass &amp; <span style="color: #808080;">&quot;Right&quot;</span></div></li></ol></div></div><br />To better show how we are using the CssClass property, we define an "alias" for it, called CssClassFamily:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> CssClassFamily<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Return CssClass</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Get</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; CssClass = value</div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Set</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #FF8000;">Property</span></div></li></ol></div></div><br /><br />Time to focus on the rendering methods inherited from WebControl. We have five of them: RenderBeginTag, RenderContents, RenderEndTag, Render and AddAttributesToRender. <br /><br />We begin with the AddAttributesToRender. This is a very important method. The basic implementation gathers all attributes created by the .NET framework (such as the id of the control) based on the markup in the page where the control is defined, as well as all the extra attributes defined by the developer (such as extra styling, onmouseover and onmouseout actions, etc), and assigns them to the first tag that we render for the control (after we call this method of course). We need the basic functionality, so the first thing we do in the overriden AddAttributesToRender, is call the base method:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected Overrides <span style="color: #0600FF;">Sub</span> AddAttributesToRender<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> writer <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">HtmlTextWriter</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Style.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;width&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">MyBase</span>.<span style="color: #0000FF;">AddAttributesToRender</span><span style="color: #000000;">&#40;</span>writer<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Cellpadding</span>, <span style="color: #808080;">&quot;0&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Cellspacing</span>, <span style="color: #808080;">&quot;0&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Border</span>, <span style="color: #808080;">&quot;0&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddStyleAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;cursor&quot;</span>, <span style="color: #808080;">&quot;pointer&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Onclick</span>, <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">OnClientClick</span> &amp; Page.<span style="color: #0000FF;">ClientScript</span>.<span style="color: #0000FF;">GetPostBackEventReference</span><span style="color: #000000;">&#40;</span>_PostBackOptions<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />Note the very first line: all these attributes will go to the first tag that we open for rendering the control. That tag is the table tag. However, as we do not know whether the left and the right cell have some custom width defined in their CSS classes, we choose not to give the table a width, and so we remove any reference to width from the style collection of attributes passed to the control at design time. We'll be using the "Width" attribute later on though. <br /><br />So, after we pass on any attributes from the control design, we go on to define a few more. Cellpadding, cellspacing, and  border are pretty much self-explanatory. We also want to let the user (the viewer of the webpage) to know that this is a clickable element, so we turn the mouse into a hand whenever it is over the table. <br /><br />The last line deserves some extra comments. This is the line that defines what happens when someone clicks anywhere on the area defined by the table. Note that the first thing we put in the value part of the "onclick" property of the rendered table, is just the "OnClientClick"  property of the control, which is either the emtpy string, or javascript code ending in a single semicolon. We then add the output of the GetPostBackEventReference method of the ClientScript object of the page. The output of this method, is a string that's the javascript needed to do the postback to the server. Note that we are using a so-far unseen member: _PostBackOptions. This is defined as:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected _PostBackOptions <span style="color: #FF8000;">As</span> PostBackOptions</div></li></ol></div></div><br />and there is a method that instantiates it:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected <span style="color: #0600FF;">Sub</span> CreatePostBackOptions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _PostBackOptions = <span style="color: #FF8000;">New</span> PostBackOptions<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span>, _CommandArgument, _PostBackUrl, <span style="color: #0600FF;">False</span>, <span style="color: #0600FF;">False</span>, <span style="color: #0600FF;">True</span>, <span style="color: #0600FF;">True</span>, _CausesValidation, _ValidationGroup<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />Apparently, this method <em class='bbcode italic'>must</em> be called <em class='bbcode italic'><strong class='bbcode bold'>before</strong></em> calling the AddAttributesToRender, or we'll be facing a null reference exception.<br /><br />We are now ready to start rendering:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> Overrides <span style="color: #0600FF;">Sub</span> RenderBeginTag<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> writer <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">HtmlTextWriter</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreatePostBackOptions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddAttributesToRender<span style="color: #000000;">&#40;</span>writer<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Table</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'Table</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Tr</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'tr</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> LeftClass <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CssClassLeft &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; LeftClass = CssClassLeft</div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; LeftClass = CssClassFamily &amp; <span style="color: #808080;">&quot;Left&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0600FF;">Class</span>, LeftClass<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Id</span>, <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">ClientID</span> &amp; <span style="color: #808080;">&quot;_td1&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Td</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td1</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Me</span>.<span style="color: #0600FF;">Width</span>.<span style="color: #0000FF;">Value</span> &lt;= <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Me</span>.<span style="color: #0600FF;">Width</span> = Unit.<span style="color: #0000FF;">Pixel</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">100</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> MiddleClass <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CssClassMiddle &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; MiddleClass = CssClassMiddle</div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; MiddleClass = CssClassFamily &amp; <span style="color: #808080;">&quot;Middle&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0600FF;">Class</span>, MiddleClass<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0600FF;">Width</span>, <span style="color: #0600FF;">CStr</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">CInt</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span>.<span style="color: #0600FF;">Width</span>.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> &amp; <span style="color: #808080;">&quot;px&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Id</span>, <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">ClientID</span> &amp; <span style="color: #808080;">&quot;_td2&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Td</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td2</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br /><br />Despite its length, this method is pretty straight forward: we instantiate the postbackOptions object, gather all attributes for the table, and then open the table (thereby assigning all the attributes and styles). We go on to open the row, within which we'll place the three cells. <br /><br />Then, we check which classname we'll use for the left cell. If the developer has defined a custom CSS classname for the left cell, we use that, otherwise we use the generic family name+ "Left". We also apply a unique id to the cell, to facilitate any custom client side code that might affect the styling of this cell (e.g. a mouseover/mouseout function that changes the classname). Finally, we open -and close- the left cell. <br /><br />Then we do the same things about CSS classes with the middle cell, but we also apply an extra attribute: width (the one we deliberately took out of the attributes for the whole table). The reason we did this, is mainly better manageability: assigning the width to the entire table may cause some unwanted side-effects to the rendering of the three cells (e.g. squeeze them). Assigning the width to the middle cell only, however, is easier to grasp and view in terms of the final rendered page.<br /><br />The method ends, leaving three tags open: the enclosing table, the row and the middle cell.<br /><br />Filling up the middle cell is what the RenderContents method does:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected Overrides <span style="color: #0600FF;">Sub</span> RenderContents<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> output <span style="color: #FF8000;">As</span> HtmlTextWriter<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span style="color: #0600FF;">Write</span><span style="color: #000000;">&#40;</span>Text<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />It simply prints out the Text assigned to the button.<br /><br />Finally, the RenderEndTag method closes the middle tag, renders the right tag (performing the same checks and assigning the same types of attributes as with the left tag), and then goes on to close the row and the table tag:<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> Overrides <span style="color: #0600FF;">Sub</span> RenderEndTag<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> writer <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">HtmlTextWriter</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td2</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">Dim</span> RightClass <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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> CssClassRight &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; RightClass = CssClassRight</div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">Else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; RightClass = CssClassFamily &amp; <span style="color: #808080;">&quot;Right&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0600FF;">Class</span>, RightClass<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">AddAttribute</span><span style="color: #000000;">&#40;</span>HtmlTextWriterAttribute.<span style="color: #0000FF;">Id</span>, <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">ClientID</span> &amp; <span style="color: #808080;">&quot;_td3&quot;</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderBeginTag</span><span style="color: #000000;">&#40;</span>HtmlTextWriterTag.<span style="color: #0000FF;">Td</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'td3</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'tr</span></div></li><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">RenderEndTag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">'table</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />Finaly, the Render method does just that: render the control.<br /><div class='code_highlight code-box' style='unicode-bidi: embed; direction: ltr'><div class="vbnet" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> &nbsp; &nbsp; &nbsp; &nbsp;Protected Overrides <span style="color: #0600FF;">Sub</span> Render<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> writer <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">HtmlTextWriter</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #804040;">Not</span> Page <span style="color: #FF8000;">Is</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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; Page.<span style="color: #0000FF;">VerifyRenderingInServerForm</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Me</span><span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">If</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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: #FF8000;">MyBase</span>.<span style="color: #0000FF;">Render</span><span style="color: #000000;">&#40;</span>writer<span style="color: #000000;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; 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;">End</span> <span style="color: #0600FF;">Sub</span></div></li></ol></div></div><br />Note that before we render, we check that the button is within a form with a runat="server" attribute. The actual rendering is left to the base class render method.<br /><br />All in all, the button does its job. It has quite a few things that need imroving though: Here's a few things I can think of off the top of my head:<br /><ul class='bbcode'><li class='bbcode'>Allow for individual styling of each of the three cells, not just CSS classes</li><li class='bbcode'>There is no management for any mouse-over state, except for the mouse turning into a hand. It would be nice if the button handled the mouseover and mouseout events intrinsically by changing its style (i.e. change CSS class names). Something along the line of this: <br /><a class='bbcode' href='http://www.aspfree.com/c/a/ASP.NET/ASPNET-Custom-Server-Controls-Cute-Image-Hover-Button/' rel='external' >http://www.aspfree.com/c/a/ASP.NET/ASPNET-Custom-Server-Controls-Cute-Image-Hover-Button/</a> </li></ul><br />]]></content:encoded>
<category domain='http://yiannis.vavouranakis.gr/myblog/news.php?cat.2'>Tech related</category>
<dc:creator>yiangos</dc:creator>
<pubDate>Sat, 16 May 2009 22:24:05 +0300</pubDate>
<guid isPermaLink="true">http://yiannis.vavouranakis.gr/myblog/news.php?item.37.2</guid>
</item>


				</channel>
				</rss>
