tag:blogger.com,1999:blog-82528671495159686742024-03-28T20:29:56.081-07:00Coding EverythingA curious endeavor to catalogue and document the wealth
of programming knowledge available on the internet
and a humble attempt to contribute to it.Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.comBlogger29125tag:blogger.com,1999:blog-8252867149515968674.post-83993006067232008402015-08-31T13:29:00.002-07:002015-09-01T06:57:15.689-07:00Using Signed Macros with MS Office<p>Signing excel apps via a digital certificate can help guarantee that no one has tampered with the code and that what you are executing is what was originally written by the original signer. Further, it helps lock down your security policy. By only running signed code and only importing certificates from people you trust, you can prevent a situation where an an excel document can hijack your whole computer (<a href="http://security.stackexchange.com/a/98226/24374">because it can</a>)</p>
<p>If you rely on VBA solutions within your organization, make sure you sign and then import certificates for <em>all</em> projects.</p>
<h3 id="create-certificate">Sign with Certificate</h3>
<p>When creating a VBA project that uses Macros, you can easily <a href="https://support.office.com/en-za/article/Digitally-sign-your-macro-project-956e9cc8-bbf6-4365-8bfa-98505ecd1c01">sign the project</a> with a self signed certificate using the following steps:</p>
<ol>
<li>If you don’t already have one, you’ll need to create a self-signed certificate on your computer. If you do have one, you can skip to <a href="#sign-3">step 3</a>. If not, go to the Start Menu > All Programs > Microsoft Office > Microsoft Office Tools > and open <strong>Digital Certificate for VBA Projects</strong> <br>
<ul><li><img src="http://i.imgur.com/nRNSLfd.png" alt="1" title=""></li></ul></li>
<li>Enter a name for your certificate and Click OK <br>
<ul><li><img src="http://i.imgur.com/EOYyY5k.png" alt="2" title=""></li></ul></li>
<li><span id="sign-3">Open</span> up your VBA project in Excel, Word, Outlook (etc) and hit <kbd>Alt</kbd> + <kbd>F11</kbd> to open up the VBA Project and go to Tools > Digital Signature <br>
<ul><li><img src="http://i.imgur.com/0oH1DyD.png" alt="3" title=""></li></ul></li>
<li>On the Digital Signature Menu, Click “Choose” <br>
<ul><li><img src="http://i.imgur.com/MlQBet2.png" alt="4" title=""></li></ul></li>
<li>You should see the Certificate we just created. Select that and hit OK <br>
<ul><li><img src="http://i.imgur.com/fgXW6iL.png" alt="5" title=""></li></ul></li>
</ol>
<h3 id="import-certificate">Import Certificate From Someone Else</h3>
<p>When the file is opened on another machine, it won’t recognize the original certificate which only exists on the original author’s machine. Using the following steps we can <a href="http://domainwebcenter.com/?p=868">import the certificate on the client’s machine</a>.</p>
<blockquote>
<p><strong>Disclaimer</strong>: You should <em>only</em> perform these steps when you are absolutely certain that the file has originated from someone you trust to execute any commands on your machine without your knowledge.</p>
</blockquote>
<ol>
<li>Open the Excel File with Macros and click “Options” <br>
<ul><li><img src="http://i.imgur.com/3geFEuw.png" alt="1" title=""></li></ul></li>
<li>Click “Show Signature Details” <br>
<ul><li><img src="http://i.imgur.com/4VoNak1.png" alt="2" title=""></li></ul></li>
<li>Click “View Certificate” <br>
<ul><li><img src="http://i.imgur.com/1F8gRFH.png" alt="3" title=""></li></ul></li>
<li>Click “Install Certificate” <br>
<ul><li><img src="http://i.imgur.com/rdvbMHw.png" alt="4" title=""></li></ul></li>
<li>Install Wizard Will Open <br>
<ul><li><img src="http://i.imgur.com/TYHxYkz.png" alt="5" title=""></li></ul></li>
<li>Select “Place all certificates in the following store” and Click “Browse” <br>
<ul><li><img src="http://i.imgur.com/J7cT2HW.png" alt="6" title=""></li></ul></li>
<li>Select “Trusted Root Certification Authorities” <br>
<ul><li><img src="http://i.imgur.com/Y1EJ58x.png" alt="7" title=""></li></ul></li>
<li>Click OK / Next several times <br>
<ul><li><img src="http://i.imgur.com/DEQiT8Y.png" alt="8" title=""></li></ul></li>
<li>Accept the Security Warning to Install the Certificate <br>
<ul><li><img src="http://i.imgur.com/6CoGgsR.png" alt="9" title=""> </li></ul></li>
<li>On the original Dialog, Select “Trust all documents from this publisher” and Click OK <br>
<ul><li><img src="http://i.imgur.com/ddjhxHp.png" alt="10" title=""></li></ul></li>
</ol>
<h3 id="manage-certificate">Manage Certificates</h3>
<p>If you want to go in and <a href="http://www.wiseowl.co.uk/blog/s241/deleting_digital_certificates.htm">make changes to your certificates directly</a>, you can view and edit all of the certs on your current system with the following steps.</p>
<ol>
<li>Hit <kbd>Win</kbd> + <kbd>R</kbd> to open up the Run Dialog</li>
<li>Type “certmgr.msc” and Click OK <br>
<ul><li><img src="http://i.imgur.com/ieyHaOL.png" alt="1" title=""></li></ul></li>
<li>You should see the certificate we added in the Personal/Certificates folder <br>
<ul><li><img src="http://i.imgur.com/nxwZYwF.png" alt="2" title=""></li></ul></li>
</ol>
<p><a href="http://answers.microsoft.com/en-us/office/forum/office_2010-excel/how-to-delete-self-signed-digital-certificate/b9dc7fa9-362e-4c6c-8c73-879236b62ae9?auth=1#ThreadAnswers"><strong>Alternatively</strong></a>: You can also load your Computer’s Management Console by going to Start > Run mmc and then going to File > Add Snap In and then selecting Certificates.</p>
<p>And that’s it! Adding certificates to your projects is a great way to keep security in mind while providing some sort of automation within Office and other products.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com58tag:blogger.com,1999:blog-8252867149515968674.post-47468351151351187042015-08-06T10:18:00.000-07:002015-08-10T12:01:43.969-07:00Introduction to Linq To Datatables<p>LINQ to objects will work on any object that implements <a href="https://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx"><code>IEnumerable</code></a> (the same interface that allows an object to be used in a For Each loop). Datasets are objects, but they don’t implement IEnumerable by default, so you’ll need to first add a reference to a library that adds some extension methods that wrap DataTable and allow it to be enumerated.</p>
<p>Just go to Project > References > Add > .NET > <strong>System.Data.DataSetExtensions</strong> </p>
<p><img src="http://i.imgur.com/QmLYZDU.png" alt="DataSetExtensions" title=""></p>
<p>In LINQ, you always start the query with the <code>From</code> statement</p>
<pre class="prettyprint"><code class="language-vb hljs vbnet"><span class="hljs-keyword">From</span> a <span class="hljs-keyword">In</span> b </code></pre>
<p>This gives you access to each item that is part of the <code>B</code> collection in the same way that a for loop does</p>
<pre class="prettyprint"><code class="language-vb hljs vbnet"><span class="hljs-keyword">For</span> <span class="hljs-keyword">Each</span> a <span class="hljs-keyword">In</span> b</code></pre>
<p>For the rest of the for loop (or linq query), you can just refer to <code>a</code> and it’s automatically populated as it iterates over the collection.</p>
<p>If you want to loop over a <code>DataTable</code>, you need to create an enumerable table by calling <code>DataTable.AsEnumerable()</code> in order to convert the collection of rows into one that is, well, enumerable. When you loop over a DataTable, you’ll be holding a row object, so you can generally query it in the same way that you otherwise would</p>
<p>If I have a <code>DataRow</code> object, I can get the values like this:</p>
<pre class="prettyprint"><code class="language-vb hljs vbnet"><span class="hljs-comment">'longhand</span>
<span class="hljs-keyword">Dim</span> name = myRow.Field(<span class="hljs-keyword">Of</span> <span class="hljs-built_in">String</span>)(<span class="hljs-string">"Name"</span>)
<span class="hljs-comment">'shorthand</span>
<span class="hljs-keyword">Dim</span> name = myRow(<span class="hljs-string">"Name"</span>)</code></pre>
<p>Let’s say that we have the following SQL Query that we’d like to do in .NET with Datatables instead:</p>
<pre class="prettyprint"><code class="language-sql hljs "><span class="hljs-operator"><span class="hljs-keyword">SELECT</span> p.ID, p.Name, e.SSN
<span class="hljs-keyword">FROM</span> People p
<span class="hljs-keyword">JOIN</span> ExtendedInfo e
<span class="hljs-keyword">ON</span> p.ID = e.PersonID</span></code></pre>
<p>LINQ (especially using it’s Query Syntax) is intended to function a lot like SQL. So here’s what it would look like in .NET:</p>
<pre class="prettyprint"><code class="language-vb hljs vbscript"><span class="hljs-keyword">Dim</span> query = From person <span class="hljs-keyword">In</span> people.AsEnumerable
<span class="hljs-built_in">Join</span> extra <span class="hljs-keyword">In</span> extendedInfo.AsEnumerable()
<span class="hljs-keyword">On</span> person(<span class="hljs-string">"ID"</span>) Equals extra(<span class="hljs-string">"PersonID"</span>)
<span class="hljs-keyword">Select</span> <span class="hljs-keyword">New</span> <span class="hljs-keyword">With</span> {
.ID = person(<span class="hljs-string">"ID"</span>),
.Name = person(<span class="hljs-string">"Name"</span>),
.SSN = extra(<span class="hljs-string">"SSN"</span>)
}</code></pre>
<p>The syntax on the bottom just creates a new <em>anonymous</em> object and then selects it. We can <code>Select</code> any object available from the query (i.e. <code>person</code>) or we can create our own on the fly and select that</p>
<p>If you’re unfamiliar with anonymous objects, the syntax may look unfamiliar, but it’s a pretty basic concept. One rarely used feature you might not know about is that if we want to initialize a <code>Person</code> class (which has a <code>Name</code> Property), we can set the name property directly from the initializer using a <code>With {}</code> statement like this:</p>
<pre class="prettyprint"><code class="language-vb hljs vbscript"><span class="hljs-keyword">Dim</span> typedPerson = <span class="hljs-keyword">New</span> Person <span class="hljs-keyword">With</span> {
.Name = <span class="hljs-string">"Angelic"</span>
}</code></pre>
<p>If we don’t have a Person class to stuff our data in, or we just want to do it on the fly, we can do the same thing by omitting the class name and then just passing in any properties we want our new class to have. </p>
<blockquote>
<p><strong>Note</strong>: The compiler will actually create a class for you behind the scenes that has all the properties you’ve specified, but we don’t have a name for it - thus it’s <em>anonymous</em>.</p>
</blockquote>
<pre class="prettyprint"><code class="language-vb hljs vbscript"><span class="hljs-comment">'Anonymous Person</span>
<span class="hljs-keyword">Dim</span> anonPerson = <span class="hljs-keyword">New</span> <span class="hljs-keyword">With</span> {
.Name = <span class="hljs-string">"Angelic"</span>
}</code></pre>
<p>Now we have a query that, once evaluated, will return the joined tables into an enumerable collection of anonymous objects. If we want to convert that back into a new Datatable it’s as simple as calling <code>CopyToDataTable</code> on the query result:</p>
<pre class="prettyprint"><code class="language-vb hljs fix"><span class="hljs-attribute">Dim newTable </span>=<span class="hljs-string"> query.CopyToDataTable()</span></code></pre>
<p><strong>However</strong>, when you do this right now, you’ll get the following error:</p>
<blockquote>
<p><code>CopyToDataTable</code> is not a member of <code>System.Collections.Generic.IEnumerable(Of <anonymous type>)</code></p>
</blockquote>
<p><strong>Okay, I lied</strong> - It <em>would</em> be simple if the enumerable was of type <code>DataRow</code>. But not to fear, as written by this MSDN article on <a href="https://msdn.microsoft.com/en-us/library/bb669096.aspx">How to Implement CopyToDataTable<t> Where the Generic Type T Is Not a DataRow</t></a>, you just need to include some extra extension methods that don’t ship with DataSetExtensions .</p>
<p>Just add <a href="https://gist.github.com/KyleMit/88833eb179f9ec0e4b48">this file</a> to your project anywhere.</p>
<p>Now the code should compile and work fine.</p>
<p>You can test the result by running:</p>
<pre class="prettyprint"><code class="language-vb hljs vbnet"><span class="hljs-keyword">For</span> <span class="hljs-keyword">Each</span> row <span class="hljs-keyword">In</span> newTable.AsEnumerable()
Console.WriteLine(<span class="hljs-string">"ID: "</span> & row(<span class="hljs-string">"ID"</span>) & <span class="hljs-string">" / "</span> &
<span class="hljs-string">"Name: "</span> & row(<span class="hljs-string">"Name"</span>) & <span class="hljs-string">" / "</span> &
<span class="hljs-string">"SSN: "</span> & row(<span class="hljs-string">"SSN"</span>))
<span class="hljs-keyword">Next</span></code></pre>
<p>If you’d like to run this code immediately without even opening up Visual Studio, you can see a <a href="https://dotnetfiddle.net/OA7SPF">working demo in dotNetFiddle here</a>.</p>
<p>For further reading, there is a TON of great information on MSDN under</p>
<p><a href="https://msdn.microsoft.com/en-us/library/bb386977.aspx">LINQ to DataSet</a> - <a href="https://msdn.microsoft.com/en-us/library/bb387003.aspx">Getting Started</a> | <a href="https://msdn.microsoft.com/en-us/library/bb387004.aspx">Programming Guide</a></p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com18tag:blogger.com,1999:blog-8252867149515968674.post-3540953192457351402015-05-12T11:01:00.000-07:002015-05-12T11:05:08.199-07:00Excel: Repeat Top Row on Every Page<p>You guys might already know this, but I just learned something new about excel.</p>
<ol>
<li>On the “Page Layout” Ribbon, click the “Print Titles”. <br>
<img src="http://i.imgur.com/5t2YZcJ.png" alt="print tiles" title=""></li>
<li>Click “Rows to repeat at top,” and click the icon that looks like a spreadsheet. <br>
<img src="http://i.imgur.com/YgcfnTn.png" alt="page setup" title=""></li>
<li>Use your cursor to click and select the row you wish to appear at the top of every page.</li>
<li>Press the Enter and Click OK</li>
</ol>
<p>You can use print preview to show you what it will look like.</p>
<p><strong>P.S.</strong> For long printed documents, don’t forget to also add page numbers:</p>
<ol>
<li>On the “Insert” Ribbon, click “Header & Footer” <br>
<img src="http://i.imgur.com/Yhnxs4o.png" alt="header & footer" title=""></li>
<li><p>Click on the Header or Footer area in which you’d like the messages to appear. This should open up the Header & Footer tools Context Sensitive Designer Ribbon. <br>
<img src="http://i.imgur.com/0UNmdSw.png" alt="footer bar" title=""> </p>
<p><img src="http://i.imgur.com/EHmcwi3.png" alt="header & footer tools" title=""></p></li>
<li><p>Then select Footer and insert any template or design you own <br>
<img src="http://i.imgur.com/V5vu9W5.png" alt="footer tool" title=""></p>
<p>This should insert the insert the text “Page &[Page] of &[Pages]” where “&[field]” will be replaced automatically</p></li>
<li><p>You can also choose from any of the following fields:</p>
<table>
<thead>
<tr>
<th>Element Name</th>
<th>Text</th>
</tr>
</thead>
<tbody><tr>
<td>Page Number</td>
<td><code>&[Page]</code></td>
</tr>
<tr>
<td>Number of Pages</td>
<td><code>&[Pages]</code></td>
</tr>
<tr>
<td>Current Time</td>
<td><code>&[Time]</code></td>
</tr>
<tr>
<td>Sheet Name</td>
<td><code>&[Tab]</code></td>
</tr>
<tr>
<td>File Name</td>
<td><code>&[File]</code></td>
</tr>
<tr>
<td>File Path</td>
<td><code>&[Path]&[File]</code></td>
</tr>
</tbody></table>
</li>
</ol>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com4tag:blogger.com,1999:blog-8252867149515968674.post-90857817450516311002014-07-19T17:05:00.000-07:002014-07-19T17:06:15.156-07:00Using Plunker With LESS<p>According to this update, it’s now possible to <a href="http://run.plnkr.co/plunks/CiBjc4/">use compiled languages like LESS with plunker</a></p>
<p>According to the <a href="http://lesscss.org/#client-side-usage">Client Side Usage from the Less Documentation</a>, you should do the following:</p><div class="se-section-delimiter"></div>
<pre class="prettyprint"><code class=" hljs xml"><span class="hljs-doctype"><!DOCTYPE html></span>
<span class="hljs-tag"><<span class="hljs-title">html</span>></span>
<span class="hljs-tag"><<span class="hljs-title">head</span>></span>
<span class="hljs-tag"><<span class="hljs-title">link</span> <span class="hljs-attribute">rel</span>=<span class="hljs-value">"stylesheet/less"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/css"</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"style.less"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"//cdnjs.cloudflare.com/ajax/libs/less.js/1.7.3/less.min.js"</span>></span><span class="javascript"></span><span class="hljs-tag"></<span class="hljs-title">script</span>></span>
<span class="hljs-tag"></<span class="hljs-title">head</span>></span>
<span class="hljs-tag"><<span class="hljs-title">body</span>></span>
<span class="hljs-tag"></<span class="hljs-title">body</span>></span>
<span class="hljs-tag"></<span class="hljs-title">html</span>></span></code></pre>
<h4 id="stylesless">styles.less:</h4>
<p>Here’s a super simple example of a less file that will chane the background color bright red</p>
<pre class="prettyprint"><code class=" hljs css"><span class="hljs-at_rule">@<span class="hljs-keyword">background-color:</span> red</span>;
<span class="hljs-tag">body</span> <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">background</span>:<span class="hljs-value"> @background-color</span></span>; <span class="hljs-rule">}</span></span></code></pre>
<h3 id="heres-an-example-in-plunker"><a href="http://plnkr.co/edit/lpsP0gIdcQV9Ftn457UD?p=preview">Here’s an example in plunker</a></h3>
<p>If it’s not showing up for whatever reason, then you probably have a compile error in your less, which will cause the entire sheet to not be delivered. You can check by copying and pasting your code into a <a href="http://www.freeformatter.com/less-compiler.html">online LESS compiler</a> and seeing if it spits out any errors.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com2tag:blogger.com,1999:blog-8252867149515968674.post-66398370429695182014-06-30T15:01:00.000-07:002017-06-26T05:28:22.550-07:00Adding a Desktop Mobile Toggle Button<p>The <strong>goal</strong> of a Bootstrap site is to deliver a mobile view that is so easily navigable that it never leaves the user yearning for a tiny shrunk down desktop experience on their phone. Still, despite a developers best intentions, some users just want what they want. And we should give it to them. We’re not designing sites so that we may enforce our will on others, but to provide content that users find easy to view, and some users have different preferences than others.</p>
<p>Luckily, we can easily provide both worlds, and default to a responsive view with the hopes that they will find it so convincing they’ll never need the desktop view.</p>
<p>Here’s a little snippet of code that I like to put on websites. You can see it in action on: <a href="http://kylemitofsky.com/">http://kylemitofsky.com/</a></p>
<p>Add two toggles to your page.</p>
<pre class="prettyprint"><code class=" hljs xml"><span class="hljs-comment"><!-- Desktop / Mobile Footer --></span>
<span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"DesktopMobileFooter"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">'h_center'</span>></span>
<span class="hljs-tag"><<span class="hljs-title">hr</span>/></span>
<span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"MobileToggle"</span>></span>
View <span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"DesktopSite"</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"#"</span> ></span>Desktop<span class="hljs-tag"></<span class="hljs-title">a</span>></span> | <span class="hljs-tag"><<span class="hljs-title">strong</span>></span>Mobile<span class="hljs-tag"></<span class="hljs-title">strong</span>></span> Site
<span class="hljs-tag"></<span class="hljs-title">div</span>></span>
<span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"MobileToggle"</span> <span class="hljs-attribute">style</span>=<span class="hljs-value">"display:none;"</span>></span>
View <span class="hljs-tag"><<span class="hljs-title">strong</span>></span>Desktop<span class="hljs-tag"></<span class="hljs-title">strong</span>></span> | <span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"MobileSite"</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"#"</span> ></span>Mobile<span class="hljs-tag"></<span class="hljs-title">a</span>></span> Site
<span class="hljs-tag"></<span class="hljs-title">div</span>></span>
<span class="hljs-tag"></<span class="hljs-title">div</span>></span></code></pre>
<p>Then add the following JavaScript:</p>
<pre class="prettyprint"><code class="language-js hljs ">$(<span class="hljs-string">".MobileToggle a"</span>).click(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> {</span>
<span class="hljs-keyword">var</span> viewport = ($(<span class="hljs-keyword">this</span>).attr(<span class="hljs-string">'id'</span>) === <span class="hljs-string">"MobileSite"</span>) ?
<span class="hljs-string">'width=device-width, initial-scale=1.0'</span> :
<span class="hljs-string">'width=1200'</span>;
$(<span class="hljs-string">"meta[name=viewport]"</span>).attr(<span class="hljs-string">'content'</span>, viewport);
$(<span class="hljs-string">".MobileToggle"</span>).toggle();
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
});</code></pre>
<p><strong>NOTE</strong>: As AJ pointed out in the comments, for this solution to work (and Bootstrap in general) make sure you’ve included the tags from <a href="http://getbootstrap.com/getting-started/#template">Bootstrap’s Basic Template</a>, specifically the viewport meta tag:</p>
<pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"viewport"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"width=device-width, initial-scale=1"</span>></span></code></pre>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com71tag:blogger.com,1999:blog-8252867149515968674.post-56636814792332348482014-06-26T09:55:00.000-07:002014-06-28T15:31:00.199-07:00How To Deselect A Radio Button<p>Sometimes radio buttons can be quite frustrating as a user when you need to deselect having answered a question.</p>
<p>Here’s a quick way to temporarily allow a page to deselect radio buttons by pressing <kbd>Ctrl</kbd> + <kbd>Click</kbd></p>
<ol>
<li>Navigate to any page that has radiobuttons. Here’s an <a href="http://jsfiddle.net/KyleMit/8vu8v/show/">example of one</a>:</li>
<li>Click <kbd>F12</kbd> to open up the developer tools</li>
<li>Navigate to the Console Pane </li>
<li><p>Copy and paste in the following code into the editor: </p>
<pre class="prettyprint"><code class=" hljs lua">document.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(e)</span></span>{
<span class="hljs-keyword">if</span> (e.ctrlKey == <span class="hljs-keyword">true</span> &&
e.target.tagName == <span class="hljs-string">'INPUT'</span> &&
e.target.<span class="hljs-built_in">type</span> == <span class="hljs-string">"radio"</span> &&
e.target.checked == <span class="hljs-keyword">true</span>) {
e.target.checked = <span class="hljs-keyword">false</span>;
}
});</code></pre></li>
<li>Click Run Script (or hit <kbd>Enter</kbd>)</li>
<li>Now, to deselect a radio button just go back to the page and hold down <kbd>Ctrl</kbd> and click with your mouse.</li>
</ol>
<p><img id="browserInstructions" src="http://i.imgur.com/aeUS4la.gif" alt="Instructions"></p>
<p>This screenshot should match your current browser, but if it doesn’t - here’s an <a href="http://imgur.com/a/SB8rN">album of screenshots for different browsers</a></p>
<div><script>
//http://stackoverflow.com/a/9851769/1366033
var isFirefox = typeof InstallTrigger !== 'undefined';
var isIE = /*@cc_on!@*/false || !!document.documentMode;
var img = document.getElementById('browserInstructions');
if (isFirefox) {
img.src = 'http://i.imgur.com/xDodZUR.gif';
} else if (isIE) {
img.src = 'http://i.imgur.com/tgWvanJ.gif';
}
</script></div>
<blockquote>
<p><strong>Note</strong>: Of course, by the time you’re opening up the developer tools, you can just edit the HTML directly, but this is a little more reusable.</p>
</blockquote>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com5tag:blogger.com,1999:blog-8252867149515968674.post-4266466177452772762014-06-20T06:16:00.002-07:002014-06-21T06:00:56.063-07:00Pulling Your Website Up By Your Twitter Bootstraps<h3 id="hello-boston-code-camp">Hello Boston Code Camp!</h3>
<p>Here are some resources to supplement my talk on Bootstrap 3 talk titled: <br>
<a href="https://www.bostoncodecamp.com/CC21/Sessions/Details/7177">Pulling Your Website Up By Your Twitter Bootstraps</a></p>
<p>Here are some powerpoint slides:</p>
<script async="" class="speakerdeck-embed" data-id="656b2740daa50131c6a17202678817e0" data-ratio="1.77777777777778" src="//speakerdeck.com/assets/embed.js"></script>
<p><br></p>
<p>Here is a working version of the website from the presentation: <br>
<a href="http://kylemitofsky.com/BootstrapPresentation/">http://kylemitofsky.com/BootstrapPresentation/</a></p>
<p>The entire code sample was published to GitHub here: <br>
<a href="https://github.com/KyleMit/BootstrapPresentation">https://github.com/KyleMit/BootstrapPresentation</a></p>
<p>You can get Bootstrap here (and also read their <em>great</em> documentation): <br>
<a href="http://getbootstrap.com/">http://getbootstrap.com/</a></p>
<p>You can style Bootstrap here: <br>
<a href="http://bootswatch.com/">http://bootswatch.com/</a></p>
<p>I’m really excited that every step of the demo can is actually a commit into the GitHub repository. I wrote an article about <a href="http://www.codingeverything.com/2014/06/StepThroughCodeDemo.html">using using revision control to demo live coding changes</a>, but here’s the meat of it. If you fork the repository on github and open it up it powershell, then you can step through every commit with these commands:</p><div class="se-section-delimiter"></div>
<pre class="prettyprint"><code class=" hljs lasso">git config <span class="hljs-subst">--</span><span class="hljs-built_in">local</span> alias<span class="hljs-built_in">.</span>child<span class="hljs-attribute">-sha</span> <span class="hljs-string">"!git rev-list HEAD..master | tail -n 1"</span>
git config <span class="hljs-subst">--</span><span class="hljs-built_in">local</span> alias<span class="hljs-built_in">.</span>initial<span class="hljs-attribute">-commit</span> <span class="hljs-string">"!git rev-list --all | tail -n 1"</span>
git checkout master
git checkout $(git initial<span class="hljs-attribute">-commit</span>)
git checkout $(git child<span class="hljs-attribute">-sha</span>)</code></pre>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com3tag:blogger.com,1999:blog-8252867149515968674.post-62632468713086128712014-06-19T14:33:00.004-07:002014-06-19T14:33:51.390-07:00How to Pivot a DataTable in .NET<p>So I would have thought this problem would have already been solved by the Internets at large. As it turns out, I couldn’t find a very simple method to solve this relatively simple task. So here’s my attempt</p>
<p>Here’s a dead simple pivot table.</p>
<h4 id="lets-say-i-have-a-table-that-looks-like-this">Let’s say I have a table that looks like this:</h4>
<table>
<thead>
<tr>
<th>Person</th>
<th>Age</th>
<th>Sport</th>
</tr>
</thead>
<tbody><tr>
<td>Susan</td>
<td>22</td>
<td>Tennis</td>
</tr>
<tr>
<td>Bob</td>
<td>29</td>
<td>Soccer</td>
</tr>
<tr>
<td>Terry</td>
<td>16</td>
<td>Basketball</td>
</tr>
</tbody></table>
<h4 id="and-i-want-to-pivot-it-to-to-look-like-this">And I want to pivot it to to look like this:</h4>
<table>
<thead>
<tr>
<th>Person</th>
<th>Susan</th>
<th>Bob</th>
<th>Terry</th>
</tr>
</thead>
<tbody><tr>
<td>Age</td>
<td>22</td>
<td>29</td>
<td>16</td>
</tr>
<tr>
<td>Sport</td>
<td>Tennis</td>
<td>Soccer</td>
<td>Basketball</td>
</tr>
</tbody></table>
<h4 id="heres-how">Here’s How</h4>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">Private</span> <span class="hljs-keyword">Function</span> PivotTable(oldTable <span class="hljs-keyword">As</span> DataTable,
<span class="hljs-keyword">Optional</span> pivotColumnOrdinal <span class="hljs-keyword">As</span> <span class="hljs-built_in">Integer</span> = <span class="hljs-number">0</span>
) <span class="hljs-keyword">As</span> DataTable
<span class="hljs-keyword">Dim</span> newTable <span class="hljs-keyword">As</span> <span class="hljs-keyword">New</span> DataTable
<span class="hljs-keyword">Dim</span> dr <span class="hljs-keyword">As</span> DataRow
<span class="hljs-comment">' add pivot column name</span>
newTable.Columns.Add(oldTable.Columns(pivotColumnOrdinal).ColumnName)
<span class="hljs-comment">' add pivot column values in each row as column headers to new Table</span>
<span class="hljs-keyword">For</span> <span class="hljs-keyword">Each</span> row <span class="hljs-keyword">In</span> oldTable.Rows
newTable.Columns.Add(row(pivotColumnOrdinal))
<span class="hljs-keyword">Next</span>
<span class="hljs-comment">' loop through columns</span>
<span class="hljs-keyword">For</span> col = <span class="hljs-number">0</span> <span class="hljs-keyword">To</span> oldTable.Columns.Count - <span class="hljs-number">1</span>
<span class="hljs-comment">'pivot column doen't get it's own row (it is already a header)</span>
<span class="hljs-keyword">If</span> col = pivotColumnOrdinal <span class="hljs-keyword">Then</span> <span class="hljs-keyword">Continue</span> <span class="hljs-keyword">For</span>
<span class="hljs-comment">' each column becomes a new row</span>
dr = newTable.NewRow()
<span class="hljs-comment">' add the Column Name in the first Column</span>
dr(<span class="hljs-number">0</span>) = oldTable.Columns(col).ColumnName
<span class="hljs-comment">' add data from every row to the pivoted row</span>
<span class="hljs-keyword">For</span> row = <span class="hljs-number">0</span> <span class="hljs-keyword">To</span> oldTable.Rows.Count - <span class="hljs-number">1</span>
dr(row + <span class="hljs-number">1</span>) = oldTable.Rows(row)(col)
<span class="hljs-keyword">Next</span>
<span class="hljs-comment">'add the DataRow to the new table</span>
newTable.Rows.Add(dr)
<span class="hljs-keyword">Next</span>
<span class="hljs-keyword">Return</span> newTable
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Function</span></code></pre>
<p>Then just call like this:</p>
<pre class="prettyprint"><code class="language-vbnet hljs "><span class="hljs-keyword">Dim</span> newTable = PivotTable(oldTable, <span class="hljs-number">0</span>)</code></pre>
<p>And that’s that.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com4tag:blogger.com,1999:blog-8252867149515968674.post-2211266858641204732014-06-15T22:26:00.000-07:002014-06-16T05:40:15.384-07:00Stepping through a Code Demo<p>One challenge to delivering coding presentations is the trade-off between: </p>
<ul>
<li>Showing actual <strong>real code</strong> that developers can get their head around <br>
</li><li style=" list-style-type: none;"> <strong>VS</strong></li>
<li>The <strong>time</strong> it costs to actually write it in front of them (debugging missing commas included).</li>
</ul>
<p>Here are some of the ways I’ve seen to side step this issue in previous coding presentations I’ve attended:</p>
<ul>
<li>Start with the full app. Study relevant sections.</li>
<li>Start with the full app, but mostly commented out. Uncomment section by section.</li>
<li>Start with a blank app and a notepad document with all the code you’ll eventually want. Copy and paste code section by section.</li>
<li>Start with the full app and use Unit Tests to step into relevant code.</li>
</ul>
<p>Here is one more that I will be trying out:</p>
<ul>
<li>Utilize revision control software to have each state of the code base represented by a different commit. Then step through the commits.</li>
</ul>
<p>If executed well, I really like this last one. It avoids having too much information. Especially if content is new, it can disrupt the noise to signal ratio if attendees are thrown into a world with too much code. It also helps reduce copy and paste errors that might occur and the overall time it takes to update the code base. The beautiful thing is that each state of the code base has a known success. There is never the possibility that you have forgotten to uncomment <em>that one critical line</em>. </p>
<p>You can easily step back through the entire code base to rehearse. <strong>What’s more</strong> - so can participants when they get home. When have you ever left a coding presentation having been able to re-create every single step performed by the presenter?</p>
<h3 id="StepThroughCommits">Stepping Through Commits</h3>
<p>Since this option definitely has the highest startup cost, I’ll detail some of the items along my learning curve.</p>
<p>I’ll preface this by saying that I’m using <a href="https://github.com/"><strong>GitHub</strong></a> as my <abbr title="Revision Control System">RCS</abbr> of choice.</p>
<p>Also, <strong>I’ve never stepped through each revision in an entire codebase before!</strong></p>
<p>It’s not something that is covered by most of the use cases for revision control. Of course, it’s a perfectly acceptable use, just not one that you’re likely to need when developing software. </p>
<p>The easiest way to change your working copy to a specific version is to use:</p>
<pre><code>git checkout <i><revision></i>
</code></pre>
<p>Where <em><code><revision></code></em> is anything that <a href="http://git-scm.com/docs/gitrevisions">identifies a revision</a></p>
<ul>
<li>We can do this with the <strong>SHA</strong> of any particular commit.</li>
<li>Or we can step back through previous commits by using <code>HEAD~<i>n</i></code> where <em>n</em> is the number of commits we want to step back</li>
</ul>
<p>The problem is what we want to do is step <em>forward</em> through revisions. While the <code>~1</code> selects the <em>parent</em> of the current revision, there is no native function to select its <em>child</em>.</p>
<blockquote>
<p><strong>Note</strong>: This is partly because of the way that a <a href="http://en.wikipedia.org/wiki/Directed_acyclic_graph">Directed Acyclic Graph (DAG)</a> works. Each commit knows who it’s parent is, but not which children might be attached to it. <br>
<img src="http://i.imgur.com/5qfhTx8.png" alt="Git History" title=""> <br>
For example, <code>[B]</code> knows it was branched off <code>[A]</code>, but does not know who it’s child is (in fact has two children!)</p>
</blockquote>
<p>Part of this complexity can be avoided as I’m looking to have a relatively straight forward linear revision history.</p>
<h4 id="ChildSelector">Child Selector</h4>
<p>From the <a href="http://stackoverflow.com/a/3557291/1366033">answer to what is the opposite of <code>git diff HEAD~1</code>?</a>:</p>
<p>You can checkout the next branch like this:</p>
<pre class="prettyprint"><code class="language-no-highlight">git checkout $(git rev-list HEAD..master | tail -n 1)</code></pre>
<h5 id="in-case-youre-new-to-gitshell-lets-break-this-down-a-little">In case you’re new to Git/Shell, let’s break this down a little.</h5>
<p>The inner expression <a href="http://git-scm.com/docs/git-rev-list"><code>git rev-list</code></a> will show a list of revisions. By specifying any two revision id’s joined by two periods, we’ll get a list of all the SHAs in between them.</p>
<p>So, for the following revision history:</p>
<pre class="prettyprint"><code class="language-no-highlight"> HEAD
↓
A -- B -- C -- D -- E (master)</code></pre>
<p>The command:</p>
<pre class="prettyprint"><code class="language-no-highlight">git rev-list HEAD..master</code></pre>
<p>Will Return (<em>with the most recent first</em>):</p>
<pre class="prettyprint"><code class="language-no-highlight">E
D
C</code></pre>
<p>Then by using the pipe operator in powershell, we can grab the last value by piping in <code>| tail -n 1</code></p>
<p>So this command:</p>
<pre class="prettyprint"><code class="language-no-highlight">git rev-list HEAD..master | tail -n 1</code></pre>
<p>Will just return the commit we want:</p>
<pre class="prettyprint"><code class="language-no-highlight">C</code></pre>
<p>In order to make this into a one liner, we’ll need to wrap the query so we can pass the output into our checkout command. We can use the <a href="http://ss64.com/ps/syntax-operators.html"><code>$( )</code> SubExpression operator</a> to return a vector value, giving us the original equation.</p>
<h4 id="adding-an-alias">Adding an Alias</h4>
<p>If we’re using this a lot, it might look prettier to <a href="https://git.wiki.kernel.org/index.php/Aliases">alias the query</a>. <br>
We can add a new command called <code>child-sha</code> to our <code>local</code> config file like this:</p>
<pre class="prettyprint"><code class="language-no-highlight">git config --local alias.child-sha "!git rev-list HEAD..master | tail -n 1"</code></pre>
<blockquote>
<p><strong>Note</strong>: <a href="http://stackoverflow.com/a/24233289/1366033">The exclamation point is important!</a> Without it, git will only execute git specific commands. Adding it allows git to use powershell commands as well like <code>| tail -n 1</code></p>
</blockquote>
<p>Then we can checkout the next commit like this:</p>
<pre class="prettyprint"><code class="language-no-highlight">git checkout $(git child-sha)</code></pre>
<h3 id="other-considerations">Other Considerations</h3>
<p>You’ll be seeing a lot of this dialog: <br>
<img src="http://i.imgur.com/9NZGbtB.png" alt="Reload Warning" title=""></p>
<p>To avoid this, go to: <br>
Tools > Options > Environment > Documents > Detect when a file is changed <br>
And make sure <code>Auto-load changes</code> is checked: <br>
<img src="http://i.imgur.com/ZHJHXKu.png" alt="Auto Load Changes" title=""></p>
<h4 id="conclusion">Conclusion:</h4>
<p>That’s about it. I’ll add my presentation here when it’s finished so you can see a little more concrete of an example. <br>
Let me know if you give it a shot and have any experiences (good or bad).</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com1tag:blogger.com,1999:blog-8252867149515968674.post-15476012647941527292014-05-28T07:34:00.000-07:002014-05-28T07:39:36.924-07:00Remove Items in For Loop<p>As a general rule, you should not modify a collection that your are looping over, only the items inside of that collection. The problem with removing items inside of a for loop is that it changes the collection that is being looped which will interfere with the list count in an <em>indexed for loop</em> and the iterator location in a <em>for each loop</em>.</p><div class="se-section-delimiter"></div>
<h3 id="two-common-solutions-are-to">Two common solutions are to:</h3>
<ol>
<li>Create a new collection so you can modify one collection and loop over another.</li>
<li>Loop backwards through the collection so changes to the iterator won’t impact the execution.</li>
</ol>
<p>In this article, we’ll create two extension methods that utilize each of these solutions.</p>
<h3 id="create-new-collection">Create New Collection:</h3>
<p>By calling <a href="http://msdn.microsoft.com/en-us/library/vstudio/bb342261%28v=vs.100%29.aspx"><code>ToList</code></a> on the original collection, you create a brand new collection. Then, you can loop over the new list to find items that need to be removed from the original. Whenever you find an object that matches your removal criteria, you can safely remove it from the original collection because it is not currently being enumerated over. </p>
<p>I think it looks pretty spiffy too:</p>
<pre class="prettyprint"><code class=" hljs vbnet"><Extension()>
<span class="hljs-keyword">Public</span> <span class="hljs-keyword">Sub</span> RemoveEachObject(<span class="hljs-keyword">Of</span> T)(<span class="hljs-keyword">ByVal</span> col <span class="hljs-keyword">As</span> Collection(<span class="hljs-keyword">Of</span> T),
<span class="hljs-keyword">ByVal</span> match <span class="hljs-keyword">As</span> Func(<span class="hljs-keyword">Of</span> T, <span class="hljs-built_in">Boolean</span>))
<span class="hljs-keyword">For</span> <span class="hljs-keyword">Each</span> o <span class="hljs-keyword">As</span> T <span class="hljs-keyword">In</span> col.ToList()
<span class="hljs-keyword">If</span> match(o) <span class="hljs-keyword">Then</span> col.Remove(o)
<span class="hljs-keyword">Next</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Sub</span></code></pre><div class="se-section-delimiter"></div>
<h3 id="loop-backwards">Loop Backwards:</h3>
<p>The previous solution works well, but a more efficient solution would be to loop backwards. For starters, the previous answer will have to create a copy of the entire collection. More importantly, when removing items, the <a href="http://msdn.microsoft.com/en-us/library/ms132413%28v=vs.110%29.aspx"><code>Remove()</code></a> method will have to loop through entire collection and check each item for reference equality with the passed in value. This can be quite expensive. It would be much easier to keep track of the current index in the collection and remove whatever item happened to be occupying it.</p>
<p>To do this, we’ll loop backwards and check the validity of each item in the collection based on the passed in lambda function. If it matches, then we’ll remove the current index.</p>
<pre class="prettyprint"><code class="language-vbnet hljs "><Extension()>
<span class="hljs-keyword">Public</span> <span class="hljs-keyword">Sub</span> RemoveEach(<span class="hljs-keyword">Of</span> T)(<span class="hljs-keyword">ByVal</span> col <span class="hljs-keyword">As</span> Collection(<span class="hljs-keyword">Of</span> T),
<span class="hljs-keyword">ByVal</span> match <span class="hljs-keyword">As</span> Func(<span class="hljs-keyword">Of</span> T, <span class="hljs-built_in">Boolean</span>))
<span class="hljs-keyword">For</span> i = col.Count - <span class="hljs-number">1</span> <span class="hljs-keyword">To</span> <span class="hljs-number">0</span> <span class="hljs-keyword">Step</span> -<span class="hljs-number">1</span>
<span class="hljs-keyword">If</span> match(col(i)) <span class="hljs-keyword">Then</span> col.RemoveAt(i)
<span class="hljs-keyword">Next</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Sub</span></code></pre><div class="se-section-delimiter"></div>
<h3 id="usage">Usage</h3>
<p>Then we can use either method like this:</p>
<pre class="prettyprint"><code class="language-vbnet hljs "><span class="hljs-keyword">Dim</span> col <span class="hljs-keyword">As</span> <span class="hljs-keyword">New</span> Collection(<span class="hljs-keyword">Of</span> <span class="hljs-built_in">Integer</span>) <span class="hljs-keyword">From</span> {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
col.RemoveEach(<span class="hljs-keyword">Function</span>(i) (i <span class="hljs-keyword">Mod</span> <span class="hljs-number">2</span>) = <span class="hljs-number">0</span>)
Console.WriteLine(<span class="hljs-built_in">String</span>.<span class="hljs-keyword">Join</span>(<span class="hljs-string">","</span>, col))
<span class="hljs-comment">'Produces: 1,3</span></code></pre>
<p>This code has been written to extend <code>Collection(Of T)</code>. You could just as easily extend <code>List(Of T)</code> as well, but the list class already exposes the method <a href="http://msdn.microsoft.com/en-us/library/wdka673a%28v=vs.110%29.aspx"><code>RemoveAll</code></a> which already does this same thing. </p>
<p>For more info, check out this great answer to <a href="http://stackoverflow.com/a/9381864/1366033">Remove from a List<t> within a ‘foreach’ loop</t></a>.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com5tag:blogger.com,1999:blog-8252867149515968674.post-38638298161867756682014-05-15T13:08:00.000-07:002014-05-15T13:08:28.750-07:00Using Shared Properties in ASP.NET and WinForms<p>A good business library should be able to drive behavior on any number of devices, exposing a application programming interface that is agnostic of its implementation. </p>
<p>However, we recently had a problem when we wanted to re-use a business library that was designed for a Windows Forms application for use in an ASP.NET web application. The problem was that our original application used shared properties to manage the state of the application. </p>
<blockquote>
<p><strong>Note</strong>: it seems prudent to <a href="http://c2.com/cgi/wiki?GlobalVariablesAreBad">avoid global variables</a>, but this was where we found ourselves.</p>
</blockquote>
<p><strong>Why was this a problem?</strong> The original design meant that each instance of the application was owned by a single user under a single process. <em>However</em>, in ASP.NET, all users share a single process hosted by the ASP.NET runtime. Since shared variables are unique to the process they live on, variables that managed state for a single user (i.e. <code>ActiveClient</code>) would now be shared, overwritten, and cleared all at once by every single user within the web application.</p>
<p>Without massive re-writing of the business and application logic, we needed a way to give windows app users their own global variables as well as giving web app users a way to access their own <em>instance</em> of the <em>shared</em> properties.</p>
<h3 id="heres-how-we-did-it">Here’s how we did it:</h3>
<p>First, consider what a normal shared property would look like in VB.NET: </p>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">Private</span> <span class="hljs-keyword">Shared</span> _activeClient <span class="hljs-keyword">As</span> Client
<span class="hljs-keyword">Public</span> <span class="hljs-keyword">Shared</span> <span class="hljs-keyword">Property</span> ActiveClient() <span class="hljs-keyword">As</span> Client
<span class="hljs-keyword">Get</span>
<span class="hljs-keyword">Return</span> _activeClient
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Get</span>
<span class="hljs-keyword">Set</span>(<span class="hljs-keyword">ByVal</span> value <span class="hljs-keyword">As</span> Client)
_activeClient = Value
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Set</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Property</span></code></pre>
<blockquote>
<p><strong>Note</strong>: For this example, I have a class named <code>Client</code> but this could be any variable that I wanted to share access to across the entire application for a single user.</p>
</blockquote>
<p>It’s important to breakdown all the things that a property does here. The property doesn’t actually hold the value across all instances; the private shared field does. The property just provides global accessors and setters to retrieve and set the value.</p>
<h4 id="aspnet-session-variables">ASP.NET Session Variables</h4>
<p>In ASP.NET, we can store values unique to each user inside of the <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms178581.aspx">Session Variable</a>. To figure out how to integrate this with the first case, let’s consider what a typical session variable looks like:</p>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-comment">'setter</span>
Session(<span class="hljs-string">"ActiveClient"</span>) = <span class="hljs-keyword">new</span> Client()
<span class="hljs-comment">'getter</span>
<span class="hljs-keyword">Dim</span> myClient = <span class="hljs-built_in">DirectCast</span>(Session(<span class="hljs-string">"ActiveClient"</span>), Client)</code></pre>
<p>This works well enough, but it’s a little messy. We have to manually perform casting ourselves and also keep track of all the key strings across the entire application. </p>
<p>Here’s one way to improve this (and other projects) by adding <a href="http://weblogs.asp.net/cstewart/archive/2008/01/09/strongly-typed-session-in-asp-net.aspx">strong typing to the ASP.NET session variable</a>:</p>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">Public</span> <span class="hljs-keyword">Shared</span> <span class="hljs-keyword">Property</span> ActiveClient() <span class="hljs-keyword">As</span> Client
<span class="hljs-keyword">Get</span>
<span class="hljs-keyword">If</span> HttpContext.Current.Session(<span class="hljs-string">"ActiveClient"</span>) <span class="hljs-keyword">Is</span> <span class="hljs-literal">Nothing</span> <span class="hljs-keyword">Then</span>
HttpContext.Current.Session(<span class="hljs-string">"ActiveClient"</span>) = <span class="hljs-keyword">New</span> Client
<span class="hljs-keyword">End</span> <span class="hljs-keyword">If</span>
<span class="hljs-keyword">Return</span> <span class="hljs-built_in">DirectCast</span>(HttpContext.Current.Session(<span class="hljs-string">"ActiveClient"</span>), Client)
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Get</span>
<span class="hljs-keyword">Set</span>(value <span class="hljs-keyword">As</span> Client)
HttpContext.Current.Session(<span class="hljs-string">"ActiveClient"</span>) = value
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Set</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Property</span></code></pre>
<p>We still have global accessors and setters provided by the shared property, but inside of the <code>Get</code> and <code>Set</code> operations we’re using the session variable to store the value for each individual user. Note that we’re using the <em>static</em> <a href="http://msdn.microsoft.com/en-us/library/system.web.httpcontext.current.aspx"><code>HttpContext.Current</code> Property</a> so the code doesn’t not have to live on a page with it’s own HttpContext.</p>
<h3 id="merging-both-properties">Merging Both Properties</h3>
<p>We now have a property in a windows application that stores a value unique to each user / process and a property in a web application that stores a value unique to each user / session. All that is left to do is to merge the properties accordingly.</p>
<p>The first step in doing so is to determine whether or not the assembly is currently executing as a web or windows application. We can do so by checking if the HttpContext Current property exists:</p>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">Dim</span> isWebDeployed <span class="hljs-keyword">as</span> <span class="hljs-built_in">Boolean</span> = System.Web.HttpContext.Current <span class="hljs-keyword">IsNot</span> <span class="hljs-literal">Nothing</span></code></pre>
<p>Using that information, we can just expand the logic in our getters and setters to first check the execution environment and then grab the appropriate value:</p>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">Private</span> <span class="hljs-keyword">Shared</span> _activeClient <span class="hljs-keyword">As</span> <span class="hljs-keyword">New</span> Client
<span class="hljs-keyword">Public</span> <span class="hljs-keyword">Shared</span> <span class="hljs-keyword">Property</span> ActiveClient() <span class="hljs-keyword">As</span> Client
<span class="hljs-keyword">Get</span>
<span class="hljs-comment">'check if deployed as web application</span>
<span class="hljs-keyword">If</span> System.Web.HttpContext.Current <span class="hljs-keyword">IsNot</span> <span class="hljs-literal">Nothing</span> <span class="hljs-keyword">Then</span>
<span class="hljs-comment">'if we've never loaded, create new instance just for session</span>
<span class="hljs-keyword">If</span> System.Web.HttpContext.Current.Session(<span class="hljs-string">"ActiveClient"</span>) <span class="hljs-keyword">Is</span> <span class="hljs-literal">Nothing</span> <span class="hljs-keyword">Then</span>
System.Web.HttpContext.Current.Session(<span class="hljs-string">"ActiveClient"</span>) = <span class="hljs-keyword">New</span> Client
<span class="hljs-keyword">End</span> <span class="hljs-keyword">If</span>
<span class="hljs-keyword">Return</span> <span class="hljs-built_in">DirectCast</span>(System.Web.HttpContext.Current.Session(<span class="hljs-string">"ActiveClient"</span>), Client)
<span class="hljs-keyword">Else</span>
<span class="hljs-comment">'application is windows application</span>
<span class="hljs-keyword">Return</span> _activeClient
<span class="hljs-keyword">End</span> <span class="hljs-keyword">If</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Get</span>
<span class="hljs-keyword">Set</span>(<span class="hljs-keyword">ByVal</span> value <span class="hljs-keyword">As</span> Client)
<span class="hljs-comment">'check if deployed as web application</span>
<span class="hljs-keyword">If</span> System.Web.HttpContext.Current <span class="hljs-keyword">IsNot</span> <span class="hljs-literal">Nothing</span> <span class="hljs-keyword">Then</span>
System.Web.HttpContext.Current.Session(<span class="hljs-string">"ActiveClient"</span>) = Value
<span class="hljs-keyword">Else</span>
_activeClient = Value
<span class="hljs-keyword">End</span> <span class="hljs-keyword">If</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Set</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Property</span></code></pre>
<h3 id="extended-solution">Extended Solution</h3>
<p>Based off the number of instances you’re dealing with, the previous solution might work just fine. However, if you need to repeat this across multiple shared properties, you might want something a little more reusable.</p>
<p>Here’s a <strong>generic getter method</strong> that takes in the shared private field for a windows application and the key string for a web application and returns the appropriate value:</p>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">Public</span> <span class="hljs-keyword">Shared</span> <span class="hljs-keyword">Function</span> GetPropertyBasedOnEnvironment(<span class="hljs-keyword">Of</span> T)(<span class="hljs-keyword">ByRef</span> sharedMember <span class="hljs-keyword">As</span> T, <span class="hljs-keyword">ByVal</span> propName <span class="hljs-keyword">As</span> <span class="hljs-built_in">String</span>) <span class="hljs-keyword">As</span> T
<span class="hljs-comment">'check if deployed as web application</span>
<span class="hljs-keyword">If</span> HttpContext.Current <span class="hljs-keyword">IsNot</span> <span class="hljs-literal">Nothing</span> <span class="hljs-keyword">Then</span>
<span class="hljs-comment">'application is web application</span>
<span class="hljs-keyword">Return</span> <span class="hljs-built_in">TryCast</span>(HttpContext.Current.Session(propName), T)
<span class="hljs-keyword">Else</span>
<span class="hljs-comment">'application is windows application</span>
<span class="hljs-keyword">Return</span> sharedMember
<span class="hljs-keyword">End</span> <span class="hljs-keyword">If</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Function</span></code></pre>
<p>Conversely, here’s <strong>generic setter method</strong> that can access the old value and assign it a new value depending on the current environment:</p>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">Public</span> <span class="hljs-keyword">Shared</span> <span class="hljs-keyword">Sub</span> SetPropertyBasedOnEnvironment(<span class="hljs-keyword">Of</span> T)(<span class="hljs-keyword">ByRef</span> sharedMember <span class="hljs-keyword">As</span> T, <span class="hljs-keyword">ByVal</span> propName <span class="hljs-keyword">As</span> <span class="hljs-built_in">String</span>, newValue <span class="hljs-keyword">As</span> T)
<span class="hljs-comment">'check if deployed as web application</span>
<span class="hljs-keyword">If</span> HttpContext.Current <span class="hljs-keyword">IsNot</span> <span class="hljs-literal">Nothing</span> <span class="hljs-keyword">Then</span>
<span class="hljs-comment">'application is web application</span>
HttpContext.Current.Session(propName) = newValue
<span class="hljs-keyword">Else</span>
<span class="hljs-comment">'application is windows application</span>
sharedMember = newValue
<span class="hljs-keyword">End</span> <span class="hljs-keyword">If</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Sub</span></code></pre>
<p>With those two methods accessible, we can now simplify our shared property using the following code:</p>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">Private</span> <span class="hljs-keyword">Shared</span> _activeClient <span class="hljs-keyword">As</span> <span class="hljs-keyword">New</span> Client
<span class="hljs-keyword">Public</span> <span class="hljs-keyword">Shared</span> <span class="hljs-keyword">Property</span> ActiveClient() <span class="hljs-keyword">As</span> Client
<span class="hljs-keyword">Get</span>
<span class="hljs-keyword">Return</span> GetPropertyBasedOnEnvironment(_activeClient, <span class="hljs-string">"ActiveClient"</span>)
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Get</span>
<span class="hljs-keyword">Set</span>(value <span class="hljs-keyword">As</span> Client)
SetPropertyBasedOnEnvironment(_activeClient, <span class="hljs-string">"ActiveClient"</span>, value)
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Set</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Property</span></code></pre>
<p><strong>Bonus</strong>: if you’re using .NET 4.5 or above, you can further simplify the custom getter and setter methods by using <a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute.aspx"><code>CallerMemberNameAttribute</code></a> to have the compiler pass the key string for you, like this:</p>
<pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">Public</span> <span class="hljs-keyword">Shared</span> <span class="hljs-keyword">Function</span> GetPropertyBasedOnEnvironment(<span class="hljs-keyword">Of</span> T)(
<span class="hljs-keyword">ByRef</span> sharedMember <span class="hljs-keyword">As</span> T,
<CallerMemberName> <span class="hljs-keyword">Optional</span> propName <span class="hljs-keyword">As</span> <span class="hljs-built_in">String</span> = <span class="hljs-literal">Nothing</span>) <span class="hljs-keyword">As</span> T
<span class="hljs-comment">'check if deployed as web application</span>
<span class="hljs-keyword">If</span> HttpContext.Current <span class="hljs-keyword">IsNot</span> <span class="hljs-literal">Nothing</span> <span class="hljs-keyword">Then</span>
<span class="hljs-keyword">Return</span> <span class="hljs-built_in">DirectCast</span>(HttpContext.Current.Session(propName), T)
<span class="hljs-keyword">Else</span>
<span class="hljs-keyword">Return</span> sharedMember
<span class="hljs-keyword">End</span> <span class="hljs-keyword">If</span>
<span class="hljs-keyword">End</span> <span class="hljs-keyword">Function</span></code></pre>
<p>Again, there might be more elegant architectural solutions, but this helped resolve the issue we were having based on the situation we had coded ourselves into.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com17tag:blogger.com,1999:blog-8252867149515968674.post-71654785631935791532014-05-02T13:29:00.000-07:002014-05-02T14:03:11.347-07:00Bootstrap Navbar Active Class with MVC<p>The ASP.NET MVC project template comes with Bootstrap scaffolding by default. And Bootstrap comes with default styling for active navbar links. So you might find it a little odd that the ASP.NET bootstrap template does not style the active menu item by default.</p>
<p><strong>It can</strong>, it just seems as if this functionality wasn’t included out of the box:</p>
<p><img src="http://i.imgur.com/rP0Ma1z.png" alt="styled navabar" title=""></p>
<p>If you plan on utilizing the bootstrap’s powerful navigational layout, you should definitely add styling for the current page. It helps users keep track of where they are within the application and assists with navigation. </p>
<p>To do so, we can add the <code>active</code> class dynamically on the shared layout by checking the current routing data. <strong>Here’s how</strong>:</p>
<h3 id="Markup">Markup</h3>
<p>When you create a new ASP.NET Web Application using MVC, the project should already contain some default pages and navigational links in the navbar. The navbar is defined as part of the shared layout in the Views folder. Your Solution Explorer should look like this:</p>
<p><img src="http://i.imgur.com/Sgt9cTu.png" alt="solution explorer" title=""></p>
<p>In the <code>_layout.vbhtml</code> file, you should find the following markup:</p>
<pre class="prettyprint"><code class="xml hljs "><span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"navbar-collapse collapse"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">ul</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"nav navbar-nav"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span><span class="highlight">@</span>Html.ActionLink("Home", "Index", "Home")<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span><span class="highlight">@</span>Html.ActionLink("About", "About", "Home")<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span><span class="highlight">@</span>Html.ActionLink("Contact", "Contact", "Home")<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"></<span class="hljs-title">ul</span>></span>
<span class="hljs-tag"></<span class="hljs-title">div</span>></span>
</code></pre>
<p>In order to highlight the current tab in the <a href="http://getbootstrap.com/components/#navbar">Bootstrap Navbar</a>, the <code><li></code> element needs to be given the class named <code>active</code>. As an example, just try hard coding it in on any one of the current links:</p>
<pre class="prettyprint"><code class="xml hljs "><span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"navbar-collapse collapse"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">ul</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"nav navbar-nav"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span> </span><b><span class="hljs-tag"><span class="hljs-attribute">class</span>=<span class="hljs-value">"active"</span></span></b><span class="hljs-tag">></span><span class="highlight">@</span>Html.ActionLink("Home", "Index", "Home")<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span><span class="highlight">@</span>Html.ActionLink("About", "About", "Home")<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span><span class="highlight">@</span>Html.ActionLink("Contact", "Contact", "Home")<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"></<span class="hljs-title">ul</span>></span>
<span class="hljs-tag"></<span class="hljs-title">div</span>></span>
</code></pre>
<p>This should look like the screenshot from above. But what we’d really like to do, is generate the active class dynamically for each <code>li</code> depending on the current page. We’ll insert the string <code>active</code> with an extension method called <code>IsActive</code> that will take in parameters for the Controller and Route.</p>
<p>We can use our extension method to insert the active class on the appropriate action link like this:</p>
<pre class="prettyprint"><code class="xml hljs "><span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"navbar-collapse collapse"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">ul</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"nav navbar-nav"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">'</span></span><span class="highlight"><span class="hljs-tag"><span class="hljs-value">@Html.IsActive("Home", "Index")</span></span></span><span class="hljs-tag"><span class="hljs-value">'</span>></span>
<span class="highlight">@</span>Html.ActionLink("Home", "Index", "Home")
<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">'</span></span><span class="highlight"><span class="hljs-tag"><span class="hljs-value">@Html.IsActive("Home", "About")</span></span></span><span class="hljs-tag"><span class="hljs-value">'</span>></span>
<span class="highlight">@</span>Html.ActionLink("About", "About", "Home")
<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">'</span></span><span class="highlight"><span class="hljs-tag"><span class="hljs-value">@Html.IsActive("Home", "Contact")</span></span></span><span class="hljs-tag"><span class="hljs-value">'</span>></span>
<span class="highlight">@</span>Html.ActionLink("Contact", "Contact", "Home")
<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"></<span class="hljs-title">ul</span>></span>
<span class="hljs-tag"></<span class="hljs-title">div</span>></span>
</code></pre>
<h3 id="ExtensionMethod">Extension Method</h3>
<p>If you don’t already have one, create a folder in your project named Utilities and add a <code>static class</code> (or <code>Module</code> in VB) named Utilities or Extensions.</p>
<p>Then, we’ll add an extension method called <code>IsActive</code> ontop of the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper%28v=vs.118%29.aspx"><code>HtmlHelper</code></a> class. We’ll use this to return the <code>active</code> class if the passed in controller text and action text match the current route.</p>
<p>To programmatically determine the current controller and action, we’ll use the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.viewcontext%28v=vs.118%29.aspx"><code>ViewContext</code></a> property on our HtmlHelper object. The ViewContext exposes, among other things, a property containing the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controllercontext.routedata%28v=vs.118%29.aspx"><code>RouteData</code></a> which <q>contains a collection of URL parameter values and default values for the route</q> in its <code>Values</code> property.</p>
<h4 id="the-whole-thing-should-look-like-this">The whole thing should look like this:</h4>
<pre class="prettyprint"><code class="language-cs hljs "><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> Utilities
{
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">string</span> <span class="hljs-title">IsActive</span>(<span class="hljs-keyword">this</span> HtmlHelper html,
<span class="hljs-keyword">string</span> control,
<span class="hljs-keyword">string</span> action)
{
<span class="hljs-keyword">var</span> routeData = html.ViewContext.RouteData;
<span class="hljs-keyword">var</span> routeAction = (<span class="hljs-keyword">string</span>)routeData.Values[<span class="hljs-string">"action"</span>];
<span class="hljs-keyword">var</span> routeControl = (<span class="hljs-keyword">string</span>)routeData.Values[<span class="hljs-string">"controller"</span>];
<span class="hljs-comment">// both must match</span>
<span class="hljs-keyword">var</span> returnActive = control == routeControl &&
action == routeAction;
<span class="hljs-keyword">return</span> returnActive ? <span class="hljs-string">"active"</span> : <span class="hljs-string">""</span>;
}
}</code></pre>
<p>Finally, in order for your view to access this method, you’ll have to make sure you import the namespace into your view using the razor syntax like this:</p>
<p><code><span class="highlight">@</span>using <i>YourProjectName</i>.Utilities</code></p>
<p>Run your project and the current page should be highlighted!</p>
<h3 id="closing-remarks">Closing Remarks</h3>
<p>You’ll notice that the login pages do not highlight when you navigate to the default account pages provided. See if you can use the info here to modify the <code>_loginPartial</code> page in the Shared Layout section. If you get stuck, you can look at the full demo below.</p>
<p><a href="https://twitter.com/ChrisJWay">Chris Way</a> has a great blog post on <a href="http://chrisondotnet.com/2012/08/setting-active-link-twitter-bootstrap-navbar-aspnet-mvc/">Setting the active link in a Twitter Bootstrap Navbar in ASP.NET MVC</a>. He comes up with a single method to generate the <code><li></code> element and the <code><a></code> element nested inside of it since there is largely redundant routing info. I’ve opted away from that for maximal flexibility as it locks you into a single method for producing links, but it does provide a terser inline syntax if that’s all you need to do.</p>
<p>Also, a lot of the basis for this code was taken from the StackOverflow question <a href="http://stackoverflow.com/q/20410623/1366033">How to add <code>active</code> class to <code>Html.ActionLink</code> in ASP.NET MVC</a></p>
<h3 id="source-code">Source Code:</h3>
<p>You can view the full working solution on GitHub in both VB and C#</p>
<p><a href="https://github.com/KyleMit/CodingEverything/tree/master/MVCBootstrapNavbar">https://github.com/KyleMit/CodingEverything/tree/master/MVCBootstrapNavbar</a></p>
<hr>
<p><a href="http://www.codingeverything.com/2014/05/mvcbootstrapactivenavbar.html">Permalink to article</a> - Published with markdown via <a href="https://stackedit.io/viewer#!url=https://raw.githubusercontent.com/KyleMit/CodingEverything/master/MVCBootstrapNavbar/MVCBootstrapNavbar.md">stackedit.io</a></p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com55tag:blogger.com,1999:blog-8252867149515968674.post-86264865123069312402014-02-28T18:30:00.002-08:002014-10-03T09:10:03.660-07:00Bootstrap Docs Sidebar Explained<p>Do you really want to make a side navbar look just like the one from the Bootstrap Documentation pages? Then this is the article for you.</p>
<p>In addition to really liking the bootstrap library, I have to say that I really enjoy the presentational style of their documentation page. I think it’s a great idea to show off the big picture of a document, while automatically expanding each section when appropriate. It’s especially helpful for long documents that require a lot of scrolling. Unfortunately, they don’t specifically outline how their documentation pages are put together. But since they’re just delivering HTML/CSS/JS, we can reverse engineer how they put it together. Here are the results of dissecting Bootstrap’s Doc’s side nav bar.</p>
<h3 id="SimpleContent">Simple Content</h3>
<p>This really works best with some content to scroll through, so let’ just start by making a bunch of blocks with unique ID tags, that are nested into sections, that we can visually see. </p>
<p>Here’s how: <br>
Create a few blocks that look like this. Copy and paste it, but every time you see the letter A, replace it with B, C, and so on.</p>
<pre class="prettyprint"><code class="language-HTML hljs xml"><span class="hljs-tag"><<span class="hljs-title">section</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"GroupA"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"group"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">h3</span>></span>Group A<span class="hljs-tag"></<span class="hljs-title">h3</span>></span>
<span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"GroupASub1"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"subgroup"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">h4</span>></span>Group A Sub 1<span class="hljs-tag"></<span class="hljs-title">h4</span>></span>
<span class="hljs-tag"></<span class="hljs-title">div</span>></span>
<span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"GroupASub2"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"subgroup"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">h4</span>></span>Group A Sub 2<span class="hljs-tag"></<span class="hljs-title">h4</span>></span>
<span class="hljs-tag"></<span class="hljs-title">div</span>></span>
<span class="hljs-tag"></<span class="hljs-title">section</span>></span></code></pre>
<p>Then style the blocks to give them a little bit of spacing. This is just so we don’t have to wade through hundreds of lines of lorem ipsum (yuk!).</p>
<pre class="prettyprint"><code class="language-CSS hljs css"><span class="hljs-class">.group</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">background</span>:<span class="hljs-value"> yellow</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">width</span>:<span class="hljs-value"> <span class="hljs-number">200</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">height</span>:<span class="hljs-value"> <span class="hljs-number">500</span>px</span></span>;
<span class="hljs-rule">}</span></span>
<span class="hljs-class">.group</span> <span class="hljs-class">.subgroup</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">background</span>:<span class="hljs-value"> orange</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">width</span>:<span class="hljs-value"> <span class="hljs-number">150</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">height</span>:<span class="hljs-value"> <span class="hljs-number">200</span>px</span></span>;
<span class="hljs-rule">}</span></span></code></pre>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/1/embedded/result,html,css" allowfullscreen="allowfullscreen"></iframe>
<h3 id="SimpleLayout">Simple Layout</h3>
<p>Now we need to create a two column layout for our page. We can put the sample body content on the right and the navbar on the left. We can do this with the <a href="http://getbootstrap.com/css/#grid">Bootstrap Grid System</a>, by placing both columns inside of a div with <code>class='row'</code> and specifying the column widths for all devices with <code>col-xs-*</code>. Finally when we scroll down, we want the content to freely scroll, but have the navigation bar remain in the same place, so we’ll use <code>position: fixed</code> to pin it to the top. It should look something like this:</p>
<pre class="prettyprint"><code class="language-HTML hljs xml"><span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"row"</span>></span>
<span class="hljs-comment"><!--Nav Bar --></span>
<span class="hljs-tag"><<span class="hljs-title">nav</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"col-xs-3"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"fixed"</span>></span>
Nav Placeholder<span class="hljs-tag"><<span class="hljs-title">br</span>/></span>
Stays on Top!
<span class="hljs-tag"></<span class="hljs-title">div</span>></span>
<span class="hljs-tag"></<span class="hljs-title">nav</span>></span>
<span class="hljs-comment"><!--Main Content --></span>
<span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"col-xs-9"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">section</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"GroupA"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"group"</span>></span><span class="hljs-tag"></<span class="hljs-title">section</span>></span>
<span class="hljs-tag"><<span class="hljs-title">section</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"GroupB"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"group"</span>></span><span class="hljs-tag"></<span class="hljs-title">section</span>></span>
<span class="hljs-tag"><<span class="hljs-title">section</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"GroupC"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"group"</span>></span><span class="hljs-tag"></<span class="hljs-title">section</span>></span>
<span class="hljs-tag"></<span class="hljs-title">div</span>></span>
<span class="hljs-tag"></<span class="hljs-title">div</span>></span></code></pre>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/2/embedded/result,html,css" allowfullscreen="allowfullscreen"></iframe>
<h3 id="SimpleNavList">Simple Navlist</h3>
<p>Before we even add any bootstrap, we need a bare bones list of links that will navigate to content on the page. This provides a nice tree structure, but no formatting. The links have bulky bullets in front of them and are very brightly colored.</p>
<pre class="prettyprint"><code class="language-HTML hljs xml"><span class="hljs-tag"><<span class="hljs-title">ul</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"sidebar"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"#GroupA"</span>></span>Group A<span class="hljs-tag"></<span class="hljs-title">a</span>></span>
<span class="hljs-tag"><<span class="hljs-title">ul</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span><span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"#GroupASub1"</span>></span>Sub-Group 1<span class="hljs-tag"></<span class="hljs-title">a</span>></span><span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span><span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"#GroupASub2"</span>></span>Sub-Group 2<span class="hljs-tag"></<span class="hljs-title">a</span>></span><span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"></<span class="hljs-title">ul</span>></span>
<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-comment"><!-- Same for Group B & C --></span>
<span class="hljs-tag"></<span class="hljs-title">ul</span>></span></code></pre>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/3/embedded/result,html,css" allowfullscreen="allowfullscreen"></iframe>
<h3 id="ListToNav">Converting List to Nav</h3>
<p>In order to make the links look more like navigation controls, and less like a list of groceries, we can use the set of <a href="http://getbootstrap.com/components/#nav">Nav classes</a> provided by Bootstrap. In order to use any nav class, you must also attach the <code>.nav</code> base class to that element as well. To make sure the list stays vertical, we want to also add the class <code>.nav-stacked</code> to each <code>ul</code> element. </p>
<pre class="prettyprint"><code class="XML hljs xml"><span class="hljs-tag"><<span class="hljs-title">ul</span> </span><span class="highlight"><span class="hljs-tag"><span class="hljs-attribute">class</span>=<span class="hljs-value">"nav nav-stacked fixed"</span></span></span><span class="hljs-tag"> <span class="hljs-attribute">id</span>=<span class="hljs-value">"sidebar"</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"#GroupA"</span>></span>Group A<span class="hljs-tag"></<span class="hljs-title">a</span>></span>
<span class="hljs-tag"><<span class="hljs-title">ul</span> </span><span class="highlight"><span class="hljs-tag"><span class="hljs-attribute">class</span>=<span class="hljs-value">"nav nav-stacked"</span></span></span><span class="hljs-tag"> ></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span><span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"#GroupASub1"</span>></span>Sub-Group 1<span class="hljs-tag"></<span class="hljs-title">a</span>></span><span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"><<span class="hljs-title">li</span>></span><span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"#GroupASub2"</span>></span>Sub-Group 2<span class="hljs-tag"></<span class="hljs-title">a</span>></span><span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-tag"></<span class="hljs-title">ul</span>></span>
<span class="hljs-tag"></<span class="hljs-title">li</span>></span>
<span class="hljs-comment"><!-- Same for Group B & C --></span>
<span class="hljs-tag"></<span class="hljs-title">ul</span>></span>
</code></pre>
<p>Let’s pause to look at just these changes because a lot has changed visually even though we haven’t added much code. The set of nav classes help strip out some of the default formatting associated with unordered lists and instead renders the links much like menu bars. The elements now all align all the way on the left because <code>.nav</code> sets <code>padding-left: 0;</code>. We’ve removed the underline with <code>text-decoration: none;</code>, removed some of the list formatting with <code>list-style: none;</code>, and softened the colors a bit with <code>color: <span>#428bca;</span></code>. The <code>nav-stacked</code> floats all the elements to the left so they ‘stack’ on top of each other. </p>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/4/embedded/result,html,css" allowfullscreen="allowfullscreen"></iframe>
<h3 id="FormatNavBar">Formatting the Nav Bar</h3>
<p>The final change to the markup is just to add the class <code>bs-docs-sidebar</code> to the top nav column div to help identify it in CSS. We can do the rest in CSS and JavaScript. <br>
First, let’s give the navbar a little breathing room by giving it margins on the top, left, and bottom:</p>
<pre class="prettyprint"><code class="language-css hljs "><span class="hljs-comment">/* sidebar */</span>
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">padding-left</span>:<span class="hljs-value"> <span class="hljs-number">20</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">margin-top</span>:<span class="hljs-value"> <span class="hljs-number">20</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">margin-bottom</span>:<span class="hljs-value"> <span class="hljs-number">20</span>px</span></span>;
<span class="hljs-rule">}</span></span></code></pre>
<p>Next, we’d like to be able to apply different formating to parent level links and child links. CSS does not currently have a <a href="http://stackoverflow.com/a/1014958/1366033">Parent Selector</a> which could be used to differentiate the top level links with those nested below them. Instead, we can apply a style to all links inside of <code>bs-docs-sidebar</code> and then override that style for any list items that are children of two <code>ul.nav</code> elements.</p>
<pre class="prettyprint"><code class="language-css hljs "><span class="hljs-comment">/* all links */</span>
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span>><span class="hljs-tag">li</span>><span class="hljs-tag">a</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">color</span>:<span class="hljs-value"> <span class="hljs-hexcolor">#999</span></span></span>;
<span class="hljs-rule"><span class="hljs-attribute">padding</span>:<span class="hljs-value"> <span class="hljs-number">4</span>px <span class="hljs-number">20</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">font-size</span>:<span class="hljs-value"> <span class="hljs-number">13</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">font-weight</span>:<span class="hljs-value"> <span class="hljs-number">400</span></span></span>;
<span class="hljs-rule">}</span></span>
<span class="hljs-comment">/* nested links */</span>
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span> <span class="hljs-class">.nav</span>><span class="hljs-tag">li</span>><span class="hljs-tag">a</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">padding-top</span>:<span class="hljs-value"> <span class="hljs-number">1</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">padding-bottom</span>:<span class="hljs-value"> <span class="hljs-number">1</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">padding-left</span>:<span class="hljs-value"> <span class="hljs-number">30</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">font-size</span>:<span class="hljs-value"> <span class="hljs-number">12</span>px</span></span>;
<span class="hljs-rule">}</span></span></code></pre>
<p>For all links we’ll apply a grey color schema and a font-weight of 400. All links will be padded in at least 20 pixels, but those nested under two <code>.nav</code> elements will be indented 30px. Top level links will be slightly larger at <code>13px</code>. And nested links will have much less padding on the top and bottom.</p>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/5/embedded/result,css,html" allowfullscreen="allowfullscreen"></iframe>
<h3 id="ScrollSpy">Using Scrollspy</h3>
<p>To do the rest of the styling we’ll need to know whether a link is active. In order to do this, we can use a <a href="http://getbootstrap.com/javascript/#scrollspy">scroll spy</a> on the page which will apply the <code>.active</code> class to the navigation list whenever a given element is scrolled into view. <br>
Scroll spy is called on the element whose scrolling activity you want to monitor. Since you will probably be scrolling through the entire page, this should go on the <code>body</code> element. <br>
The <a href="http://getbootstrap.com/javascript/#scrollspy-usage" id="Update3">target of scrollspy is</a>: </p>
<blockquote>
<p>the ID or class of the <strong><em>parent</em></strong> element of any Bootstrap <code>.nav</code> component.</p>
</blockquote>
<p>So we’ll target the <code>#sidebar</code> by passing in it’s parent: <code>.bs-docs-sidebar</code></p>
<p>The offset represents the <code>pixels to offset from top when calculating position of scroll.</code> We’ll give it a running start of 40, so it can find the first nested child item of each group so that will be set as active as well.</p>
<pre class="prettyprint"><code class="language-js hljs ">$(<span class="hljs-string">'body'</span>).scrollspy({
target: <span class="hljs-string">'.bs-docs-sidebar'</span>,
offset: <span class="hljs-number">40</span>
});</code></pre>
<p>You are still in charge of styling any elements you would like to display. Scroll spy merely adds the <code>active</code> class. As of right now, it won’t look like it’s doing anything because we haven’t styled the elements yet. As a placeholder, just to see it working, let’s color active links purple. We’ll replace this with more sophisticated stuff next.</p>
<pre class="prettyprint"><code class="language-css hljs "><span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span>><span class="hljs-class">.active</span>><span class="hljs-tag">a</span> <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">color</span>:<span class="hljs-value"> <span class="hljs-hexcolor">#563d7c</span></span></span>; <span class="hljs-rule">}</span></span></code></pre>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/48/embedded/result,js,html,css" allowfullscreen="allowfullscreen"></iframe>
<h3 id="ActiveLinks">Styling Active Links</h3>
<p>Whenever an element is set to active (due to scrollspy) or is hovered or focused, we’ll apply some styles to the anchor. We’ll color it purple. We’ll make sure that it doesn’t have an underline or a grey box highlighting it. And we’ll add a purple flag on the left to help identify which items are active. To do this, we’ll apply a 2 pixel border to the left of the element. </p>
<blockquote>
<p><strong>Note</strong>: Because of the way the CSS box model works, when we add a 2px border to the left, the entire element shifts 2 pixels to the right, displaced by the border that previously took up zero pixels. One way to handle this is to shorten the padding we added by 2px every time the element is active. But I think a cooler trick is to just start off with a transparent 2px border so the object does not get resized when adding a colorful border</p>
</blockquote>
<pre class="prettyprint"><code class="language-css hljs "><span class="hljs-comment">/* all links */</span>
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span>><span class="hljs-tag">li</span>><span class="hljs-tag">a</span> <span class="hljs-rules">{
<span class="hljs-comment">/*add trasnparent border */</span>
<span class="hljs-rule"><span class="hljs-attribute">border-left</span>:<span class="hljs-value"> <span class="hljs-number">2</span>px solid transparent</span></span>;
<span class="hljs-rule">}</span></span>
<span class="hljs-comment">/* active & hover links */</span>
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span>><span class="hljs-class">.active</span>><span class="hljs-tag">a</span>,
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span>><span class="hljs-tag">li</span>><span class="hljs-tag">a</span><span class="hljs-pseudo">:hover</span>,
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span>><span class="hljs-tag">li</span>><span class="hljs-tag">a</span><span class="hljs-pseudo">:focus</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">color</span>:<span class="hljs-value"> <span class="hljs-hexcolor">#563d7c</span></span></span>;
<span class="hljs-rule"><span class="hljs-attribute">text-decoration</span>:<span class="hljs-value"> none</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">background-color</span>:<span class="hljs-value"> transparent</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">border-left</span>:<span class="hljs-value"> <span class="hljs-number">2</span>px solid <span class="hljs-hexcolor">#563d7c</span></span></span>;
<span class="hljs-rule">}</span></span></code></pre>
<p>Let’s also make active parent links have a very thick weight, and child links less so. <br>
<strong>Remember</strong>: we’ll use the style we want for parent objects on all the links and then override it for nested links.</p>
<pre class="prettyprint"><code class="language-css hljs "><span class="hljs-comment">/* all active links */</span>
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span>><span class="hljs-class">.active</span>><span class="hljs-tag">a</span>,
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span>><span class="hljs-class">.active</span><span class="hljs-pseudo">:hover</span>><span class="hljs-tag">a</span>,
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span>><span class="hljs-class">.active</span><span class="hljs-pseudo">:focus</span>><span class="hljs-tag">a</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">font-weight</span>:<span class="hljs-value"> <span class="hljs-number">700</span></span></span>;
<span class="hljs-rule">}</span></span>
<span class="hljs-comment">/* nested active links */</span>
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span> <span class="hljs-class">.nav</span>><span class="hljs-class">.active</span>><span class="hljs-tag">a</span>,
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span> <span class="hljs-class">.nav</span>><span class="hljs-class">.active</span><span class="hljs-pseudo">:hover</span>><span class="hljs-tag">a</span>,
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span> <span class="hljs-class">.nav</span>><span class="hljs-class">.active</span><span class="hljs-pseudo">:focus</span>><span class="hljs-tag">a</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">font-weight</span>:<span class="hljs-value"> <span class="hljs-number">500</span></span></span>;
<span class="hljs-rule">}</span></span></code></pre>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/49/embedded/result,css,js,html" allowfullscreen="allowfullscreen"></iframe>
<h3 id="CollapsingGroups">Collapsing Inactive SubGroups</h3>
<p>One of my favorite features of the Bootstrap Navbar is that it automatically collapses subgroups that are not currently in view. This allows a lot of information to be available, but prevents a lot of noise when it’s not in use. To do this we’ll use the active flag on the parent group. To hide all subgroups, we’ll set <code>display:none</code> to all <code>ul.nav</code> elements that are children of other <code>.nav</code> elements. This will collapse all subgroups. Then we need to expand the active group by looking for a parent level nav with an <code>.active</code> child and set <code>display:block</code> on it’s child ul. So it will look like this:</p>
<pre class="prettyprint"><code class="css hljs "><span class="hljs-comment">/* hide all (inactive) nested list */</span>
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span> <span class="hljs-tag">ul</span><span class="hljs-class">.nav</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">display</span>:<span class="hljs-value"> none</span></span>;
<span class="hljs-rule">}</span></span>
<span class="hljs-comment">/* show active nested list */</span>
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-class">.nav</span><span class="highlight">><span class="hljs-class">.active</span>></span><span class="hljs-tag">ul</span><span class="hljs-class">.nav</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">display</span>:<span class="hljs-value"> block</span></span>;
<span class="hljs-rule">}</span></span>
</code></pre>
<p>And behave like this:</p>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/50/embedded/result,css,js,html" allowfullscreen="allowfullscreen"></iframe>
<h3 id="WrapUp">Wrap Up</h3>
<p>So that’s it. You can have fun applying other styles as well. Bootstrap uses <a href="http://getbootstrap.com/javascript/#affix">Affix</a> to lock the navbar into place after scrolling past the header. <br>
Also, they use media queries to collapse the navbar if the screen is below a certain size like this:</p>
<pre class="prettyprint"><code class="language-css hljs "><span class="hljs-at_rule">@<span class="hljs-keyword">media</span> screen and (max-width: <span class="hljs-number">500</span>px)</span>{
<span class="hljs-class">.bs-docs-sidebar</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">display</span>:<span class="hljs-value">none
</span></span></span>}
}</code></pre>
<p>The impetus for this article was actually a spice website that I was making to catalog my home spices. It uses the bootstrap side bar when space allows, but then converts into a top navbar for smaller screens. You can view the final page here:</p>
<p><a href="http://kylemitofsky.com/Spices/">http://kylemitofsky.com/Spices/</a></p>
<p>And you you can browse the source code here if you’re interested in how anything is done:</p>
<p><a href="https://github.com/KyleMit/Spices/tree/gh-pages">https://github.com/KyleMit/Spices/tree/gh-pages</a></p>
<p>Here’s the final fiddle. Feel free to play around with it, fork it, or leave me a comment below.</p>
<iframe width="100%" height="600" src="http://jsfiddle.net/KyleMit/v6zhz/embedded/result,css,js,html" allowfullscreen="allowfullscreen"></iframe>
<h3 id="WithTopNavBar">Update with Top NavBar:</h3>
<p>Here’s a quick rundown of how to add a <strong>horizontal navbar</strong> to the example. The primary difficulty in adding any fixed position navbar to the top of the window is it will <strong>break</strong> all your anchor tags like so:</p>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/52/embedded/result,css,js,html" allowfullscreen="allowfullscreen"></iframe>
<p>The first trick when adding a navbar is to displace everything on the page by the same number of pixels, that way nothing starts off hidden underneath the navbar. The standard implementation (even listed in the <a href="http://getbootstrap.com/components/#navbar-fixed-top">docs</a>) is to just offset the entire document by placing a top margin or padding on the body element:</p>
<pre class="prettyprint"><code class="language-css hljs "><span class="hljs-tag">body</span> <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">margin-top</span>:<span class="hljs-value"><span class="hljs-number">50</span>px</span></span>; <span class="hljs-rule">}</span></span></code></pre>
<p>But as you can see from the previous example, this doesn’t solve the issue. </p>
<p>Why is that? </p>
<p>For more information, you can see my Stack Overflow answer to the question <a href="http://stackoverflow.com/a/18948554/1366033">When navigating to #id, element is hidden under fixed position header</a>, but here’s the gist of it. When the browser is told to navigate to a <a href="http://en.wikipedia.org/wiki/Fragment_identifier">fragment identifier</a> (#ID):</p>
<blockquote>
<p>your browser always wants to scroll your anchor to the exact top of the window. If you set your anchor where your text actually begins, it will be occluded by your menu bar.</p>
</blockquote>
<p>One way to overcome this is to make sure the content of your anchor element starts well after the element begins. To do this, we’ll need a basic understanding of the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/box_model">CSS box model</a>. We’ll give the element some extra height at the top by setting the <code>padding-top</code> to about 50px, but since we don’t actually want each anchor element to have 50 pixels of overhead, we’ll also set the <code>margin-top</code> the the same amount, but <strong>negative</strong>.</p>
<p>Here’s an example, that hopefully makes the point more concrete:</p>
<p>By adding this CSS</p>
<pre class="prettyprint"><code class="language-css hljs "><span class="hljs-class">.group</span>, <span class="hljs-class">.subgroup</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">padding-top</span>:<span class="hljs-value"> <span class="hljs-number">50</span>px</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">margin-top</span>:<span class="hljs-value"> -<span class="hljs-number">50</span>px</span></span>;
<span class="hljs-rule">}</span></span></code></pre>
<p>We make the element grow 50 pixels taller, but ensure that the content stays in exactly the same place. Here’s a look at the example from the chrome developer tools:</p>
<p><img src="http://i.imgur.com/oFCMEhF.png" alt="PaddingMarginOffset" title=""></p>
<p>Now when we scroll the top of the element to the top of the window, it will start 50 pixels before the content. Here’s a full example with a working top navbar</p>
<iframe width="100%" height="300" src="http://jsfiddle.net/KyleMit/v6zhz/53/embedded/result,css,js,html" allowfullscreen="allowfullscreen"></iframe>
<h3 id="ScrollabelNavbar">Update with Scrollable Navbar</h3>
<p>To make the sidebar scrollable, you can add the following CSS:</p>
<pre class="prettyprint"><code class="language-css hljs "><span class="hljs-class">.bs-docs-sidebar</span> > <span class="hljs-tag">ul</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">overflow-y</span>:<span class="hljs-value"> auto</span></span>;
<span class="hljs-rule"><span class="hljs-attribute">height</span>:<span class="hljs-value"> <span class="hljs-number">100</span>%</span></span>;
<span class="hljs-rule">}</span></span></code></pre>
<p>When content overflows it’s container, there are a couple different ways to <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/overflow">handle it in css</a>:</p>
<pre><code>overflow: visible | hidden | scroll | auto
</code></pre>
<p>The default is to have the content remain visible. However, this poses problems when using an element with <code>position:fixed</code> because you cannot simply scroll through the window to bring the visible element into view.</p>
<p>You can use <code>overflow: scroll</code> to add a scrollbar to the div, but this will always be visible, even if unnecessary, and scrollbars should be avoided unless absolutely necessary. The better option is to use <code>overflow: auto</code> which will provide a scrollbar only if necessary. Since we’d rather wrap long horizontal text than scroll it, we’ll only apply this to the <em>y-axis</em> by using <code>overflow-y: auto</code>.</p>
<p>You’ll notice once this is in place it doesn’t do anything yet. That’s because we need to tell the container how large it is so it knows when any of its contents are taking up more space that it can provide. As a test, you can just throw in <code>height: 100px</code> and you’ll notice that the entire contents fits into a box that is 100px tall and you can scroll to get to the rest of it.</p>
<p>However, we don’t necessarily know how much space we want to allow the sidebar to consume. It’s going to depend on the space available in the window and how you’re site is laid out. In the simplest form, if we alloted the entire screen height to the sidebar, we could use <code>height: 100<b>%</b></code>. </p>
<blockquote>
<p><strong>Note</strong>: Whenever you use <code>height: 100%</code> in CSS, you have to next ask yourself, “<em>100% of what?</em>” Often this is the parent element, but fixed position elements break the layout so 100% will automatically refer to the window size. If your sidebar does not start at the top of the window, 100% height will extend past the bottom of the screen and make the scrollbar difficult to manage. You can choose a height <100% or apply your own padding to the element, instead of its parent.</p>
</blockquote>
<p>Here’s a demo with a scrollable sidebar:</p>
<iframe width="100%" height="200" src="http://jsfiddle.net/KyleMit/v6zhz/89/embedded/result,css,js,html" allowfullscreen="allowfullscreen"></iframe>
<p>You can look at my <a href="http://kylemitofsky.com/Spices/">spice project</a> for a demo in production using a scrollable sidebar</p>
<h3 id="WithAffix">Update with Affix</h3>
<p>In the spirit of the Bootstrap’s own use of their sidebar, you can use <code>affix</code> to help place the location as you scroll through the page. You’ll just need to add the affix to your sidebar like this:</p>
<pre class="prettyprint"><code class=" hljs css">$("<span class="hljs-id">#sidebar</span>")<span class="hljs-class">.affix</span>(<span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">offset</span>:<span class="hljs-value"> {
top: <span class="hljs-number">60</span>
</span></span></span>}
});</code></pre>
<p>And then set some styling when the <code>.affix</code> class is in place (bootstrap will automatically add <code>position:fixed</code> so we just need to set the height:</p>
<pre class="prettyprint"><code class=" hljs css"><span class="hljs-id">#sidebar</span><span class="hljs-class">.affix</span> <span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">top</span>:<span class="hljs-value"> <span class="hljs-number">20</span>px</span></span>;
<span class="hljs-rule">}</span></span></code></pre>
<p>Here’s a demo with an affixed sidebar:</p>
<iframe width="100%" height="200" src="http://jsfiddle.net/KyleMit/v6zhz/170/embedded/result,css,js,html" allowfullscreen="allowfullscreen"></iframe>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com48tag:blogger.com,1999:blog-8252867149515968674.post-45065184186980435302014-01-12T21:23:00.000-08:002014-01-12T21:42:30.970-08:00Insert HTML, CSS, and JS into any Web Page with a Chrome Extension<h3 id="overview">Overview</h3>
<p>Chrome extensions have pretty good documentation and pretty good community support. Naturally, you might expect that between <a href="http://developer.chrome.com/extensions/index.html">docs</a> and <a href="http://stackoverflow.com/questions/tagged/google-chrome-extension?sort=votes&pageSize=50">forums</a> that you can get a small project up and running quickly. </p>
<p>Unfortunately, sometimes minor simple tasks can get lost in the weeds. Documentation usually only covers the basic, small, happy flow cases and forums usually only ask about difficult, large problems. Well what about issues of medium size and complexity? Leave that to the bloggers!</p>
<p>Here we’ll take a look at how to create a chrome extension that uses a <strong>Content Script</strong> to change the background color of any page using <a href="http://jquery.com/">jQuery</a>.</p><div class="se-section-delimiter"></div>
<h3 id="manifestjson">Manifest.json</h3>
<p>First things first. You need a way to tell chrome what your intentions are, what components come with your extension, and when they apply. To do this, we’ll use a <a href="http://developer.chrome.com/extensions/manifest.html">manifest file</a>.</p>
<p>The manifest file contains some JSON formatted data that Google knows how to read. There is a lot of boiler plate info that you can explore, but, for our purposes, we want to pay special attention to the <a href="https://developer.chrome.com/extensions/content_scripts.html">content script</a> section:</p>
<pre class="prettyprint"><code class="JSON json">{
"<span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute">manifest_version</span></span></span></span></span></span></span>": <span class="value"><span class="number"><span class="value"><span class="number"><span class="value"><span class="number"><span class="value"><span class="number"><span class="value"><span class="number"><span class="value"><span class="number"><span class="value"><span class="number">2</span></span></span></span></span></span></span></span></span></span></span></span></span></span>,
"<span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute">name</span></span></span></span></span></span></span>": <span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string">"Page Changer"</span></span></span></span></span></span></span></span></span></span></span></span></span></span>,
"<span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute">version</span></span></span></span></span></span></span>": <span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string">"0.1"</span></span></span></span></span></span></span></span></span></span></span></span></span></span>,
"<span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute">description</span></span></span></span></span></span></span>": <span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string"><span class="value"><span class="string">"Page Changer"</span></span></span></span></span></span></span></span></span></span></span></span></span></span>,
"<span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute">icons</span></span></span></span></span></span></span>": <span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">{ "</span></span></span></span></span></span><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute">16</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">": </span></span></span></span></span></span><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string">"Icons/Logo16.png"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">,
"</span></span></span></span></span></span><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute">32</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">": </span></span></span></span></span></span><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string">"Icons/Logo32.png"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">,
"</span></span></span></span></span></span><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute">48</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">": </span></span></span></span></span></span><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string">"Icons/Logo48.png"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">,
"</span></span></span></span></span></span><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute">128</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">": </span></span></span></span></span></span><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string">"Icons/Logo128.png"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"> </span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">}</span></span></span></span></span></span></span>,
<b>"<span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute"><span class="attribute">content_scripts</span></span></span></span></span></span></span>": <span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">[
{
"</span></span></span></span></span></span><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute">matches</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">": </span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">[</span></span></span></span></span></span></span></span></span></span></span></span><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string">"*://*/*"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">]</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">,
"</span></span></span></span></span></span><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute"><span class="value"><span class="attribute">js</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">": </span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">[</span></span></span></span></span></span></span></span></span></span></span></span><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string">"jquery.min.js"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">, </span></span></span></span></span></span></span></span></span></span></span></span><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string"><span class="value"><span class="value"><span class="string">"script.js"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">]
</span></span></span></span></span></span></span></span></span></span></span></span></span><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">}
]</span></span></span></span></span></span></span></b><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value"><span class="value">
</span></span></span></span></span></span></span>}
</code></pre>
<p>The content script must specify two things:</p>
<ol>
<li>The pages against which to run the code</li>
<li>The external code files to run.</li>
</ol>
<p>In this case, we’ve specified that we want to our content to run whenever the web url matches the pattern <code>*://*/*</code>, which is to say for all protocols, for all domains, for all pages (<em>in other words, run everywhere</em>). </p>
<p>If you just wanted to target google.com or any of it’s content pages, then you could put that there.</p>
<p>The second thing we’ve specified is which files to run and in which order. First we’ll load jQuery from a local copy that we deploy, and next we’ll run a file called <code>script.js</code> that we’ll look at in a second.</p><div class="se-section-delimiter"></div>
<h3 id="scriptjs">Script.js</h3>
<p>In this simplified example, let’s say we just want to change the background color of the current page for a very obvious example of whether or not all the right components have been called. In a real world example, you might want to find all the links in the page and turn them a particular color. Or attach some code to any images on the page to allow you to easily email them. Whatever you want!</p>
<p>For now, let’s just run the following script which will find the <code>body</code> element and use the <code>css()</code> method in jQuery to apply the value <code>blue</code> to the <code>background</code> property:</p><div class="se-section-delimiter"></div>
<pre class="prettyprint"><code class="javascript">$(<span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword"><span class="function"><span class="keyword">function</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params"><span class="function"><span class="params">()</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"><span class="function"> {</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
$(<span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string">"body"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>).css(<span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string">"background"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>,<span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string">"blue"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>);
});</code></pre>
<p><strong>Note:</strong> Just so I don’t get comments about it. If this was the only change you wanted to make to the page, you can and <em>should</em> do this purely with CSS. The idea was to add jQuery to a page in a trivial example so you can implement your own functionality later.</p>
<h3 id="deployment">Deployment</h3>
<p>To run the extension, do the following steps:</p>
<ol>
<li>In Chrome, Open the Browser <strong>Menu</strong> <i class="icon-menu"></i></li>
<li>Click on <strong>Tools</strong>, and then <strong>Extensions</strong></li>
<li>Make sure <strong>Developer Mode</strong> is checked <em>(usually in the top right)</em></li>
<li>Click <strong>Load Unpacked Extension</strong> and select the folder that contains your manfiest and extension files.</li>
</ol>
<p>That’s it! Go to any affected page and hit refresh to see your changes applied:</p>
<p><img src="http://i.imgur.com/Tu4PuzP.png" alt="Demo" title=""></p>
<p>To get started, you can download all the files you need to run this extension from the <a href="http://sdrv.ms/1aw1ZCG">SkyDrive</a> folder below.</p>
<p><iframe src="https://skydrive.live.com/embed?cid=9935281CD314F220&resid=9935281CD314F220%2136029&authkey=AFQnetIgn4vZY_0" width="165" height="128" frameborder="0" scrolling="no"></iframe><br></p>
<p>If you’ve made changes and are happy with them, you can even deploy to the <a href="https://chrome.google.com/webstore/category/extensions">Chrome Extension Store</a> for other people to use after getting a <a href="https://chrome.google.com/webstore/developer/dashboard">developer account</a>.</p>
<p>Here’s a couple of extensions I’ve built for chrome, along with their source code if you’re looking for examples. Feel free to leave comments on GitHub with any suggestions or bugs using GitHub’s issue tracker.</p>
<ul>
<li><strong>Link Finder</strong> - Find all links to named anchors within the page so you can create descriptive links to content within a page <br>
<ul><li><a href="https://chrome.google.com/webstore/detail/link-finder/jnodcgnkggmnlmcgibanbaeagpjckjnd">Store Link</a></li>
<li><a href="https://github.com/KyleMit/LinkFinder">Git Hub</a></li></ul></li>
<li><strong>Copy Tabs</strong> - Creates a keyboard shortcut (<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>C</kbd>) which copies links for selected tabs <br>
<ul><li><a href="https://chrome.google.com/webstore/detail/copy-tabs/efmfdbcopjfmgoefenohhlcplheclbeo">Store Link</a></li>
<li><a href="https://github.com/KyleMit/ChromeCopyTabs">Git Hub</a></li></ul></li>
</ul>
<hr>
<p>Upcoming: Part 2 - How to Insert HTML, CSS, and JavaScript into any page using a browser action.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com257tag:blogger.com,1999:blog-8252867149515968674.post-5469884158864888482013-12-31T14:50:00.000-08:002014-01-01T15:10:26.421-08:00SQL Like 'In' Function For .NET<p>SQL has a wonderful terse and natural syntax for checking if an item is inside of a collection by using the <a href="http://technet.microsoft.com/en-us/library/ms177682.aspx"><strong><code>IN</code></strong> keyword</a>. Which can be expressed like this:</p>
<pre class="prettyprint"><code class="SQL lasso"><i>expression</i> <span class="keyword">IN</span> (<i>value_1</i>, <i>value_2</i>, <i><span class="attribute">...</span><span class="built_in">.</span> value_n</i>)
</code></pre>
<p>In .NET, we can preform the same operation with <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.contains.aspx"><code>.Contains()</code></a> method on any enumerable collection which:</p>
<blockquote>
<p>Determines whether a sequence contains a specified element </p>
</blockquote>
<p>Beginning with VB 2010, we can even do this on the fly by using the <a href="http://msdn.microsoft.com/en-us/library/dd293617%28v=VS.100%29.aspx">Collection Initializers</a>.</p>
<p>For example, to check whether a variable called <code>personName</code> was either <code>"Sally"</code> or <code>"Jenny"</code>, we could use the following expression:</p><div class="se-section-delimiter"></div>
<pre class="prettyprint"><code class="language-vbnet">{<span class="string"><span class="string">"Sally"</span></span>,<span class="string"><span class="string">"Jenny"</span></span>}.Contains(personName)</code></pre>
<p><strong>However</strong>, I think this syntax leaves something to be desired. The verbiage is all wrong. I don’t really care if a collection <em>contains</em> some item. I care if an item is <em>in</em> a collection. Of course, logically, this is performing the same operation, but I want the personName variable to be in the drivers seat. I want <code>personName</code> to be the subject that is verbing against the other items. </p>
<p>For a bit of syntactic sugar, we can add a generic extension method to take in an <code>ParamArray</code> and check if the extended element falls inside that array.</p>
<h4 id="heres-the-in-method">Here’s the <strong>In</strong> method:</h4>
<p><em>Note</em>: <code>In</code> needs to be inside of square brackets because it is a <a href="http://msdn.microsoft.com/en-us/library/ksh7h19t%28v=vs.90%29.aspx">Protected Keyword</a>.</p>
<h3 class="vbColor" id="visual-basic">Visual Basic</h3>
<pre class="prettyprint"><code class="vbnet"><span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">'''</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><summary></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">'''</span></span></span><span class="comment"> Determines if the Item is contained within the listed array</span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">'''</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></summary></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">'''</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><typeparam name="T"></span></span></span><span class="comment">The type of object</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></typeparam></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">'''</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><param name="item"></span></span></span><span class="comment">The calling item</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></param></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">'''</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><param name="range"></span></span></span><span class="comment">An array or comma separated list of the items to check against the calling</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></param></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">'''</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><returns></span></span></span><span class="comment">True if item is found in list</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></returns></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">'''</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><remarks></span></span></span><span class="comment">Provides syntatic sugar by reordering the subject of the IEnumerable.Contains method</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></remarks></span></span></span></span>
<Extension()>
<span class="keyword"><span class="keyword">Public</span></span> <span class="keyword"><span class="keyword">Function</span></span> [<span class="keyword"><span class="keyword">In</span></span>](<span class="keyword"><span class="keyword">Of</span></span> T)(<span class="keyword"><span class="keyword">ByVal</span></span> item <span class="keyword"><span class="keyword">As</span></span> T, <span class="keyword"><span class="keyword">ByVal</span></span> <span class="keyword"><span class="keyword">ParamArray</span></span> range() <span class="keyword"><span class="keyword">As</span></span> T) <span class="keyword"><span class="keyword">As</span></span> <span class="built_in"><span class="built_in">Boolean</span></span>
<span class="keyword"><span class="keyword">Return</span></span> range.Cast(<span class="keyword"><span class="keyword">Of</span></span> T).Contains(item)
<span class="keyword"><span class="keyword">End</span></span> <span class="keyword"><span class="keyword">Function</span></span></code></pre>
<h3 class="csColor" id="c-sharp">C Sharp</h3>
<pre class="prettyprint"><code class="cs"><span class="keyword"><span class="keyword">public</span></span> <span class="keyword"><span class="keyword">static</span></span> <span class="keyword"><span class="keyword">class</span></span> Extensions
{
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">///</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><summary></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">///</span></span></span><span class="comment"> Determines if the Item is contained within the listed array</span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">///</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></summary></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">///</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><typeparam name="T"></span></span></span><span class="comment">The type of object</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></typeparam></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">///</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><param name="item"></span></span></span><span class="comment">The calling item</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></param></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">///</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><param name="range"></span></span></span><span class="comment">An array or comma separated list of the items to check against the calling</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></param></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">///</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><returns></span></span></span><span class="comment">True if item is found in list</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></returns></span></span></span></span>
<span class="comment"><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag">///</span></span></span><span class="comment"> </span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"><remarks></span></span></span><span class="comment">Provides syntatic sugar by reordering the subject of the IEnumerable.Contains method</span><span class="xmlDocTag"><span class="comment"><span class="xmlDocTag"></remarks></span></span></span></span>
<span class="keyword"><span class="keyword">public</span></span> <span class="keyword"><span class="keyword">static</span></span> <span class="keyword"><span class="keyword">bool</span></span> In<T>(<span class="keyword"><span class="keyword">this</span></span> T item, <span class="keyword"><span class="keyword">params</span></span> T[] range)
{
<span class="keyword"><span class="keyword">return</span></span> range.Contains(item);
}
}</code></pre>
<p>Throw this inside any module in your assembly, preferably one named something like <code>Utilities</code> or <code>ExtensionMethods</code>. Now we can call like this:</p>
<pre class="prettyprint"><code class="language-vbnet">personName.<span class="keyword"><span class="keyword">In</span></span>(<span class="string"><span class="string">"Sally"</span></span>,<span class="string"><span class="string">"Jenny"</span></span>)</code></pre>
<p>If you’re checking against a predefined list you can pass that in as a parameter and cast back into an array.</p>
<p>Personally, I take this utility method with me wherever I go. I find it incredibly helpful for checking if an item exists within a defined range. Once you start using it, you won’t stop, and I think that’s a good thing! For my money, it substantially improves readability, especially if you find yourself working on older code bases without collection initializers.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com5tag:blogger.com,1999:blog-8252867149515968674.post-52928543438699413212013-12-19T14:06:00.002-08:002014-01-01T15:07:58.087-08:00List (Of LINQ Enumerable Methods)<div><style type="text/css">
strong em {
background: yellow
}
</style></div>
<p>Here’s a grouped listing of all the methods available on the IEnumerable Class. <br>
Methods which can be composed using VB Query Syntax are generally listed first and are also <strong><em>highlighted yellow</em></strong>. </p><div class="se-section-delimiter"></div>
<h4 class="tinyHeader" id="projection-operations"><a href="http://msdn.microsoft.com/en-us/library/bb546168.aspx">Projection Operations</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb548891%28v=vs.110%29.aspx#"><strong><em>Select</em></strong></a> - Projects each element of a sequence into a new form.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534336%28v=vs.110%29.aspx#"><strong><em>SelectMany</em></strong></a> - Projects each element of a sequence to an IEnumerable(Of T) and flattens the resulting sequences into one sequence.</li>
</ul><div class="se-section-delimiter"></div>
<h4 class="tinyHeader" id="partitioning-data"><a href="http://msdn.microsoft.com/en-us/library/bb546164.aspx">Partitioning Data</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb358985%28v=vs.110%29.aspx#"><strong><em>Skip</em></strong></a> - Bypasses a specified number of elements in a sequence and then returns the remaining elements.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb549075%28v=vs.110%29.aspx#"><strong><em>SkipWhile</em></strong></a> - Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb503062%28v=vs.110%29.aspx#"><strong><em>Take</em></strong></a> - Returns a specified number of contiguous elements from the start of a sequence.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534804%28v=vs.110%29.aspx#"><strong><em>TakeWhile</em></strong></a> - Returns elements from a sequence as long as a specified condition is true.</li>
</ul>
<h4 class="tinyHeader" id="join-operations"><a href="http://msdn.microsoft.com/en-us/library/bb397908.aspx">Join Operations</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534675%28v=vs.110%29.aspx#"><strong><em>Join</em></strong></a> - Correlates the elements of two sequences based on matching keys. The default equality comparer is used to compare keys.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534297%28v=vs.110%29.aspx#"><strong><em>GroupJoin</em></strong></a> - Correlates the elements of two sequences based on equality of keys and groups the results.</li>
</ul>
<h4 class="tinyHeader" id="grouping-data"><a href="http://msdn.microsoft.com/en-us/library/bb546139.aspx">Grouping Data</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534501%28v=vs.110%29.aspx#"><strong><em>GroupBy</em></strong></a> - Groups the elements of a sequence according to a specified key selector function.</li>
</ul>
<h4 class="tinyHeader" id="filtering-data"><a href="http://msdn.microsoft.com/en-us/library/bb546161.aspx">Filtering Data</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534803%28v=vs.110%29.aspx#"><strong><em>Where</em></strong></a> - Filters a sequence of values based on a predicate.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb360913%28v=vs.110%29.aspx#"><strong>OfType</strong></a> - Filters the elements of an IEnumerable depending on their ability to be cast to a specified type.</li>
</ul>
<h4 class="tinyHeader" id="sorting-data"><a href="http://msdn.microsoft.com/en-us/library/bb546145.aspx">Sorting Data</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534966%28v=vs.110%29.aspx#"><strong><em>OrderBy</em></strong></a> - Sorts the elements of a sequence in ascending order according to a key.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534855%28v=vs.110%29.aspx#"><strong><em>OrderByDescending</em></strong></a> - Sorts the elements of a sequence in descending order according to a key.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534743%28v=vs.110%29.aspx#"><strong><em>ThenBy</em></strong></a> - Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb534736%28v=vs.110%29.aspx#"><strong><em>ThenByDescending</em></strong></a> - Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb358497%28v=vs.110%29.aspx#"><strong>Reverse</strong></a> - Inverts the order of the elements in a sequence.</li>
</ul>
<h4 class="tinyHeader" id="aggregation-operations"><a href="http://msdn.microsoft.com/en-us/library/bb546138.aspx">Aggregation Operations</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb354760%28v=vs.110%29.aspx#"><strong><em>Average</em></strong></a> - Computes the average of a sequence of values.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb338038%28v=vs.110%29.aspx#"><strong><em>Count</em></strong></a> - Returns the number of elements in a sequence.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb353539%28v=vs.110%29.aspx#"><strong><em>LongCount</em></strong></a> - Returns an Int64 that represents the total number of elements in a sequence.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb335614%28v=vs.110%29.aspx#"><strong><em>Max</em></strong></a> - Returns the maximum value in a sequence of values.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb298087%28v=vs.110%29.aspx#"><strong><em>Min</em></strong></a> - Returns the minimum value in a sequence of values.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb298138%28v=vs.110%29.aspx#"><strong><em>Sum</em></strong></a> - Computes the sum of a sequence of values.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb548651%28v=vs.110%29.aspx#"><strong>Aggregate</strong></a> - Applies an accumulator function over a sequence.</li>
</ul>
<h4 class="tinyHeader" id="set-operations"><a href="http://msdn.microsoft.com/en-us/library/bb546153.aspx">Set Operations</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb348436%28v=vs.110%29.aspx#"><strong><em>Distinct</em></strong></a> - Returns distinct elements from a sequence by using the default equality comparer to compare values.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb300779%28v=vs.110%29.aspx#"><strong>Except</strong></a> - Produces the set difference of two sequences by using the default equality comparer to compare values.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb460136%28v=vs.110%29.aspx#"><strong>Intersect</strong></a> - Produces the set intersection of two sequences by using the default equality comparer to compare values.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb341731%28v=vs.110%29.aspx#"><strong>Union</strong></a> - Produces the set union of two sequences by using the default equality comparer.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb302894%28v=vs.110%29.aspx#"><strong>Concat</strong></a> - Concatenates two sequences.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/dd267698%28v=vs.110%29.aspx#"><strong>Zip</strong></a> - Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.</li>
</ul>
<h4 class="tinyHeader" id="quantifier-operations"><a href="http://msdn.microsoft.com/en-us/library/bb546128.aspx">Quantifier Operations</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb548541%28v=vs.110%29.aspx#"><strong>All</strong></a> - Determines whether all elements of a sequence satisfy a condition.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb337697%28v=vs.110%29.aspx#"><strong>Any</strong></a> - Determines whether a sequence contains any elements.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb352880%28v=vs.110%29.aspx#"><strong>Contains</strong></a> - Determines whether a sequence contains a specified element by using the default equality comparer.</li>
</ul>
<h4 class="tinyHeader" id="generation-operations"><a href="http://msdn.microsoft.com/en-us/library/bb546129.aspx">Generation Operations</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb360179%28v=vs.110%29.aspx#"><strong>DefaultIfEmpty</strong></a> - Returns the elements of the specified sequence or the type parameter’s default value in a singleton collection if the sequence is empty.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb341042%28v=vs.110%29.aspx#"><strong>Empty</strong></a> - Returns an empty IEnumerable(Of T) that has the specified type argument.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range%28v=vs.110%29.aspx#"><strong>Range</strong></a> - Generates a sequence of integral numbers within a specified range.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb348899%28v=vs.110%29.aspx#"><strong>Repeat</strong></a> - Generates a sequence that contains one repeated value.</li>
</ul>
<h4 class="tinyHeader" id="element-operations"><a href="http://msdn.microsoft.com/en-us/library/bb546140.aspx">Element Operations</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb291976%28v=vs.110%29.aspx#"><strong>First</strong></a> - Returns the first element of a sequence.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb340482%28v=vs.110%29.aspx#"><strong>FirstOrDefault</strong></a> - Returns the first element of a sequence, or a default value if the sequence contains no elements.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb358775%28v=vs.110%29.aspx#"><strong>Last</strong></a> - Returns the last element of a sequence.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb301849%28v=vs.110%29.aspx#"><strong>LastOrDefault</strong></a> - Returns the last element of a sequence, or a default value if the sequence contains no elements.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb299233%28v=vs.110%29.aspx#"><strong>ElementAt</strong></a> - Returns the element at a specified index in a sequence.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb494386%28v=vs.110%29.aspx#"><strong>ElementAtOrDefault</strong></a> - Returns the element at a specified index in a sequence or a default value if the index is out of range.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb155325%28v=vs.110%29.aspx#"><strong>Single</strong></a> - Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb342451%28v=vs.110%29.aspx#"><strong>SingleOrDefault</strong></a> - Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.</li>
</ul>
<h4 class="tinyHeader" id="equality-operations"><a href="http://msdn.microsoft.com/en-us/library/bb546160.aspx">Equality Operations</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb348567%28v=vs.110%29.aspx#"><strong>SequenceEqual</strong></a> - Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.</li>
</ul>
<h4 class="tinyHeader" id="converting-data-types"><a href="http://msdn.microsoft.com/en-us/library/bb546162.aspx">Converting Data Types</a></h4>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb341406%28v=vs.110%29.aspx#"><strong>Cast</strong></a> - Casts the elements of an IEnumerable to the specified type.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb335435%28v=vs.110%29.aspx#"><strong>AsEnumerable</strong></a> - Returns the input typed as IEnumerable(Of T).</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx"><strong>AsQueryable</strong></a> - Converts a (generic) IEnumerable to a (generic) IQueryable.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb298736%28v=vs.110%29.aspx#"><strong>ToArray</strong></a> - Creates an array from a IEnumerable(Of T).</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb549277%28v=vs.110%29.aspx#"><strong>ToDictionary</strong></a> - Creates a Dictionary(Of TKey, TValue) from an IEnumerable(Of T)according to a specified key selector function.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb342261%28v=vs.110%29.aspx#"><strong>ToList</strong></a> - Creates a List(Of T) from an IEnumerable(Of T).</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb549073%28v=vs.110%29.aspx#"><strong>ToLookup</strong></a> - Creates a Lookup(Of TKey, TElement) from an IEnumerable(Of T)according to a specified key selector function.</li>
</ul>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com0tag:blogger.com,1999:blog-8252867149515968674.post-74002509780533586602013-12-08T20:44:00.002-08:002014-01-01T15:01:06.594-08:00Explanation of using FOR XML PATH('') to Concatenate Rows in SQL<p>I was recently shown a cool way to concatenate an unlimited number of rows into a single cell in SQL, but was left wondering how it worked under the hood. The trick involves using <code>FOR XML PATH('')</code> to build multiple values and then cast them back into a single string. Starting from a simple query, and getting increasingly more complex, let’s look at how this actually works.</p>
<p>I’ll do everything on <a href="http://sqlfiddle.com/">SQLFiddle</a>. You can follow along by starting your own fiddle or clicking the <em>results</em> link for each step. Let’s start off by creating a basic table called <code>Person</code> with two columns and three rows of data:</p><div class="se-section-delimiter"></div>
<h3 id="create-a-table">Create A Table</h3>
<p>Start by <a href="http://sqlfiddle.com/#!6/ce75c">making a table</a>:</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">CREATE</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">TABLE</span></span></span></span></span><span class="operator"><span class="operator"> Person
(
FirstName </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">varchar</span></span></span></span></span><span class="operator"><span class="operator">(</span></span><span class="number"><span class="operator"><span class="number"><span class="operator"><span class="number">20</span></span></span></span></span><span class="operator"><span class="operator">),
LastName </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">varchar</span></span></span></span></span><span class="operator"><span class="operator">(</span></span><span class="number"><span class="operator"><span class="number"><span class="operator"><span class="number">20</span></span></span></span></span><span class="operator"><span class="operator">)
);</span></span></span>
<span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">INSERT</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">INTO</span></span></span></span></span><span class="operator"><span class="operator"> Person
(FirstName, LastName)
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">VALUES</span></span></span></span></span><span class="operator"><span class="operator">
(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Kyle'</span></span></span></span></span><span class="operator"><span class="operator">, </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Mit'</span></span></span></span></span><span class="operator"><span class="operator">),
(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Bob'</span></span></span></span></span><span class="operator"><span class="operator">, </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Builder'</span></span></span></span></span><span class="operator"><span class="operator">),
(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Jimi'</span></span></span></span></span><span class="operator"><span class="operator">, </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Hendrix'</span></span></span></span></span><span class="operator"><span class="operator">);</span></span></span></code></pre>
<h3 id="plain-sql-query">Plain SQL Query</h3>
<p>This is just a run of the mill SQL query returning 3 rows of data:</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> FirstName, LastName
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FROM</span></span></span></span></span><span class="operator"><span class="operator"> Person </span></span></span></code></pre>
<p><a href="http://sqlfiddle.com/#!6/ce75c/6">Results</a>: </p>
<div class="preTable"></div>
<table>
<thead>
<tr>
<th>FirstName</th>
<th>LastName</th>
</tr>
</thead>
<tbody><tr>
<td>Kyle</td>
<td>Mit</td>
</tr>
<tr>
<td>Bob</td>
<td>Builder</td>
</tr>
<tr>
<td>Jimi</td>
<td>Hendrix</td>
</tr>
</tbody></table>
<h3 id="sql-query-as-xml-grouped-by-person">SQL Query as XML Grouped by ‘Person’</h3>
<p>Here, the output of the Query is formatted into XML with each row represented as a Root node named ‘Person’ and each column as a child element:</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> FirstName, LastName
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FROM</span></span></span></span></span><span class="operator"><span class="operator"> Person
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FOR</span></span></span></span></span><span class="operator"><span class="operator"> XML PATH(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Person'</span></span></span></span></span><span class="operator"><span class="operator">)</span></span></span></code></pre>
<p><a href="http://sqlfiddle.com/#!6/ce75c/7">Results</a>:</p>
<pre class="prettyprint"><code class="xml"><span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">FirstName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Kyle<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">FirstName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">LastName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Mit<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">LastName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">FirstName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Bob<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">FirstName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">LastName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Builder<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">LastName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">FirstName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Jimi<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">FirstName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">LastName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Hendrix<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">LastName</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span></code></pre>
<h3 id="xml-query-with-custom-named-child-nodes">XML Query With Custom Named Child Nodes</h3>
<p>The name of each child node under person is determined by the final column name <em>(this is important later)</em>. When columns are selected, those are used by default, however you can manually give any column a specific name. <br>
<strong>Note</strong>: The name of the child nodes has changed based on our <code>select</code> statement:</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> FirstName </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">AS</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">First</span></span></span></span></span><span class="operator"><span class="operator">, LastName </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">AS</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">Last</span></span></span></span></span><span class="operator"><span class="operator">
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FROM</span></span></span></span></span><span class="operator"><span class="operator"> Person
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FOR</span></span></span></span></span><span class="operator"><span class="operator"> XML PATH(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Person'</span></span></span></span></span><span class="operator"><span class="operator">)</span></span></span></code></pre>
<p><a href="http://sqlfiddle.com/#!6/ce75c/9">Results</a>:</p>
<pre class="prettyprint"><code class="xml"><span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">First</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Kyle<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">First</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Mit<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">First</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Bob<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">First</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Builder<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">First</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Jimi<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">First</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>Hendrix<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span></code></pre>
<h3 id="xml-query-with-comma-and-named-child-node">XML Query with comma and NAMED child node</h3>
<p>In SQL, when you perform any kind of aggregation or selection function to a column, it no longer applies a <em>default name</em>. We’d like to concatenate our fields with a comma, so we’ll add a comma to the <code>select</code>. As an <em>intermediary</em> step, we’ll explicitly specify a name just to show that the comma itself isn’t modifying the result set:</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">','</span></span></span></span></span><span class="operator"><span class="operator"> + LastName </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">AS</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">Last</span></span></span></span></span><span class="operator"><span class="operator">
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FROM</span></span></span></span></span><span class="operator"><span class="operator"> Person
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FOR</span></span></span></span></span><span class="operator"><span class="operator"> XML PATH(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Person'</span></span></span></span></span><span class="operator"><span class="operator">)</span></span></span></code></pre>
<p><a href="http://sqlfiddle.com/#!6/ce75c/10">Results</a>:</p>
<pre class="prettyprint"><code class="xml"><span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>,Mit<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>,Builder<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>,Hendrix<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Last</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span></code></pre>
<h3 id="xml-query-with-comma-and-unnamed-child-node">XML Query with comma and UNNAMED child node</h3>
<p>By removing the explicit namimg, SQL isn’t able to guess the column name. Consequently, the data is just stuffed into the Person Element. <br>
From the FOR <a href="http://technet.microsoft.com/en-us/library/bb510469.aspx">XML PATH documentation</a>:</p>
<blockquote>
<p>Any column without a name will be inlined. For example, computed columns or nested scalar queries that do not specify column alias will generate columns without any name.</p>
</blockquote>
<p><strong>NOTE</strong>: This is a very important step towards concatenation. We need to somehow get rid of the xml markup around our data and we’ve dropped an entire node. In the <a href="#NoRoot">next step</a> we’ll find out how to get rid of the second one.</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">','</span></span></span></span></span><span class="operator"><span class="operator"> + LastName
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FROM</span></span></span></span></span><span class="operator"><span class="operator"> Person
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FOR</span></span></span></span></span><span class="operator"><span class="operator"> XML PATH(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">'Person'</span></span></span></span></span><span class="operator"><span class="operator">)</span></span></span></code></pre>
<p><a href="http://sqlfiddle.com/#!6/ce75c/11">Results</a>:</p>
<pre class="prettyprint"><code class="xml"><span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>,Mit<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>,Builder<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>
<span class="tag"><span class="tag"><span class="tag"><</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span>,Hendrix<span class="tag"><span class="tag"><span class="tag"></</span></span><span class="title"><span class="tag"><span class="title"><span class="tag"><span class="title">Person</span></span></span></span></span><span class="tag"><span class="tag">></span></span></span></code></pre>
<h3 id="NoRoot">XML Query with no root node</h3>
<p>The command we’ve been using all along is <a href="http://technet.microsoft.com/en-us/library/ms178107.aspx"><code>FOR XML</code></a> which has four different modes:</p>
<blockquote>
<ol>
<li>RAW</li>
<li>AUTO</li>
<li>EXPLICIT</li>
<li>PATH</li>
</ol>
</blockquote>
<p>In this case, we’re using <a href="http://technet.microsoft.com/en-us/library/ms189885.aspx">PATH</a>, which will wrap the data elements in a parent element named for the table from which it came. Optionally, you can add a string parameter to Path to override the root element name. The last trick:</p>
<blockquote>
<p>If you specify a zero-length string, the wrapping element is not produced.</p>
</blockquote>
<p>By manually specifying the path as an empty string, all the data elements are shown right next to each other. <br>
Note the file name: this is still returning XML (just poorly formatted XML)</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">','</span></span></span></span></span><span class="operator"><span class="operator"> + LastName
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FROM</span></span></span></span></span><span class="operator"><span class="operator"> Person
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FOR</span></span></span></span></span><span class="operator"><span class="operator"> XML PATH(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">''</span></span></span></span></span><span class="operator"><span class="operator">)</span></span></span></code></pre>
<p><a href="http://sqlfiddle.com/#!6/ce75c/12">Results</a>: </p>
<div class="preTable"></div>
<table>
<thead>
<tr>
<th>XML_F52E2B61-18A1-11D1-B105-00805F49916B</th>
</tr>
</thead>
<tbody><tr>
<td>,Mit,Builder,Hendrix</td>
</tr>
</tbody></table>
<h3 id="get-result-of-the-xml-sub-query">Get result of the XML sub query</h3>
<p>By selecting the result of the entire query, we transform the XML into a value:</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> (
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">','</span></span></span></span></span><span class="operator"><span class="operator"> + LastName
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FROM</span></span></span></span></span><span class="operator"><span class="operator"> Person
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FOR</span></span></span></span></span><span class="operator"><span class="operator"> XML PATH(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">''</span></span></span></span></span><span class="operator"><span class="operator">)
)</span></span></span></code></pre>
<p><a href="http://sqlfiddle.com/#!6/ce75c/13">Results</a>: </p>
<div class="preTable"></div>
<table>
<thead>
<tr>
<th>COLUMN_0</th>
</tr>
</thead>
<tbody><tr>
<td>,Mit,Builder,Hendrix</td>
</tr>
</tbody></table>
<h3 id="remove-the-first-comma">Remove the First Comma</h3>
<p>To remove the leading comma, we’ll use <a href="http://technet.microsoft.com/en-us/library/ms188043.aspx"><code>STUFF(character_expression, start, length, replaceWith_expression)</code></a>. The following query will start at position <strong>1</strong> and replace the <strong>1st</strong> character with <strong><code>''</code></strong>:</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> STUFF((
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">','</span></span></span></span></span><span class="operator"><span class="operator"> + LastName
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FROM</span></span></span></span></span><span class="operator"><span class="operator"> Person
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FOR</span></span></span></span></span><span class="operator"><span class="operator"> XML PATH(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">''</span></span></span></span></span><span class="operator"><span class="operator">)
), </span></span><span class="number"><span class="operator"><span class="number"><span class="operator"><span class="number">1</span></span></span></span></span><span class="operator"><span class="operator">, </span></span><span class="number"><span class="operator"><span class="number"><span class="operator"><span class="number">1</span></span></span></span></span><span class="operator"><span class="operator">, </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">''</span></span></span></span></span><span class="operator"><span class="operator">)</span></span></span></code></pre>
<p><a href="http://sqlfiddle.com/#!6/ce75c/14">Results</a>: </p>
<div class="preTable"></div>
<table>
<thead>
<tr>
<th>COLUMN_0</th>
</tr>
</thead>
<tbody><tr>
<td>Mit,Builder,Hendrix</td>
</tr>
</tbody></table>
<h3 id="cast-into-varchar-for-type-safety">Cast into VARCHAR for type safety</h3>
<p>Finally, we’ll take the whole query and make sure it’s of type <code>VARCHAR</code>. Also, for good measure, we’ll give the returned column a name:</p>
<pre class="prettyprint"><code class="language-sql"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">CAST</span></span></span></span></span><span class="operator"><span class="operator">(STUFF((
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">SELECT</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">','</span></span></span></span></span><span class="operator"><span class="operator"> + LastName
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FROM</span></span></span></span></span><span class="operator"><span class="operator"> Person
</span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">FOR</span></span></span></span></span><span class="operator"><span class="operator"> XML PATH(</span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">''</span></span></span></span></span><span class="operator"><span class="operator">)
), </span></span><span class="number"><span class="operator"><span class="number"><span class="operator"><span class="number">1</span></span></span></span></span><span class="operator"><span class="operator">, </span></span><span class="number"><span class="operator"><span class="number"><span class="operator"><span class="number">1</span></span></span></span></span><span class="operator"><span class="operator">, </span></span><span class="string"><span class="operator"><span class="string"><span class="operator"><span class="string">''</span></span></span></span></span><span class="operator"><span class="operator">) </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">AS</span></span></span></span></span><span class="operator"><span class="operator"> </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">VARCHAR</span></span></span></span></span><span class="operator"><span class="operator">(</span></span><span class="aggregate"><span class="operator"><span class="aggregate"><span class="operator"><span class="aggregate">MAX</span></span></span></span></span><span class="operator"><span class="operator">)) </span></span><span class="keyword"><span class="operator"><span class="keyword"><span class="operator"><span class="keyword">AS</span></span></span></span></span><span class="operator"><span class="operator"> LastNames</span></span></span></code></pre>
<p><a href="http://sqlfiddle.com/#!6/ce75c/15">Results</a>: </p>
<div class="preTable"></div>
<table>
<thead>
<tr>
<th>LastNames</th>
</tr>
</thead>
<tbody><tr>
<td>Mit,Builder,Hendrix</td>
</tr>
</tbody></table>
<p>And that’s how to go from constructing XML to a relatively simple concatenation. There are other ways to do this and other customizations you can add to this query, but this should help you understand a little bit more of the fundamentals underneath it all.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com6tag:blogger.com,1999:blog-8252867149515968674.post-18134159490872974392013-11-16T12:27:00.002-08:002014-01-01T15:10:08.008-08:00Windows Store MVVM Template<p>A bare bones starter template for Windows Store 8.1 Projects that Utilizes the Model View View Model Pattern </p>
<p>You can download the template from the <a href="http://visualstudiogallery.msdn.microsoft.com/9ef94ec6-3378-45b9-908c-cff703d2b901">Visual Studio Extension Gallery</a> <br>
You can fork or contribute to the template on <a href="https://github.com/KyleMit/WindowsStoreMvvmTemplate">github.com/KyleMit/WindowsStoreMvvmTemplate</a> </p>
<h2 id="directory">Directory:</h2>
<p>Here’s a breakdown of the directory structure:</p>
<p><img src="http://i.imgur.com/IEz8jLj.png" alt="directory" title=""></p>
<ul>
<li><strong>Assets</strong>: contains images for the Package.appxmanifest</li>
<li><strong>Model</strong>: contains core business logic for the application</li>
<li><strong>Resources</strong>: contains common resources for the whole application <br>
<ul><li><strong>AppResources.xaml</strong>: contains xaml resources that is added to App.Resources</li></ul></li>
<li><strong>Utilities</strong>: contains reusable helper classes that can be used in this, and other, projects. <br>
<ul><li><strong>BindableBase.vb</strong>: implements <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx"><code>INotifyPropertyChanged</code></a> which can be inherited by classes in you ViewModel</li>
<li><strong>NavigationHelper.vb</strong>: adds Navigation support and Process Lifetime Management</li>
<li><strong>RelayCommand.vb</strong>: implements <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand%28v=vs.110%29.aspx"><code>ICommand</code></a> to help invoke delegates through binding.</li>
<li><strong>SuspensionManager.vb</strong>: captures global session state to simplify process lifetime management</li></ul></li>
<li><strong>View</strong>: contains views for the User Interface layer of the application <br>
<ul><li><strong>MainView.xaml</strong>: has the initial window that the application launches</li></ul></li>
<li><strong>ViewModel</strong>: contains classes which help communicate between the <em>View</em> and the <em>Model</em> <br>
<ul><li><strong>MainViewModel.vb</strong>: contains the business logic for <code>MainView.xaml</code></li></ul></li>
<li><strong>App.xaml</strong>: contains resources scoped to the entire application <br>
<ul><li><strong>App.xaml.vb</strong>: contains startup code for the application</li></ul></li>
</ul>
<h2 id="samples">Samples:</h2>
<h3 id="binding">Binding</h3>
<h4 id="mainviewxaml">MainView.xaml</h4>
<pre class="prettyprint"><code class="xml"><span class="tag"><<span class="title">TextBox</span> <span class="attribute">Text</span>=<span class="value">"{Binding PersonName}"</span> /></span></code></pre>
<h4 id="mainviewmodelvb">MainViewModel.vb</h4>
<pre class="prettyprint"><code class="vbnet"><span class="keyword">Private</span> _personName <span class="keyword">As</span> <span class="built_in">String</span>
<span class="keyword">Public</span> <span class="keyword">Property</span> PersonName <span class="keyword">As</span> <span class="built_in">String</span>
<span class="keyword">Get</span>
<span class="keyword">Return</span> _personName
<span class="keyword">End</span> <span class="keyword">Get</span>
<span class="keyword">Set</span>(<span class="keyword">ByVal</span> value <span class="keyword">As</span> <span class="built_in">String</span>)
SetProperty(_personName, value)
<span class="keyword">End</span> <span class="keyword">Set</span>
<span class="keyword">End</span> <span class="keyword">Property</span></code></pre>
<h4 id="mainviewmodelcs">MainViewModel.cs</h4>
<pre class="prettyprint"><code class="cs"><span class="keyword">private</span> <span class="keyword">string</span> _propertyName;
<span class="keyword">public</span> <span class="keyword">string</span> PropertyName {
<span class="keyword">get</span> { <span class="keyword">return</span> _propertyName; }
<span class="keyword">set</span> { SetProperty(<span class="keyword">ref</span> _propertyName, <span class="keyword">value</span>); }
}</code></pre>
<h3 id="commands">Commands</h3>
<h4 id="mainviewxaml-1">MainView.xaml</h4>
<pre class="prettyprint"><code class="xml"><span class="tag"><<span class="title">button</span> <span class="attribute">content</span>=<span class="value">"Say Hello"</span> <span class="attribute">command</span>=<span class="value">"{Binding SayHelloCommand}"</span> /></span></code></pre>
<h4 id="mainviewmodelvb-1">MainViewModel.vb</h4>
<pre class="prettyprint"><code class="vbnet"><span class="keyword">Public</span> <span class="keyword">ReadOnly</span> <span class="keyword">Property</span> EnterNameCommand <span class="keyword">As</span> RelayCommand
<span class="keyword">Get</span>
<span class="keyword">If</span> _enterNameCommand <span class="keyword">Is</span> <span class="literal">Nothing</span> <span class="keyword">Then</span>
_enterNameCommand = <span class="keyword">New</span> RelayCommand(Async <span class="keyword">Sub</span>() Await EnterName())
<span class="keyword">End</span> <span class="keyword">If</span>
<span class="keyword">Return</span> _enterNameCommand
<span class="keyword">End</span> <span class="keyword">Get</span>
<span class="keyword">End</span> <span class="keyword">Property</span>
<span class="keyword">Private</span> <span class="keyword">Function</span> EnterName() <span class="keyword">As</span> Task
<span class="comment">'do awaitable stuff here</span>
<span class="keyword">End</span> <span class="keyword">Function</span></code></pre>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com1tag:blogger.com,1999:blog-8252867149515968674.post-36521600747875222992013-09-20T19:31:00.000-07:002014-05-23T14:56:11.572-07:00Windows 8 App Development Presentation<h4 id="heres-a-list-of-all-the-resources-ive-put-together-for-my-talk-on-windows-8-application-development-that-i-gave-at">Here’s a list of all the resources I’ve put together for my talk on Windows 8 Application Development that I gave at:</h4>
<ul>
<li>The <a href="http://vtcodecamp.org/2013/sessions#introduction-to-windows-8-apps-for-windows-forms-developers">Vermont Code Camp</a> on 9/21/13</li>
<li>The <a href="http://www.bostoncodecamp.com/CC20/Sessions/Details/4166">Boston Code Camp</a> 10/19/13</li>
<li>The <a href="http://www.meetup.com/VTCode/events/146213762/">VT.NET User Group</a> on 11/18/13</li>
</ul>
<p>You can view the <a href="https://speakerdeck.com/kylemit/introduction-to-windows-8-apps-for-windows-forms-developers">slides</a> from my presentation or <a href="http://speakerrate.com/talks/26141-introduction-to-windows-8-apps-for-windows-forms-developers">rate</a> the talk if you’d like to provide some feedback </p>
<p>The <strong>BindableBase</strong> class helps implement the MVVM pattern by implementing INotifyPropertyChanged on a base class which should be inherited by classes in your ViewModel </p>
<ul>
<li><a href="http://codepaste.net/dfijb9">BindableBase <span class="vbColor">(VB)</span></a></li>
<li><a href="http://codepaste.net/by8jbe">BindableBase <span class="csColor">(C#)</span></a></li>
</ul>
<p>The <strong>RelayCommand</strong> class helps implement the MVVM pattern by exposing methods on your ViewModel as Properties to which your View can Bind </p>
<ul>
<li><a href="http://codepaste.net/xazv4j">RelayCommand <span class="vbColor">(VB)</span></a></li>
<li><a href="http://codepaste.net/f48jvb">RelayCommand <span class="csColor">(C#)</span></a></li>
</ul>
<p>The <strong>UserSettingsBase</strong> class provides base functionality that allows you to create persistent user settings with regular Property Syntax.</p>
<ul>
<li><a href="http://codepaste.net/4bmahn">UserSettingsBase <span class="vbColor">(VB)</span></a></li>
<li><a href="http://codepaste.net/1pzhgt">UserSettingsBase <span class="csColor">(CS)</span></a></li>
</ul>
<p>The <strong>SettingsHelper</strong> class takes care of the plumbing to link the Settings Charm Panel with your own User Controls. </p>
<p><strong>NOTE</strong>: As of Windows 8.1, winRT released a native <a href="http://msdn.microsoft.com/library/windows/apps/dn252814">SettingsFlyout</a> so the following code is primarily for historical purposes.</p>
<ul>
<li><a href="http://codepaste.net/tvhkj8">Settings Helper <span class="vbColor">(VB)</span></a></li>
<li><a href="http://codepaste.net/gq1rr3">Settings Helper <span class="csColor">(CS)</span></a></li>
</ul>
<p>You can download the MVVM template in the <a href="http://visualstudiogallery.msdn.microsoft.com/9ef94ec6-3378-45b9-908c-cff703d2b901">Visual Studio Extensions Gallery</a> <br>
Fork or contribute to it on <a href="https://github.com/KyleMit/WindowsStoreMvvmTemplate/">GitHub</a> <br>
Or read about it in my <a href="http://codingeverything.blogspot.com/2013/11/windows-store-mvvm-template.html">Blogpost</a>. </p>
<p>For the Full Word Guesser Application in the Windows Store :</p>
<ul>
<li><a href="https://github.com/KyleMit/WordGuess">Source Code On GitHub</a></li>
<li><a href="http://apps.microsoft.com/windows/en-us/app/b85001c1-0782-4d4a-af96-caf23d56806e">Windows Store Link</a></li>
</ul>
<p>Here’s a <a href="https://skydrive.live.com/?cid=9935281cd314f220&id=9935281CD314F220%2132843&authkey=!AMX4OQAZXQw_odU">SkyDrive Folder</a> with all the files you might need. In here, you’ll find the <strong>PowerPoint</strong>, Demo <strong>Source Code</strong>, Resharper <strong>Templates</strong>, and code from the <strong>Demo</strong>.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com1tag:blogger.com,1999:blog-8252867149515968674.post-8655213530344587502013-08-25T13:35:00.003-07:002014-01-01T15:09:15.065-08:00Generating Windows Store Logos at Every Resolution<h3 id="introduction">Introduction</h3>
<p>The first daunting challenge of getting an Windows 8 Application into the Windows Store can be resizing your app logo to each of several dozen different resolutions. If you start off like I did, you’ll make a whole bunch of copies of a single file, size them down or up and then give them names based on the final resolution. But this is a flawed approach, and we can do much, much better.</p>
<p>For starters, whenever a resizing tool scales an image up, it will cause <em>artifacts</em>, or scales down, <em>pixelation</em>: neither of which are a desirable look for a polished Windows App.</p>
<p>But more importantly, you have not left yourself many outs if you want to change your logo. Let’s say the color doesn’t work with your background and you need to go in and make one tiny change: you now have to go through all your resizing and do it over from scratch.</p>
<p>Instead, we’ll make a vector based image and use slices to automatically export every asset we need in every resolution we need. See MSDN for a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh846296.aspx">full list</a> of all the logos you’ll need in the Windows Store and a description of each.</p>
<ol>
<li>Your <strong>first step</strong> is to read Jerry Nixon’s great blogpost, <a href="http://blog.jerrynixon.com/2013/08/windows-8-apps-need-28-logo-varietals.html">Windows 8 apps need 28 logo varietals</a>, in which he outlines how to output all the logos you’ll need from a vectorized image.</li>
<li>Your <strong>second step</strong> is to download the FREE version of <a href="http://www.microsoft.com/en-us/download/details.aspx?id=36180">Microsoft Expression Design 4</a></li>
<li>Once you’ve followed the steps above and created your logos, you’re ready to add them to your Visual Studio project (which, it turns out, is slightly trickier than you might imagine).</li>
</ol>
<h3 id="adding-logos-to-visual-studio">Adding Logos to Visual Studio</h3>
<p>In order to compile, your Windows Store App requires logos. The only reason it works right out of the box is because they have provided 4 Starter logos and added them to your app manifest.</p>
<p>Here are the <strong>logos</strong>:</p>
<p><img src="http://i.imgur.com/Rs7LUXp.png" alt="Starting Logos" title=""></p>
<p>We can see them in the <strong>designer</strong> for the <code>Package.appxmanifest</code> file here:</p>
<p><img src="http://i.imgur.com/GQXyEmH.png" alt="Designer View" title=""></p>
<p>It’s important to understand, that the only thing the designer has actually done, is add this bit of code to the xml in the <strong><code>Package.appxmanifest</code></strong></p>
<p><img src="http://i.imgur.com/oOVl39K.png" alt="XML View" title=""></p>
<h3 id="potential-errors">Potential Errors</h3>
<p>You <strong>must</strong> have at least the default size of all logos in order to compile the project. If you delete the reference to a particular logo, you’ll get the following error:</p>
<blockquote>
<p>App manifest validation failed. Length of attribute ‘xxx/@Logo’ must be at least 1 character(s)</p>
</blockquote>
<p>The next point at which you’re likely to get an error is when adding the same image with <strong>different scales</strong>. If I add an 150px logo named Logox150.png and then add the 120 version, I’m likely to get this screen:</p>
<p><img src="http://i.imgur.com/KkkZrjz.png" alt="Designer Error" title=""> <br>
<img src="http://i.imgur.com/5CRZnQB.png" alt="Solution Explorer Assets" title=""></p>
<p>There are a couple things going on here that I learned from this <a href="http://stackoverflow.com/questions/13943552/windows-8-app-manifest-image-assets-error#comment19230171_13943552">comment</a> by <a href="http://codocent.com/">Jim O’Neil</a> on Stack Overflow.</p>
<p>When you add scaled assets, Visual Studio takes the name of your default image (provided in the AppManifest) and decorates it with <strong><code>.scale-xx</code></strong>. If that name already exists, then you’re fine. If it doesn’t, then it creates a copy of the asset you’ve provided to fit that naming convention. </p>
<p>Secondly, if <em>any</em> of your assets have a scaled modifier, then <em>all</em> your assets must have a scale modifier, even the default. So the original logo named <code>logox150.png</code> now must be named <code>logox150.scale-100.png</code> in order to be found by Visual Studio. This actually makes a little bit of sense. The manifest only allows you to set a single property for the Logo name and location. How, other than using this strict scaled notation, would it know where to look for each of the different assets you provided at each size.</p>
<p>This is an important restriction to keep in minding when creating our slices in Expression Designer. In order to actually make use of them, you should name each logo and decorate each with the appropriate scaling factor.</p>
<p>Here are the <strong>slices</strong> I’ve setup to export using <strong>Expression</strong>:</p>
<p><img src="http://i.imgur.com/gAofw1Q.png" alt="Slices" title=""></p>
<p>Once you’ve done that, you simply have to replace the contents of your Assets folder with the exported images and VS will automatically load them for you at each resolution:</p>
<p><img src="http://i.imgur.com/4ixB7Lo.png" alt="Fully Loaded Designer" title=""> <br>
<img src="http://i.imgur.com/jWXRwY3.png" alt="Fully Loaded Assets" title=""></p>
<p>You may not want the same image at every resolution, but as a starter kit, here are two designer files (one square and one wide) for each aspect ratio used in the Windows Store (<strong><code>1:1</code></strong> and <strong><code>31:15</code></strong>) with all the slices properly configured. Just sub out the content with whatever logo you’d like to use, and hit <kbd>Ctrl</kbd> + <kbd>E</kbd> to export all the assets you’ll need. </p>
<h3 id="problems-exporting-slice-resolutions">Problems Exporting Slice Resolutions</h3>
<p>The designer doesn’t know that you’re exporting slices to match pixel perfect resolutions, so will try to adjust images for you, occasionally resulting in a <code>150x150</code> slice exporting with a width or height of <strong><code>149</code></strong> or <strong><code>151</code></strong>. I found this <a href="http://blogs.msdn.com/b/avip/archive/2010/10/29/expression-design-problems-with-exporting-slices-wrong-sizes.aspx">post</a> explaining how to fix the problem.</p>
<ol>
<li>Go to the Options Pages (<kbd>Ctrl</kbd> + <kbd>K</kbd>) or <strong>Edit</strong> –> Options</li>
<li>In the ‘General’ tab, change ‘Rectangle Antialiasing’ from ‘Crisp’ to ‘Smooth’</li>
</ol>
<p><img src="http://i.imgur.com/rcuTntv.png" alt="Designer Options" title=""></p>
<h2 id="blank-designer-files">Blank Designer Files</h2>
<p>To to save youself the setup work, click to <strong><a href="http://sdrv.ms/16zCB83">download stock template designer files from skydrive</a></strong></p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com1tag:blogger.com,1999:blog-8252867149515968674.post-36309824691471225162013-07-24T07:27:00.000-07:002014-01-01T15:01:29.878-08:00Moving ASP.NET Designer Generated Code to Separate Designer.vb File<h3 id="introduction">Introduction</h3>
<p>If you’ve converted an an ASP.NET application from versions of Visual Studio 2003 and earlier to 2005 and beyond, you’ll notice the converted web pages behave slightly differently than those natively generated in new projects. The difference is that designer generated code used to have to live inline with developer code because all class properties needed to exist within the same file. </p>
<h3 id="solution">Solution</h3>
<p>Starting in .NET 2.0, the compiler can now use the <a href="http://msdn.microsoft.com/en-us/library/yfzd5350.aspx">partial</a> keyword which allows you to split up the declaration of a class over <em>n</em> number of files. The paved the way for Visual Studio to break apart the ugly code it generates from the (hopefully) beautiful code you’ve written. To make use of this feature on old asp.net web applications that have been converted to a compatible version of visual studio, simply do the following two steps.</p>
<h3 id="easy-steps">5 Easy Steps</h3>
<ol>
<li><p>Take the entire region label <code>Web Form Designer Generated Code</code> and Delete it from your code behind file:</p>
<p><a href="http://i.imgur.com/cPEpPHW.png"><img src="http://i.imgur.com/cPEpPHW.png" alt="" title=""></a></p></li>
<li><p>Our Solution Explorer currently looks likes this:</p>
<p><a href="http://i.imgur.com/XFdsLVV.png"><img src="http://i.imgur.com/XFdsLVV.png" alt="" title=""></a></p></li>
<li><p>Right Click on the <code>.aspx</code> or <code>.ascx</code> page and Select <code>Convert to Web Application</code>”</p>
<p><a href="http://i.imgur.com/f3W7JtU.png"><img src="http://i.imgur.com/f3W7JtU.png" alt="" title=""></a></p></li>
<li><p>It will add a designer file with all the code we just deleted:</p>
<p><a href="http://i.imgur.com/Vug3Lbk.png"><img src="http://i.imgur.com/Vug3Lbk.png" alt="" title=""></a></p></li>
<li><p>Of course, if you turn of “Show All Files”, you’ll still only see the page in your solution explorer</p>
<p><a href="http://i.imgur.com/16fElfK.png"><img src="http://i.imgur.com/16fElfK.png" alt="" title=""></a></p></li>
</ol>
<p><strong>And that’s it!</strong> Now enjoy the faster design time analysis and cleaner code that comes from separating your generated code and your written code.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com7tag:blogger.com,1999:blog-8252867149515968674.post-35847715631272151052013-05-31T07:07:00.001-07:002014-01-01T15:02:38.013-08:00Calculating Age From DOB<p>I couldn’t believe there was no native way in .NET to do something alarmingly simple like calculate someone’s age from their date of birth. Of all the amazing functions and properties on <code>DateTime</code> objects, this seems surprisingly non-existent. </p>
<p>For such an easy task, it’s also surprisingly complex; so much so that Jeff Atwood, creator of StackOverflow, even asked that question himself <a href="http://stackoverflow.com/q/9/1366033">here</a>.</p>
<p>For all my (and the internet’s) failed attempts, read on. If you’re just looking for something to copy and paste in that will work, grab the following code</p>
<pre class="prettyprint"><code class="language-vbnet"><span class="keyword"><span class="keyword">Public</span></span> <span class="keyword"><span class="keyword">Function</span></span> GetCurrentAge(<span class="keyword"><span class="keyword">ByVal</span></span> dob <span class="keyword"><span class="keyword">As</span></span> <span class="built_in"><span class="built_in">Date</span></span>) <span class="keyword"><span class="keyword">As</span></span> <span class="built_in"><span class="built_in">Integer</span></span>
<span class="keyword"><span class="keyword">Dim</span></span> age <span class="keyword"><span class="keyword">As</span></span> <span class="built_in"><span class="built_in">Integer</span></span>
age = Today.Year - dob.Year
<span class="keyword"><span class="keyword">If</span></span> (dob > Today.AddYears(-age)) <span class="keyword"><span class="keyword">Then</span></span> age -= <span class="number"><span class="number">1</span></span>
<span class="keyword"><span class="keyword">Return</span></span> age
<span class="keyword"><span class="keyword">End</span></span> <span class="keyword"><span class="keyword">Function</span></span></code></pre>
<p>All of these methods, except the bottom one fail for various reasons:</p>
<pre class="prettyprint"><code class="vbnet"><span class="keyword"><span class="keyword">Dim</span></span> dob <span class="keyword"><span class="keyword">As</span></span> <span class="built_in"><span class="built_in">Date</span></span> = <span class="preprocessor"><span class="preprocessor">#5/14/1994#</span></span>
<span class="keyword"><span class="keyword">Dim</span></span> today <span class="keyword"><span class="keyword">As</span></span> <span class="built_in"><span class="built_in">Date</span></span> = <span class="preprocessor"><span class="preprocessor">#5/13/2013#</span></span>
<span class="keyword"><span class="keyword">Dim</span></span> age <span class="keyword"><span class="keyword">As</span></span> <span class="built_in"><span class="built_in">Integer</span></span>
age = DateDiff(DateInterval.Year, dob, today)
Console.WriteLine(<span class="string"><span class="string">"DateDiff Year Age: {0}"</span></span>, age)
<span class="comment"><span class="comment">'19</span></span>
age = today.Subtract(dob).TotalDays / <span class="number"><span class="number">365.25</span></span>
Console.WriteLine(<span class="string"><span class="string">"Subtraction Age: {0}"</span></span>, age)
<span class="comment"><span class="comment">'19</span></span>
age = Math.Floor(DateDiff(DateInterval.Month, dob, today) / <span class="number"><span class="number">12</span></span>)
Console.WriteLine(<span class="string"><span class="string">"DateDiff Month Age: {0}"</span></span>, age)
<span class="comment"><span class="comment">'19</span></span>
age = today.Year - dob.Year
<span class="keyword"><span class="keyword">If</span></span> (dob > today.AddYears(-age)) <span class="keyword"><span class="keyword">Then</span></span> age -= <span class="number"><span class="number">1</span></span>
Console.WriteLine(<span class="string"><span class="string">"Year Part and Compare Age: {0}"</span></span>, age)
<span class="comment"><span class="comment">'18</span></span>
Console.ReadKey()</code></pre>
<p>Hope that helps!</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com1tag:blogger.com,1999:blog-8252867149515968674.post-85349710760271478542013-05-07T14:16:00.000-07:002014-01-01T15:03:29.719-08:00Extending XmlReader Class: IsEndElement()<h3 id="introduction">Introduction</h3>
<p>You may find, as I did, the lack of symmetry in the <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx"><code>XmlReader</code></a> Class quite odd. If we look at the NodeType property, which exposes the <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlnodetype.aspx"><code>XmlNodeType</code></a> enumeration, we find an EndElement, but no StartElement (The Element member actually only identifies opening xml tag elements, but it’s not this shortage on which I’d like to elaborate). Only when we look at the methods available to the class do we see an <code>IsStartElement()</code> method for evaluation, but without a corollary method named something like: <code>IsEndElement()</code>.</p>
<p>There are dozens of ways of getting around this deficiency, but I’m all for visually appealing code that leaves other coders with a easy and quick understanding of what you are attempting to do. Checking if the reader is on an opening tag with <code>IsStartElement</code> and then finding closing tags by evaluation the reader’s <code>NodeType</code> property might work correctly, but just <em>looks</em> wrong to me.</p>
<h3 id="extension-methods">Extension Methods</h3>
<p>What I’d like to do is create a method that looks and feels like the <code>IsStartElement()</code> function, but instead evaluates if the reader is currently on an EndElement node type. This is where <a href="http://msdn.microsoft.com/en-us/library/bb384936.aspx"><strong>extension methods</strong></a> come into play. Extension methods allow you to extend the functionality of a built in class with custom methods that act as if they were native to the class. I think they are best suited for when you are of the sincere belief that method deficit is a slight oversight of the framework designers, and if given the opportunity, they would happily accept your additional coding to improve the underlying code. Since all instances of this type or any derived type will have automatic access to any extension methods written on top of a particular class, you want to be careful with their implementation .</p>
<p>The first step is to open your project, right click on it, click add, then select module:</p>
<p><img src="http://i.imgur.com/WbP7l.png" alt="Add Module" title=""></p>
<p>Then give your module a name. I like to create a single Module for all extensions with a name like <strong>ExtensionMethods</strong>.</p>
<h3 id="isendelement-code">IsEndElement Code</h3>
<p>First, you’ll need an <code>imports</code><em>(vb)</em> or <code>using</code><em>(c#)</em> statement with the extensions Namespace:</p>
<pre class="prettyprint"><code class="language-vbnet"><span class="keyword"><span class="keyword">Imports</span></span> System.Runtime.CompilerServices</code></pre>
<p>Then, add the following code to your module:</p>
<pre class="prettyprint"><code class="language-vbnet"><Extension()>
<span class="keyword"><span class="keyword">Public</span></span> <span class="keyword"><span class="keyword">Function</span></span> IsEndElement(<span class="keyword"><span class="keyword">ByVal</span></span> xmlReader <span class="keyword"><span class="keyword">As</span></span> XmlReader,
<span class="keyword"><span class="keyword">Optional</span></span> <span class="keyword"><span class="keyword">ByVal</span></span> name <span class="keyword"><span class="keyword">As</span></span> <span class="built_in"><span class="built_in">String</span></span> = <span class="string"><span class="string">""</span></span>) <span class="keyword"><span class="keyword">As</span></span> <span class="built_in"><span class="built_in">Boolean</span></span>
<span class="keyword"><span class="keyword">If</span></span> xmlReader.NodeType = XmlNodeType.EndElement <span class="keyword"><span class="keyword">Then</span></span>
<span class="keyword"><span class="keyword">If</span></span> name = <span class="string"><span class="string">""</span></span> <span class="keyword"><span class="keyword">Then</span></span>
<span class="comment"><span class="comment">'node is end element, name value not set </span></span>
<span class="keyword"><span class="keyword">Return</span></span> <span class="literal"><span class="literal">True</span></span>
<span class="keyword"><span class="keyword">Else</span></span>
<span class="keyword"><span class="keyword">If</span></span> xmlReader.Name = name <span class="keyword"><span class="keyword">Then</span></span>
<span class="comment"><span class="comment">'node is end element, AND named the same as parameter </span></span>
<span class="keyword"><span class="keyword">Return</span></span> <span class="literal"><span class="literal">True</span></span>
<span class="keyword"><span class="keyword">Else</span></span>
<span class="comment"><span class="comment">'node is end element, but name is diff than parameter </span></span>
<span class="keyword"><span class="keyword">Return</span></span> <span class="literal"><span class="literal">False</span></span>
<span class="keyword"><span class="keyword">End</span></span> <span class="keyword"><span class="keyword">If</span></span>
<span class="keyword"><span class="keyword">End</span></span> <span class="keyword"><span class="keyword">If</span></span>
<span class="keyword"><span class="keyword">Else</span></span>
<span class="comment"><span class="comment">'node is not an end element </span></span>
<span class="keyword"><span class="keyword">Return</span></span> <span class="literal"><span class="literal">False</span></span>
<span class="keyword"><span class="keyword">End</span></span> <span class="keyword"><span class="keyword">If</span></span>
<span class="keyword"><span class="keyword">End</span></span> <span class="keyword"><span class="keyword">Function</span></span> </code></pre>
<p>And voilà! Now you can call your code on native objects with full IntelliSense:</p>
<p><img src="http://i.imgur.com/H0nB9lb.png" alt="IntelliSense" title=""></p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com1tag:blogger.com,1999:blog-8252867149515968674.post-70670857160052289282013-05-07T13:52:00.000-07:002014-01-01T15:01:56.183-08:00Automatically Increment Minimum Required Version in a ClickOnce Application<h3 id="introduction">Introduction</h3>
<p>ClickOnce deployments make publishing .NET applications simple and easy. One nice feature is forcing the user to update to the latest available version. This can be very important on sensitive applications that require any potential fixes or patches to be immediately sent to the users.</p>
<p>One way to do this is to set the minimum required version on the publish page to the current version. ClickOnce then knows to automatically download the latest version whenever a user goes to run the application. The problem with this can be that you have to remember to increment the value every time you do a publish. This post will make life easier by automating the increase of the minimum required version every time you preform a publish. </p>
<p>We’ll do so by adding an <a href="http://msdn.microsoft.com/en-us/library/wea2sca5%28v=vs.90%29.aspx"><strong>MSBuild</strong></a> event to the project. This post assumes you don’t have any experience with MS build and will introduce you to the relevant concepts; if you’re already familiar with that, feel free to jump ahead to <a href="#StepNumberSix">Step 6</a> and copy and paste the code into your .proj file.</p>
<h3 id="introduction-to-project-editor">Introduction to Project Editor</h3>
<p>1) First, in solution explorer, right click on your project and select unload project</p>
<ul>
<li><img src="http://i.imgur.com/cIbF2UN.png" alt="" title=""></li>
</ul>
<p>2) Once the project has become unavailable, right click again and select edit <code>[project_name].csproj</code> </p>
<ul>
<li><img src="http://i.imgur.com/AeFvWN8.png" alt="" title=""> </li>
<li><em>Note</em>: This unlocks full set of IntelliSense of all the MSBuild features</li>
</ul>
<h3 id="introduction-to-ms-build">Introduction to MS Build</h3>
<p>3) MS Build notation uses properties with key/value pairs to extract information. We’ll use the notation <code>$(key)</code> in the implementation section to reference the .proj elements in Step 4.</p>
<ul>
<li><em>ex</em>. Using the property name as an alias, you can use <code>$(OutputPath)</code> to obtain the value for the element <code><OutputPath>.\bin</OutputPath></code>. So in this case <code>$(OutputPath) = .\bin</code></li>
</ul>
<p>4) We’ll use the following properties generated for a ClickOnce deployment</p>
<pre class="prettyprint"><code class="xml"><span class="tag"><span class="tag"><</span><span class="title"><span class="tag"><span class="title">MinimumRequiredVersion</span></span></span><span class="tag">></span></span>1.0.0.6<span class="tag"><span class="tag"></</span><span class="title"><span class="tag"><span class="title">MinimumRequiredVersion</span></span></span><span class="tag">></span></span>
<span class="tag"><span class="tag"><</span><span class="title"><span class="tag"><span class="title">ApplicationRevision</span></span></span><span class="tag">></span></span>7<span class="tag"><span class="tag"></</span><span class="title"><span class="tag"><span class="title">ApplicationRevision</span></span></span><span class="tag">></span></span>
<span class="tag"><span class="tag"><</span><span class="title"><span class="tag"><span class="title">ApplicationVersion</span></span></span><span class="tag">></span></span>1.0.0.%2a<span class="tag"><span class="tag"></</span><span class="title"><span class="tag"><span class="title">ApplicationVersion</span></span></span><span class="tag">></span></span> </code></pre>
<ul>
<li>The <code>ApplicationVersion</code> element specifies the build number. The last value of <code>%2a</code> is set by Visual Studio when the option to “<em>Automatically increment revision with each publish</em>” is checked on. In that case, it substitutes the <code>%2a</code> placeholder with whatever value is in the <code>ApplicationRevision</code> element</li>
<li>The <code>MinimumRequiredVersion</code> element is what we’d like to set with each publish so that it automatically increments as well.</li>
</ul>
<p>5) MSBuild Tasks can be specified in the .proj file and invoked during a build event.</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/ff595162.aspx"><code>FormatVersion</code></a> is a built-in task for .NET 4.0 and later that formats the ApplicationVersion and ApplicationRevision into a single version number</li>
</ul>
<h3 id="StepNumberSix">Implementation</h3>
<p>6) Copy and Paste the following code into the opened project file as a child element to the root <code><Project></code> element: </p>
<pre class="prettyprint"><code class="language- xml"><span class="tag"><span class="tag"><</span><span class="title"><span class="tag"><span class="title">Target</span></span></span><span class="tag"> </span><span class="attribute"><span class="tag"><span class="attribute">Name</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"AutoSetMinimumRequiredVersion"</span></span></span><span class="tag">
</span><span class="attribute"><span class="tag"><span class="attribute">BeforeTargets</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"GenerateDeploymentManifest"</span></span></span><span class="tag">></span></span>
<span class="tag"><span class="tag"><</span><span class="title"><span class="tag"><span class="title">FormatVersion</span></span></span><span class="tag"> </span><span class="attribute"><span class="tag"><span class="attribute">Version</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"$(ApplicationVersion)"</span></span></span><span class="tag">
</span><span class="attribute"><span class="tag"><span class="attribute">Revision</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"$(ApplicationRevision)"</span></span></span><span class="tag">></span></span>
<span class="tag"><span class="tag"><</span><span class="title"><span class="tag"><span class="title">Output</span></span></span><span class="tag"> </span><span class="attribute"><span class="tag"><span class="attribute">PropertyName</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"MinimumRequiredVersion"</span></span></span><span class="tag">
</span><span class="attribute"><span class="tag"><span class="attribute">TaskParameter</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"OutputVersion"</span></span></span><span class="tag"> /></span></span>
<span class="tag"><span class="tag"></</span><span class="title"><span class="tag"><span class="title">FormatVersion</span></span></span><span class="tag">></span></span>
<span class="tag"><span class="tag"><</span><span class="title"><span class="tag"><span class="title">FormatVersion</span></span></span><span class="tag"> </span><span class="attribute"><span class="tag"><span class="attribute">Version</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"$(ApplicationVersion)"</span></span></span><span class="tag">
</span><span class="attribute"><span class="tag"><span class="attribute">Revision</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"$(ApplicationRevision)"</span></span></span><span class="tag">></span></span>
<span class="tag"><span class="tag"><</span><span class="title"><span class="tag"><span class="title">Output</span></span></span><span class="tag"> </span><span class="attribute"><span class="tag"><span class="attribute">PropertyName</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"_DeploymentBuiltMinimumRequiredVersion"</span></span></span><span class="tag">
</span><span class="attribute"><span class="tag"><span class="attribute">TaskParameter</span></span></span><span class="tag">=</span><span class="value"><span class="tag"><span class="value">"OutputVersion"</span></span></span><span class="tag"> /></span></span>
<span class="tag"><span class="tag"></</span><span class="title"><span class="tag"><span class="title">FormatVersion</span></span></span><span class="tag">></span></span>
<span class="tag"><span class="tag"></</span><span class="title"><span class="tag"><span class="title">Target</span></span></span><span class="tag">></span></span></code></pre>
<ul>
<li>This code will take ApplicationVersion and ApplicationRevision as parameters in the Format Version task and will save the output by overwriting the MinimumRequiredVersion with the full publish version</li>
</ul>
<p>7) Save and reload your project. Every ClickOnce deployment will now automatically update without a prompt with the ability to skip.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com6tag:blogger.com,1999:blog-8252867149515968674.post-89003969062709775132013-05-07T12:46:00.000-07:002014-01-13T12:45:02.120-08:00Making Sure Outlook Calendar Reminders Actually Pop-up in Windows 7<h3 id="introduction">Introduction</h3>
<p>In Windows 7, MS Outlook seems to be horribly bad at popping up calendar reminders when outlook doesn’t have focus. This can cause a series of problems from minor annoyances to missed meetings. As discussed in this <a href="http://superuser.com/q/251963/180163">StackExchange</a> question, there are some relatively easy work arounds to this issue that I’ll elaborate on here. If you’ve never used Outlook’s VBA editor to extend it’s capabilities before, this article will take you through how to set up everything you’ll need to do, start to finish. If you’re already familiar with using VBA in Outlook, then you can just skip to <a href="#StepNumberFive">Step 5</a> and paste in the code and you’ll be all set.</p>
<h3 id="step-by-step">Step By Step</h3>
<ul>
<li><strong>1)</strong> Hit the Windows Key and type “certificate” <br>
<ul><li><img src="http://i.imgur.com/jEKNPo6.png" alt="" title=""></li></ul></li>
<li><strong>2)</strong> Select Digital Certificate for VBA Projects and type in a name for your certificate and hit OK <br>
<ul><li><img src="http://i.imgur.com/BblTAWZ.png" alt="" title=""></li></ul></li>
<li><strong>3)</strong> Open Outlook 2010 and Hit <kbd>Alt</kbd> + <kbd>F11</kbd> to start the VBA editor</li>
<li><strong>4)</strong> In the Project Pane on the left, Expand <code>Project1</code> > <code>Microsoft Outlook Objects</code> > and double click on <code>ThisOutlookSession</code> <br>
<ul><li><img src="http://i.imgur.com/mlxcL8B.png" alt="" title=""></li></ul></li>
<li><strong>5)</strong> Paste in the following code exactly:</li>
</ul>
<pre class="prettyprint"><code class="vbnet"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment">'ensures all reminder notices receive focus</span></span></span></span></span></span></span></span>
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Private</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Sub</span></span></span></span></span></span></span></span> Application_Reminder(<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> Item <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Object</span></span></span></span></span></span></span></span>)
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">If</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">TypeOf</span></span></span></span></span></span></span></span> Item <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Is</span></span></span></span></span></span></span></span> AppointmentItem <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Then</span></span></span></span></span></span></span></span>
Application.ActiveWindow.Activate
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">End</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">If</span></span></span></span></span></span></span></span>
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">End</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Sub</span></span></span></span></span></span></span></span></code></pre>
<h3 id="StepNumberFive">Edit: Added New Better Code Below:</h3>
<pre class="prettyprint"><code class="vbnet"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment">'Declare Functions From User32 Library </span></span></span></span></span></span></span></span>
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Private</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Declare</span></span></span></span></span></span></span></span> PtrSafe <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Function</span></span></span></span></span></span></span></span> FindWindowA <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Lib</span></span></span></span></span></span></span></span> <span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string">"user32"</span></span></span></span></span></span></span></span> _
(<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> lpClassName <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">String</span></span></span></span></span></span></span></span>, _
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> lpWindowName <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">String</span></span></span></span></span></span></span></span>) <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span>
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Private</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Declare</span></span></span></span></span></span></span></span> PtrSafe <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Function</span></span></span></span></span></span></span></span> SetWindowPos <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Lib</span></span></span></span></span></span></span></span> <span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string">"user32"</span></span></span></span></span></span></span></span> _
(<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> hwnd <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span>, _
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> hWndInsertAfter <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span>, _
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> X <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span>, _
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> Y <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span>, _
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> cx <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span>, _
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> cy <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span>, _
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> wFlags <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span>) <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span>
<span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment">'Declare Constants </span></span></span></span></span></span></span></span>
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Private</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Const</span></span></span></span></span></span></span></span> SWP_NOSIZE = &H1
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Private</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Const</span></span></span></span></span></span></span></span> SWP_NOMOVE = &H2
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Private</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Const</span></span></span></span></span></span></span></span> FLAGS <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Long</span></span></span></span></span></span></span></span> = SWP_NOMOVE <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Or</span></span></span></span></span></span></span></span> SWP_NOSIZE
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Private</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Const</span></span></span></span></span></span></span></span> HWND_TOPMOST = -<span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number">1</span></span></span></span></span></span></span></span>
<span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment">'Only show the message the first time </span></span></span></span></span></span></span></span>
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Private</span></span></span></span></span></span></span></span> messageAlreadyShown <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Boolean</span></span></span></span></span></span></span></span>
<span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment">'Finds Reminder Window and Brings to TopMost position </span></span></span></span></span></span></span></span>
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Private</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Sub</span></span></span></span></span></span></span></span> Application_Reminder(<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">ByVal</span></span></span></span></span></span></span></span> Item <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">As</span></span></span></span></span></span></span></span> <span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in"><span class="built_in">Object</span></span></span></span></span></span></span></span>)
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">On</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Error</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Resume</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Next</span></span></span></span></span></span></span></span>
<span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment">'show message box for first reminder </span></span></span></span></span></span></span></span>
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">If</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Not</span></span></span></span></span></span></span></span> messageAlreadyShown <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Then</span></span></span></span></span></span></span></span>
MsgBox <span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string">"First Reminder"</span></span></span></span></span></span></span></span>, vbSystemModal, <span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string">""</span></span></span></span></span></span></span></span>
messageAlreadyShown = <span class="literal"><span class="literal"><span class="literal"><span class="literal"><span class="literal"><span class="literal"><span class="literal"><span class="literal">True</span></span></span></span></span></span></span></span>
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">End</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">If</span></span></span></span></span></span></span></span>
<span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment">'find reminder window </span></span></span></span></span></span></span></span>
ReminderWindow = FindWindowA(vbNullString, <span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string"><span class="string">"1 Reminder"</span></span></span></span></span></span></span></span>)
<span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment"><span class="comment">'bring reminder window to front </span></span></span></span></span></span></span></span>
SetWindowPos ReminderWindow, HWND_TOPMOST, <span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number">0</span></span></span></span></span></span></span></span>, <span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number">0</span></span></span></span></span></span></span></span>, <span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number">0</span></span></span></span></span></span></span></span>, <span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number"><span class="number">0</span></span></span></span></span></span></span></span>, FLAGS
<span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">End</span></span></span></span></span></span></span></span> <span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword"><span class="keyword">Sub</span></span></span></span></span></span></span></span> </code></pre>
<ul>
<li><strong>6)</strong> Sign the Macro by going to Tools > Digital Signature and clicking Choose <br>
<ul><li><img src="http://i.imgur.com/RcGS2hs.png" alt="" title=""></li></ul></li>
<li><strong>7)</strong> Select the certificate you created earlier and hit OK <br>
<ul><li><img src="http://i.imgur.com/CDJzfrG.png" alt="" title=""></li></ul></li>
<li><strong>8)</strong> Select OK again, hit <kbd>Ctrl</kbd> + <kbd>S</kbd> to save and exit the VBA window</li>
<li><strong>9)</strong> To Enable Macros, Go to File > Options and select Trust Center from the left window <br>
<ul><li><img src="http://i.imgur.com/9SP6oqC.png" alt="" title=""></li></ul></li>
<li><strong>10)</strong> Run the Trust center by clicking the Trust Center Settings button on the right.</li>
<li><strong>11)</strong> From the Trust Center, select Macro Settings, and select “Notifications for digitally signed macros, all other macros disabled” and hit OK <br>
<ul><li><img src="http://i.imgur.com/r9pQ1Pi.png" alt="" title=""></li></ul></li>
<li><strong>12)</strong> Exit Outlook - It will ask you if you want to save the project, click Yes</li>
<li><strong>13)</strong> Start Outlook - It will give you a security notice. Select “Trust all documents from this publisher” (You can first confirm that you are the publisher by selecting “Show Signature Details”) <br>
<ul><li><img src="http://i.imgur.com/nD4t5CE.png" alt="" title=""></li></ul></li>
<li><strong>14)</strong> That’s it! You’re all set. You never have to touch any of that code again or miss another meeting (unintentionally)</li>
</ul>
<p><strong>UPDATE!</strong></p>
<p>I’ve update the code to use <a href="http://msdn.microsoft.com/en-us/library/office/aa219402%28v=office.11%29.aspx">ActiveWindow</a> instead of <a href="http://msdn.microsoft.com/en-us/library/office/aa219397%28v=office.11%29.aspx">ActiveExplorer</a>, which returns <strong>nothing</strong> “if no explorer is active.” Thanks to CW for the impetus to update my code.</p>Kyle Anthony Mitofskyhttp://www.blogger.com/profile/13800270579139050074noreply@blogger.com35