<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>XTab's Blog</title><link>http://blogs.vbcity.com/xtab/</link><description>Ged Mead's Blog at vbCity</description><managingEditor>Ged Mead</managingEditor><dc:language>et</dc:language><generator>.Text Version 0.95.2004.102</generator><item><dc:creator>Ged Mead</dc:creator><title>Object Initializers - A New Perspective</title><link>http://blogs.vbcity.com/xtab/archive/2008/08/28/9166.aspx</link><pubDate>Thu, 28 Aug 2008 07:09:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/08/28/9166.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9166.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/08/28/9166.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9166.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9166.aspx</trackback:ping><description>&lt;FONT face=verdana&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;There's something that's always bothered me a little bit about creating classes. We go to the trouble of creating backing Private fields and Public properties in order to implement data hiding - right?&amp;nbsp;&amp;nbsp; And when we want to include data validation, we usually use properties for that task, don't we? &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; But then what do we do? We go and create a parameterized constructor that lets client code push invalid data straight into a field. At least that's what I see in most of the class building examples I've&amp;nbsp;come across&amp;nbsp;- both in books as well as in real world code. It's quite rare to see values passed into properties in a parameterized constructor; fields seem to be the most popular recipients of initialization data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I'm surprised at how many times I've seen this done. Of course it's possible to include validation in the constructor too (but the examples I've seen that use fields rarely seem to do&amp;nbsp;this; leaving the door to bad data wide open) &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Luckily, we now have a very easy solution - Object Initializers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;In fact, you could claim that with the arrival of Object Initializers the parameterized constructor has just about become redundant. &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;Parameterized constructors were devised to make it easy to pass arguments in to a constructor and to avoid having to add several lines of code for property values when initializing a new object. You know the sort of thing:-&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #d1eecc"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; sr&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; SalesRecord&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sr.CustomerName =&lt;FONT color=#a31515&gt; "Joe Soap"&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sr.Total = 2399.65&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;etc&amp;nbsp;&amp;nbsp;etc&lt;/FONT&gt;&lt;BR&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;which was short-circuited with :-&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #d1eecc"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; sr&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; SalesRecord(&lt;FONT color=#a31515&gt;"Joe Soap"&lt;/FONT&gt;, 2399.65)&lt;BR&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;However, as I said, in most cases people code this so that it assigns the CustomerName and Total values directly to the fields.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Object Initializers step round this issue while still enabling client code to be fairly concise. How it works is that when you initialize an instance of a class object you are allowed to use the 'With' keyword as a way of assigning values to properties, all in one line of code. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Let's knock up a quick class to demo this.&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #d1eecc"&gt;&lt;FONT color=#0000ff&gt;Public&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Class&lt;/FONT&gt; Product&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt; _itemcode&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Public&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Property&lt;/FONT&gt; ItemCode()&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Get&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Return&lt;/FONT&gt; _itemcode&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Get&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Set&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; value&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_itemcode = value&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Set&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Property&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt; _cost&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Decimal&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Public&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Property&lt;/FONT&gt; Cost()&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Decimal&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Get&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Return&lt;/FONT&gt; _cost&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Get&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Set&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; value&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Decimal&lt;/FONT&gt;)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt; value &amp;gt; 0&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_cost = value&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Else&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_cost = 0&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Set&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Property&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt; _itemname&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Public&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Property&lt;/FONT&gt; ItemName()&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Get&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Return&lt;/FONT&gt; _itemname&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Get&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Set&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; value&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_itemname = value&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Set&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Property&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Class&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;So, three fields, three properties (one of which includes some validation) and just the default constructor. To create a new instance of the Product class and assign values to any of the properties that interest us, this is what we can now do:&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #d1eecc"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; P1&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; Product&lt;FONT color=#0000ff&gt; With&lt;/FONT&gt; {.ItemName =&lt;FONT color=#a31515&gt; "Widget"&lt;/FONT&gt;, .Cost = 0.99}&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;That's it! &amp;nbsp;&amp;nbsp; A Product instance with those values for the ItemName and Cost properties now exists. And of course the validation is applied effectively at the time of instantiation to avoid any unacceptable values being passed. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Note that the property assignments are contained inside curly braces and not standard parentheses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;There are several advantages to this approach. Not only does it get around the validation problem I mentioned at the start, but it also allows you to pick'n'mix the properties you want to assign values to when you instantiate the object. As you saw in my example above, I chose only to assign values to two of the properties. &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; And if that's not enough to get you interested, there's more. You even have the&amp;nbsp;further advantage that you can pass the values in any order you like (unlike the standard parameter arguments which have to be passed in a strict order). &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; Finally, if there is a parameterized constructor available that contains some, but not all, the arguments you want to pass in, then you can simply combine the two approaches and use the constructor's parameters, as well as the object initializers, all in one chunk of initialization code. &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;So if you had this parameterized constructor in the class:- &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #d1eecc"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Sub&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; code&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Me&lt;/FONT&gt;.ItemCode = code&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;you could use the following code to assign values to all three properties:-&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #d1eecc"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; P2&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; Product(&lt;FONT color=#a31515&gt;"TOOL12"&lt;/FONT&gt;)&lt;FONT color=#0000ff&gt; With&lt;/FONT&gt; {.ItemName =&lt;FONT color=#a31515&gt; "Spade"&lt;/FONT&gt;, _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Cost =&lt;FONT color=#a31515&gt; "12.99"&lt;/FONT&gt;}&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; How cool is that? &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; To be honest, I'm not sure how often I'd want to do that - given that I can do the same thing using three object initializers - but it's an option to consider. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;So there it is. A new feature that you may have heard about but weren't sure if it was going to be of use to you. Personally, I think it's great and plan on implementing this approach whenever I can from now on.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9166.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>WPF FlowDocuments and Using WPF Controls in Windows Forms</title><link>http://blogs.vbcity.com/xtab/archive/2008/07/15/9135.aspx</link><pubDate>Tue, 15 Jul 2008 17:01:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/07/15/9135.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9135.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/07/15/9135.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9135.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9135.aspx</trackback:ping><description>&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; I get a reassuringly high number of hits on my WPF&amp;nbsp; blog items, so I guess they're proving useful to a few people out there.&amp;nbsp; So it just occurred to me that I should maybe include a link here in my blog to a couple of WPF articles that I've written recently that&amp;nbsp;are published over on our devCity site that might be of interest to newcomers to WPF.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; The first one is an introduction to using &lt;/FONT&gt;&lt;A href="http://www.devcity.net/Articles/351/1/article.aspx  "&gt;&lt;FONT face=Verdana&gt;FlowDocuments in WPF&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana&gt;.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; When WPF first came on the scene, one of the features that got a lot of air time was the display of documents.&amp;nbsp; Leading edge projects like the NY Times Reader and the British Library manuscript display raised a lot of awareness of the possibilities.&amp;nbsp; Strangely though, since VS 2008 shipped there doesn't seem to have been a lot of mention of this part of the package.&amp;nbsp; So I thought I'd take a look at how easy (or not) document handling really is in WPF&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; If you look at it, you'll see that it's Part 1 of what will be at least a two part series, maybe more.&amp;nbsp;&amp;nbsp; Part 2 is written (in my head) and just needs the time to be put down over there on devCity.&amp;nbsp; Hopefully it will be done no later than next month.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; (Oh and if you were hoping the examples will stand comparison to the NYT or British Library you'll probably be in for a disappointment, but the articles will enable you to harness the technology at an easy level.)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; This week I published an article on something I've been wanting to write about for a while.&amp;nbsp; &lt;A href="http://www.devcity.net/Articles/353/1/article.aspx"&gt;Using WPF Controls in Windows Forms&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;Whenever I see a question along the lines of "How can I include an image alongside each item in a list of items or use more than one font, or&amp;nbsp;use a mix of&amp;nbsp;background colors?" I&amp;nbsp;usually find myself muttering "WPF!".&amp;nbsp;&amp;nbsp;&amp;nbsp; This is a lot more sociable and positive than many of the&amp;nbsp;things I mutter as I sit here each day, but finally I've realised that I need to do something about it.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; The result is this article, which works through the steps needed to harness the rich UI features of WPF to the more familiar paradigm of Windows Forms.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; The example used&amp;nbsp;will show you how easy it can be to use a WPF control in a Windows Forms application and you can use this article as the stepping stone to creating your own.&amp;nbsp; I've used a&amp;nbsp;WPF ListBox as an example and even the unadventurous one I've created will&amp;nbsp; give&amp;nbsp; an idea of what can be done.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; &amp;nbsp;I picked a ListBox purposely as it gives me an opportunity to show how DataTemplates work too.&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; If you're interested in WPF and want to know more about either of these topics then I hope you'll check out my articles.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9135.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>How To Navigate Around Your Code in Visual Studio</title><link>http://blogs.vbcity.com/xtab/archive/2008/07/08/9126.aspx</link><pubDate>Tue, 08 Jul 2008 15:48:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/07/08/9126.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9126.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/07/08/9126.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9126.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9126.aspx</trackback:ping><description>&lt;FONT face=Verdana&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;One of the things that I've always found a bit frustrating as a project grows larger and larger is that it gets more difficult to navigate backwards and forwards between chunks of code that you've recently accessed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; Now I know that Visual Studio has some built in navigation hot keys:&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;B&gt;Ctrl and Minus&lt;/B&gt; to go back to the last point, &lt;BR&gt;&lt;B&gt;Ctrl-Shift-Minus&lt;/B&gt; to go forward again are the most useful, &lt;/P&gt;
&lt;P&gt;&lt;BR&gt;but it can still be a bit laborious when you're trying to flit between several sets of code blocks and you don't particularly want to move in a linear or consecutive way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Step forward that very clever little Add-In (well, not so little really!) &lt;B&gt;CodeSMART&lt;/B&gt;. For some reason I'd stopped using it when I upgraded Visual Studios a while back, but when I was looking around for an answer to this problem I remembered that amongst its many little tools is the Code Explorer. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; One of the elements included in the Code Explorer window is a history of the last 20 procedures you've visited. So not only can you&amp;nbsp;move backwards and forwards one step at a time, you can of course jump to any of the saved locations just by double clicking on a choice in the list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/CodeExplorer.jpg" &lt;P &amp;gt;&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;It has some smart features (well, it is Code&lt;EM&gt;Smart &lt;/EM&gt;after all!), one of which is that it greys out any history items from other files - which helps you to very quickly identify those in your current file that you may be looking for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; And best of all? It persists your history list for each individual Visual Studio Solution, so that the next time you access a particular project Code Explorer will be repopulated with the relevant history for that project. How cool is that? &lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The Code Explorer does have additional features, such as a fast and neat search tool to take you to procedures that match search criteria, together with what's effectively a Favorites store, the Workbench, where you can keep tabs on items you're particularly interested in. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Of course, CodeSMART has a ton of other features and tools, so if you want to check it out you can download a trial version from their &lt;A href="http://www.axtools.com"&gt;website here&lt;/A&gt;.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9126.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Another Gotcha! - Select Case Using a Range of Integers</title><link>http://blogs.vbcity.com/xtab/archive/2008/07/06/9123.aspx</link><pubDate>Sun, 06 Jul 2008 17:22:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/07/06/9123.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9123.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/07/06/9123.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9123.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9123.aspx</trackback:ping><description>&lt;FONT face=verdana&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I never cease to be amazed at how many ways you can fool yourself into thinking that your code will do one thing, but in fact it does something completely different. Sometimes the subtlety of the error is so fine that you can spend ages trying to work out the source of the problem and, to make life harder, there's no Exception message to give you a pointer and sometimes even using the debugging tools doesn't lead you straight to it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I was working on something that kept track of a particular value. If the value was between 31 and 40 then I wanted to display a message. Without thinking too hard about it, I wrote the following code:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f2f2f2"&gt;&lt;FONT color=#0000ff&gt;Select&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Case&lt;/FONT&gt; InputValue&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Case&lt;/FONT&gt; 31 - 40&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MessageBox.Show(&lt;FONT color=#a31515&gt;"Invalid Values"&lt;/FONT&gt;)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Case&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Else&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Do something&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Select&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; So, what do you think? Will this do the job as described?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Actually, no - of course not; otherwise I wouldn't be sat here writing about it, would I? The question is then: What's wrong with it? On the face of it, it seems to check for values between 31 and 40, displaying a message if that case fits. Otherwise, it does something else. But if you were to try this code and enter values between 31 and 40 you wouldn't get to see that MessageBox.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;It's one of those Duh! moments as soon as you realise why. Although most of us will have read that as saying "Case 31 to 40" , that isn't how the compiler sees it. Because we are dealing with numbers and because the compiler knows about adding and (in this case most importantly) &lt;I&gt;subtracting&lt;/I&gt; .... the compiler reads that as "Case 31 &lt;I&gt;minus&lt;/I&gt; 40. So the MessageBox will only appear if a value of -9 is applied to the InputValue variable. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;It's not a compilation error, so you don't get any feedback. It's allowable but faulty syntax, so you're not alerted to what is essentially a logic mistake. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;So simple when you realise why, but it's another example of those tricky little Gotchas that'll get ya if you don't watch out! &lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9123.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>WPF Basics: How To Play Sounds and Music in Windows Presentation Foundation</title><link>http://blogs.vbcity.com/xtab/archive/2008/06/10/9091.aspx</link><pubDate>Tue, 10 Jun 2008 08:46:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/06/10/9091.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9091.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/06/10/9091.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9091.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9091.aspx</trackback:ping><description>&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;U&gt;Introduction&lt;/U&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;FONT face=Verdana&gt;You'll know by now that I'm quite a fan of WPF, but I have to say that my first&amp;nbsp;excursion into using sound in a WPF application was a bit of a disappointment.&amp;nbsp; Rather frustrating too.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;Maybe I expected too much, but I had this idea that the audio features would have been ramped up&amp;nbsp;by the same kind of&amp;nbsp;scale as the visual features have.&amp;nbsp; Sadly that's not quite the case and, although things have moved on a tad beyond having to use the&amp;nbsp; sound APIs directly in code, I still had to fiddle around more than I would have liked.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; But as I say,&amp;nbsp;maybe I'm just becoming spoiled by the wealth of other WPF features.&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; Anyway, as I had to create a&amp;nbsp; test project to work out how I could get the sounds to play as and when I needed them, I thought I'd blog some of the results.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; To keep this to a reasonable length, it will be more of a "here's how" approach than a "why it works that way" one.&amp;nbsp; Also I will be creating everything in code, not markup.&amp;nbsp; There are several more approaches and advanced&amp;nbsp;techniques available via XAML&amp;nbsp;that I won't be covering here.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;STRONG&gt;PART 1&amp;nbsp; :&amp;nbsp; The SoundPlayer&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;U&gt;1. Hard Coded File Path&lt;/U&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; Starting with possibly the least realistic scenario, you can use the SoundPlayer to play a .wav file by pointing directly to a file&amp;nbsp;via a hard coded path.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt; System.Media&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button1_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Windows.RoutedEventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; Button1.Click&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; player&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; SoundPlayer(&lt;FONT color=#a31515&gt;"C:\Temp\4WAY.WAV"&lt;/FONT&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;player.Play()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; Note the Imports statement at the top.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; This wave file will play asynchronously (I &lt;EM&gt;do &lt;/EM&gt;wish someone would come up with an easier to type word than that!&amp;nbsp; -&amp;nbsp; it's nearly as bad as 'concatenation' )&amp;nbsp;&amp;nbsp; But I digress....&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; If it's a particularly large wav&amp;nbsp;file, you can help things out by inserting a Load statement after the initialization and before the Play fires.&amp;nbsp; This will tee the player up to play without any kind of pause while it cranks up the file.&amp;nbsp;&amp;nbsp; This is something well worth bearing in mind as we users have&amp;nbsp;become more and more impatient of delays.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;FONT face=Verdana&gt;2.&amp;nbsp; Use a Content File &lt;/FONT&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; One particularly useful approach which gets around all the potential problems of hard coding file paths&amp;nbsp;is to add the file to your Solution Explorer items.&amp;nbsp; You can do this with the standard "Add Existing Item" selection from the IDE menu, adding the wav file directly to the list of files for the Project.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; This now brings me to Frustration #1.&amp;nbsp;&amp;nbsp;First, here's valid code to play the sound using a SoundPlayer object:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button6_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Object,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Windows.RoutedEventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; Button6.Click&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; player&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; SoundPlayer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;player =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; SoundPlayer(&lt;FONT color=#a31515&gt;"SND0103.WAV"&lt;/FONT&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;player.Play()&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; Now, before we move on, let's check the Solution Explorer:&lt;/FONT&gt;&lt;/P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/MissingFile.jpg"&gt; 
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT face=Verdana&gt;As you can see the file name is correctly entered and that file is sitting in the Solution Explorer's list of files that (you would think) it knows about.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; The key to fixing this problem is the 'Copy to Output Directory' Property.&amp;nbsp; By default, this is set to 'Do Not Copy'.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/DoNotCopy.jpg"&gt; 
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;You need to change this to 'Copy Always' and you'll be back on track.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;FONT face=Verdana&gt;3.&amp;nbsp;Audio File as&amp;nbsp;Resource (Frustration #2)&lt;/FONT&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; I wasn't going to include this option because it has generally caused me more trouble than it's worth (especially bearing in mind that there are several relatively trouble-free alternatives).&amp;nbsp; However, you can add a wav file as a Project Resource as normal and then play that Resource via a Stream.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; The syntax is:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; btnResource_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Object,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Windows.RoutedEventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; btnResource.Click&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; player&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; SoundPlayer(&lt;FONT color=#0000ff&gt;My&lt;/FONT&gt;.Resources.canon)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;player.Play()&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; Even with small, well tested files I often found that using this approach I would get a lot of unacceptable sound distortion (actually additional&amp;nbsp;unrelated sounds to that&amp;nbsp;expected).&amp;nbsp;&amp;nbsp; I have tried it on several PCs,&amp;nbsp; each with different sound card configurations&amp;nbsp;and the problems occur on them all. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;U&gt;5.&amp;nbsp; Other SoundPlayer Features&lt;/U&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; In addition to the standard Play method, the SoundPlayer has a PlaySync method - in case you should rather unusually want the system to be locked until the sound has finished playing.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; And finally if you want a sound to be repeatedly played, you can use the PlayLooping method to drive people mad; sanity can be restored by inserting the Stop method!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;STRONG&gt;PART 2:&amp;nbsp; MediaPlayer and Media Element&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt; As you've already seen, the&amp;nbsp; SoundPlayer has some limitations.&amp;nbsp; It only understands wav format files and you can generally only play one wav file at a time (unless you start to get quite fancy with multithreading).&amp;nbsp;&amp;nbsp;In many cases, the MediaPlayer or MediaElement will be a better option, but there is a good reason why I included the discussion of the SoundPlayer above.&amp;nbsp; In some situations on particular systems you&amp;nbsp;will have &amp;nbsp;problems getting the&amp;nbsp; two Media components to work properly.&amp;nbsp; So it's always useful to know how to use the lighter weight SoundPlayer if you run into these problems in the future.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;U&gt;6.&amp;nbsp; But I Want to Play MP3 Files!&lt;/U&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; -&amp;nbsp; or WMA or MID, etc. for that matter.&amp;nbsp; And this is a job for either the MediaPlayer or MediaElement.&amp;nbsp; Keeping to my approach of doing all the work in code, I'm therefore going to home in on the MediaPlayer.&amp;nbsp; (If you have a need to create a media object in XAML and have it play, pause, stop, trigger, etc, only in XAML then the MediaElement will be your tool of choice.)&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; Before abandoning the MediaElement approach altogether, I should point out that&amp;nbsp; you &lt;EM&gt;can&lt;/EM&gt; create the MediaElement in XAML and then access it's properties and methods in code.&amp;nbsp;&amp;nbsp; However, there is no advantage that I know of and in fact, the syntax is slightly more verbose and it also caused Frustration #4 - the fact that the audio file will only play once and then needs to be 'reset' by calling its Stop method; the Stop method apparently&amp;nbsp;has the effect of returning the playhead to the beginning of the file.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; So let's&amp;nbsp;look at&amp;nbsp;the MediaPlayer in WPF.&amp;nbsp;&amp;nbsp;It has an Open method which loads the file&amp;nbsp;in the&amp;nbsp;form of a URI.&amp;nbsp; This was Frustration #5&amp;nbsp;for me, as it took me a while to get past the problem of the "Value of Type 'String' cannot be converted&amp;nbsp;to 'System.URI.'" error.&amp;nbsp; It took a lot more&amp;nbsp; than a quick search&amp;nbsp;to finally&amp;nbsp;discover that I needed simply to add the URIKind argument to the Open method; so the considerable amount of&amp;nbsp;time I spent trying to do casts was&amp;nbsp;wasted.&lt;/FONT&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; But maybe that's something I &lt;EM&gt;should&lt;/EM&gt; have known.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; It also has Play, Pause and Stop methods, plus the more sophisticated Balance and Volume properties.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; There is another small Gotcha involved in using this control.&amp;nbsp; Take a look at the following code snippet:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; mplayer&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; MediaPlayer&lt;BR&gt;mplayer.Open(&lt;FONT color=#0000ff&gt;New&lt;/FONT&gt; Uri(&lt;FONT color=#a31515&gt;"Godfather.mp3"&lt;/FONT&gt;, UriKind.Relative))&lt;BR&gt;mplayer.Play()&lt;BR&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;FONT face=Verdana&gt; It's hard to see anything wrong with that code snippet, but if you do try it &lt;/FONT&gt;&lt;FONT face=Verdana&gt;you'll find that the audio will play for a short period of time and then - for no &lt;/FONT&gt;&lt;FONT face=Verdana&gt;apparent reason - stop.&amp;nbsp;&amp;nbsp; This is Frustration #6.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;FONT face=Verdana&gt; As I understand it, what happens here is that the new MediaPlayer object is &lt;/FONT&gt;&lt;FONT face=Verdana&gt;created, passed a URI for its source and then instructed to play.&amp;nbsp; Play begins &lt;/FONT&gt;&lt;FONT face=Verdana&gt;but at that point seemingly the MediaPlayer closes itself, saying&amp;nbsp; "I've set the &lt;/FONT&gt;&lt;FONT face=Verdana&gt;audio away, so my work here is done".&amp;nbsp; The end result is that at some &lt;/FONT&gt;&lt;FONT face=Verdana&gt;unpredictable point in time the closed MediaPlayer is released from memory &lt;/FONT&gt;&lt;FONT face=Verdana&gt;and will be disposed.&amp;nbsp; And of course&amp;nbsp;as soon as&amp;nbsp;it no longer exists there is no &lt;/FONT&gt;&lt;FONT face=Verdana&gt;means for the sound to continue to be played.&amp;nbsp; So it stops in mid note!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; Anyway, having dragged you&amp;nbsp; laboriously through that explanation, you'll probably &lt;/FONT&gt;&lt;FONT face=Verdana&gt;already have realised that the fix is simple.&amp;nbsp; All you need do is instantiate the &lt;/FONT&gt;&lt;FONT face=Verdana&gt;MediaPlayer outside the scope of the click event so that the MediaPlayer will &lt;/FONT&gt;&lt;FONT face=Verdana&gt;continue to exist throughout the lifetime of that Window or Page.&amp;nbsp; In other &lt;/FONT&gt;&lt;FONT face=Verdana&gt;words, like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt; Window1&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; mplayer&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; MediaPlayer&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button1_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Windows.RoutedEventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; Button1.Click&lt;FONT color=#008000&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mplayer =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; MediaPlayer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mplayer.Open(&lt;FONT color=#0000ff&gt;New&lt;/FONT&gt; Uri(&lt;FONT color=#a31515&gt;"Godfather.mp3"&lt;/FONT&gt;, UriKind.Relative))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mplayer.Play()&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;BR&gt;&lt;BR&gt;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Class&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; As with the SoundPlayer sample, the mp3 file has been&amp;nbsp;added to the project files in Solution Explorer,&amp;nbsp; its Build property set to Content and its Copy To Output Director set to Copy Always.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; If the file is extra large and you get a pause before it actually begins to play then you can of course move the mediaplayer&amp;nbsp;instantiation and Open code to, for instance, the Window Loaded event or some other place where the delay won't be so obvious.&amp;nbsp; It will then be teed up ready to go as soon as the Play method is invoked.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;U&gt;7.&amp;nbsp; Summary&lt;/U&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; There isn't much else to add.&amp;nbsp; I think you now have the&amp;nbsp;tools you need to&amp;nbsp; play sound and music files in WPF.&amp;nbsp; By the way, that does also mean both sounds and music at the same time, because you can have multiple instances of MediaPlayers running concurrently (or MediaPlayer(s) and a SoundPlayer running together).&amp;nbsp;&amp;nbsp; Therefore&amp;nbsp; you can have almost any combination of sound files running, subject to the limitations of your system, that is.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; I hope you will find this information useful in your future projects if you need to include sound and want to avoid the frustration of the learning curve.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; &lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9091.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Searching Through Folders to Find File(s)</title><link>http://blogs.vbcity.com/xtab/archive/2008/06/04/9086.aspx</link><pubDate>Wed, 04 Jun 2008 16:10:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/06/04/9086.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9086.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/06/04/9086.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9086.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9086.aspx</trackback:ping><description>&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; I was looking for a task to use as&amp;nbsp;the background task&amp;nbsp;of my recent &lt;A href="http://www.devcity.net/Articles/352/1/article.aspx"&gt;article on the BackgroundWorker component &lt;/A&gt;and decided that I'd use a recursive search through folders.&amp;nbsp;&amp;nbsp; Ironically, I ended up spending about twice as much time on getting&amp;nbsp;the recursive search procedures running correctly&amp;nbsp;than I spent on the main BackgroundWorker code.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; As I ended up with three varieties of recursive searches&amp;nbsp;- and found&amp;nbsp; some things that should have worked, but didn't seem to - I thought I'd blog it.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; To find a particular file type in folders and sub-folders I used this code:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; FileFinder(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; dir&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Try&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Display all files in a directory that match file type&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; fname&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; IO.Directory.GetFiles(dir)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt; fname.EndsWith(&lt;FONT color=#800000&gt;"txt"&lt;/FONT&gt;)&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;pass back progress message&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(fname)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' A recursive call for all the subdirectories in this directory.&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; subdir&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; IO.Directory.GetDirectories(dir)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FileFinder(subdir)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Catch&lt;/FONT&gt; ex&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Exception&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MessageBox.Show(ex.Message.ToString)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Try&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;FONT face=Verdana&gt;&amp;nbsp; You'll probably have spotted that I used the "EndsWith" function there to test for the tile type I'm interested in.&amp;nbsp;&amp;nbsp; Originally I tried using the SearchPattern parameter of the GetFiles method, but for whatever reason it didn't identify the files for me.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; Another little glitch to keep in mind is that there&amp;nbsp;may be&amp;nbsp;some files to which users don't have authorised access.&amp;nbsp; If for example you call the above code and pass in "C:\" as the argument you will probably get&amp;nbsp;one or more&amp;nbsp;"Access Denied" messages via the Exception Handler.&amp;nbsp; Of course, because the exception is being handled, the user can click on the messagebox's OK button and the procedure will continue to run.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; Obviously, to avoid the message appearing at all you can just comment out the MessageBox.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; If you want to home in on that particular issue and allow access but still want to know if other kinds of problems occur then you can fine tune your structured exception handlers (as of course you should) and include a handler specifically aimed at the access exception:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;Catch&lt;/FONT&gt; ioex&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.UnauthorizedAccessException&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Access Denied Problem will be handled here&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;Catch&lt;/FONT&gt; ex&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Exception&lt;BR&gt;&lt;FONT color=#008000&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Other Exceptions will be flagged up to users&lt;/FONT&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MessageBox.Show(ex.Message.ToString)&lt;BR&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; Hard coding the file type limits reusability, so a better version would be:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Sub&lt;/FONT&gt; FileTypeFinder(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; dir&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; FileExt&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Try&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Display all files in a directory that match file type&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; fname&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; IO.Directory.GetFiles(dir)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt; fname.EndsWith(FileExt)&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;pass back progress message&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(fname)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' A recursive call for all the subdirectories in this directory.&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; subdir&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; IO.Directory.GetDirectories(dir)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FileTypeFinder(subdir, FileExt)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Catch&lt;/FONT&gt; ioex&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.UnauthorizedAccessException&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Catch&lt;/FONT&gt; ex&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Exception&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MessageBox.Show(ex.Message.ToString)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Try&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;BR&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; The client now passes in the file type as well as the parent folder.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; The final version I used was one which recursively searched through folders and subfolder, found files that matched a particular file extension and then searched inside matching files for a particular string.&amp;nbsp; This of course could be a particularly long task in some cases and so a good candidate for the BackgroundWorker.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; FileContentFinder(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; dir&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; FileExt&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;, _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ByVal&lt;/FONT&gt; TargetText&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; rdr&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; IO.StreamReader&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Try&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Display all files in a directory that match file type&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; fname&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; IO.Directory.GetFiles(dir)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt; fname.EndsWith(FileExt)&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rdr =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; IO.StreamReader(fname)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; strFileContents&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt; = rdr.ReadToEnd&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rdr.Close()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rdr =&lt;FONT color=#0000ff&gt; Nothing&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' If this file contains the target string, pass back progress message&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt; strFileContents.Contains(TargetText)&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(fname)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' A recursive call for all the subdirectories in this directory.&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; subdir&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; IO.Directory.GetDirectories(dir)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FileContentFinder(subdir, FileExt, TargetText)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Catch&lt;/FONT&gt; ex&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Exception&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MsgBox(ex.Message.ToString)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Try&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; There are many other twists you could use on the above approaches, such as testing against file attributes for dates, for example, or copying files that meet certain criteria.&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9086.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>WPF Basics: Using Animation to Fade Colors In and Out</title><link>http://blogs.vbcity.com/xtab/archive/2008/05/07/9058.aspx</link><pubDate>Wed, 07 May 2008 21:03:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/05/07/9058.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9058.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/05/07/9058.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9058.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9058.aspx</trackback:ping><description>&lt;FONT face=verdana size=2&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;One of the great things about WPF is its ability to let you use animation to enhance your user interface. (Or wreck it if you go too far!) In my opinion, subtle animations can go a long way towards improving the user experience. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; One particular way is to fade colors in and out so that text seems to appear and disappear in a way that gives the impression of movement, but without any jarring visual effects.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Doing this in the XAML markup is well documented and you will find many examples around the place. However, I recently had a situation where I wanted to create this animation in the VB code behind. Now, I knew that anything that could be done in XAML can be done in VB, but when it came down to the task it actually caused me more problems than I'd expected. And when I started to search for answers, it took a while. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;It turned out that the reason for my difficulty was a fundamental misunderstanding on my part. I was thinking "color" as being the element to change or animate; in reality it doesn't work quite like that. But after a good deal of trial and error, I eventually hit on the answer. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; In this case I wanted to fade the text on a button from its original black to white. But because I would use this effect in a number of situations, including some where there was no user driven event to fire it, I decided that putting the animation instructions in the code behind was the way to go. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I'll start with the XAML though, as I think that in most cases this will be your preferred route. I'll cover the VB approach at the end.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;here's the XAML:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#a31515&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;Button&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; Margin&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="30,67,0,111"&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; Name&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="Button1"&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; Foreground&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="Black"&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; HorizontalAlignment&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="Left"&amp;gt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;Animated Button&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#800000&gt;Button&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;lt;Button&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; HorizontalAlignment&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="Right"&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; Margin&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="0,77,96,111"&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; Name&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="Button2"&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; Click&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="Button2_Click"&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; Content&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="Animated Button"&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; &amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;Button.Triggers&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;EventTrigger&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; RoutedEvent&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="Button.Click"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;BeginStoryboard&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;Storyboard&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; TargetProperty&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="Foreground.Color"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;ColorAnimation&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; To&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="White"&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; Duration&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;="0:0:2" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#800000&gt;Storyboard&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#800000&gt;BeginStoryboard&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#800000&gt;EventTrigger&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#800000&gt;Button.Triggers&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;BR&gt;&amp;lt;/&lt;/FONT&gt;&lt;FONT color=#800000&gt;Button&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;gt;&lt;/FONT&gt;&lt;FONT color=#a31515&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Most of it is pretty straightforward:&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;1. &amp;nbsp;Having first created the button and set its main visual properties, we create a Triggers block for the Button.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;2. &amp;nbsp;Next we identify the trigger to be used to fire this animation. In this case it is the Button's Click event. &lt;/P&gt;
&lt;P&gt;3.&amp;nbsp;&amp;nbsp;The Storyboard is then created and we identify the Target Property which is the Foreground Color of the button. &lt;/P&gt;
&lt;P&gt;4.&amp;nbsp;&amp;nbsp; Finally, the animation details which comprise the starting color, the ending color and the duration in seconds. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The remainder of that code snippet are closing tags.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Now it's the fact that the TargetProperty is identified as the Foreground.Color that threw me off the track when I came to create a VB version of this task. I was trying to find a way of assigning the animation &lt;I&gt;directly&lt;/I&gt; to the Foreground Color of the button (or the Button.Content Color or various other permutations - none of which worked).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The reason is that you have to set the animation on a &lt;I&gt;Brush&lt;/I&gt;, and not directly to the Foreground property. You then assign this brush as the one to be used for the Foreground. And finally, when you run the animation to change the color of the brush, the animation is then seen to be applied to the Foreground. I hope that makes sense!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Here's the VB code that does this:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Times New Roman'; BACKGROUND-COLOR: #f5f8d6"&gt;&lt;FONT color=#008000&gt;' Create color animation sequence.&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;Dim&lt;/FONT&gt; blackToWhite&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; ColorAnimation =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; ColorAnimation(Colors.White,&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; Duration(&lt;FONT color=#0000ff&gt;New&lt;/FONT&gt; TimeSpan(0, 0, 2)))&lt;BR&gt;&lt;FONT color=#008000&gt;&lt;BR&gt;'Create a new brush and apply the color animation to the brush&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;Dim&lt;/FONT&gt; scb&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; SolidColorBrush =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; SolidColorBrush(Colors.Black)&lt;BR&gt;&lt;BR&gt;scb.BeginAnimation(SolidColorBrush.ColorProperty, blackToWhite)&lt;BR&gt;&lt;FONT color=#008000&gt;&lt;BR&gt;'Assign the brush to Button's Foreground&lt;/FONT&gt;&lt;BR&gt;Button1.Foreground = scb&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#a31515&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Like all these little glitches and gotchas, you wonder how it seemed so difficult once you've seen the answer. But as we all know, it's really easy to set yourself off on the wrong mindset and find yourself needlessly chasing your tail trying to get the exact syntax for what you're trying to achieve. Hopefully if you need to run this kind of animation in code, you'll now be saved a bit of potential frustration. &lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9058.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>VB 2005: How To Print With Word Wrapped Lines</title><link>http://blogs.vbcity.com/xtab/archive/2008/04/08/9021.aspx</link><pubDate>Tue, 08 Apr 2008 17:06:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/04/08/9021.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9021.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/04/08/9021.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9021.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9021.aspx</trackback:ping><description>&lt;FONT face=verdana size=2&gt;
&lt;P&gt;&lt;U&gt;Introduction&lt;/U&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;Printing can often be a bit of a tricky task in .NET. Some things are more easily resolved than others though.&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;I saw a post recently that raised a problem that seems to occur fairly regularly: You want to print a large chunk of text to the printer, but VB seems to want to print everything all on one long line - even when this means that a lot of the text will be truncated or "printed" invisibly into thin air! &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Fortunately, this is one of the cases where the solution is relatively easy. Essentially, what you do is set a rectangle whose width represents the width of your printed page (or any lesser width you prefer). You then pass this rectangle to the DrawString method and it will obediently print within those boundaries.&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Solution&lt;/U&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; Here's the basic solution: &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f2f2f2"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Imports&lt;/FONT&gt; System.Drawing.Printing&lt;BR&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;Public&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Class&lt;/FONT&gt; frmPrint&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' The document you will use to print on&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; DocPrint&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; PrintDocument&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Set font for printing&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; fnt&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Font =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; Font(&lt;FONT color=#a31515&gt;"Verdana"&lt;/FONT&gt;, 12, FontStyle.Regular)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;'&amp;nbsp;&amp;nbsp;The printing is fired with a button click event&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button1_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Object,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; Button1.Click&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Print the textbox content&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DocPrint =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; PrintDocument&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' A handler for it&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AddHandler&lt;/FONT&gt; DocPrint.PrintPage,&lt;FONT color=#0000ff&gt; AddressOf&lt;/FONT&gt; PrintTheTextBox1&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Print the document&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DocPrint.Print()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;The procedure that does the printing&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; PrintTheTextBox1(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; PrintPageEventArgs)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'Set a rectangle to bound the&amp;nbsp;&amp;nbsp;printing&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; PrintRect&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; RectangleF&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Width = 800&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Height = 1000&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Print the text from the TextBox:&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(TextBox1.Text, fnt, Brushes.Black, PrintRect)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;BR&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;/U&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Yes, But.....&lt;/U&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;That's OK as far as it goes, but it does suffer from one flaw. When you check your printed page you'll find that the text is stuffed right up at the top left of the paper and each line extends fully across to the very last few millimetres at the right hand side. Now, it may be that paper saving is high on your agenda, in which case you'll be happy. But generally you will probably want a layout that's a bit more generous with its whitespace.&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; The trouble is that there isn't an overload that allows you to pass in the Rectangle &lt;I&gt;&lt;B&gt;and&lt;/B&gt;&lt;/I&gt; the start position for the printing. Is that going to stump us? Nah, of course not! &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;If we go back to the Rectangle itself, there is a constructor overload that allows you to pass in the x and y values as well as the width and height. The x and y values will of course be used as the left and top points respectively of the rectangle as it is positioned on the printed page. So this variation will get the start position, the text line width and text block height all sorted:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f2f2f2"&gt;&lt;FONT color=#0000ff&gt;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; PrintTheTextBox2(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; PrintPageEventArgs)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'Set a rectangle to bound the&amp;nbsp;&amp;nbsp;printing&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; PrintRect&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; RectangleF(30, 100, 750, 1000)&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Assign its Left, Top, Width and Height values&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Print the text from the TextBox:&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(TextBox1.Text, fnt, Brushes.Black, PrintRect)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;While I was working on that, I remembered that the Rectangle has a useful little method named Offset (something you would normally use when drawing actual rectangles in a graphics based task). However, by using this method and adjusting the values that are passed so that the top left of the rectangle is shunted down and to the right, we can get nearer to the layout we desire. If you chose to use this alternative route, it would look like this: &lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f2f2f2"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; PrintRect&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; RectangleF&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Width = 800&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Height = 1000&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Offset(25, 60)&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Show Me The Width&lt;/U&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;It probably won't have escaped your notice that I very conveniently just happened to know what width and height I needed for the text to span across the page. Many times you will have this information to hand, but if you don't, what do you do then? Answer: You calculate it by querying the PrintDocument's PageSettings, find the width, height and margins and use those. Here's the code: &lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f2f2f2"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; PrintTheTextBox3(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; PrintPageEventArgs)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'Set a rectangle to bound the&amp;nbsp;&amp;nbsp;printing&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; PrintRect&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; RectangleF&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;With&lt;/FONT&gt; DocPrint.DefaultPageSettings&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Width = .PaperSize.Width - (.Margins.Left + .Margins.Right)&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Height = .PaperSize.Height&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Offset(.Margins.Left, .Margins.Top)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; With&lt;/FONT&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Print the text from the TextBox:&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(TextBox1.Text, fnt, Brushes.Black, PrintRect)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Summary&lt;/U&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Although there are many more tweaks we could add to this approach, I think that the above will cover many common situations. If text trimming at the end of lines is something you need to refine to the nth degree then you should check out the StringFormat class in general and the StringTrimming enumeration in particular. There are several options available there. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Don't forget of course that you may not need to hand code your printing processes at all. There is a free Power Pack available which will print the contents of a form with minimum effort on your part. This will often be all that you need, but if not then at least you now know how to package up a bundle of text and send it to the printer to be laid out in a way that suits your requirements. &lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9021.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Pro WPF with VB 2008 by Matthew MacDonald</title><link>http://blogs.vbcity.com/xtab/archive/2008/04/06/9016.aspx</link><pubDate>Sun, 06 Apr 2008 11:02:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/04/06/9016.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9016.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/04/06/9016.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9016.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9016.aspx</trackback:ping><description>&lt;FONT face=verdana size=2&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Friday was something of a Red Letter Day in the Mead household, because that was the day that the book I've been looking forward to getting for months finally arrived on my doorstep. At last - a WPF book aimed at those of us who prefer to use Visual Basic for the code behind. The book is Matthew MacDonald's "Pro WPF with VB 2008", published by Apress. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;In the couple of days since it arrived I obviously haven't been able to get too deeply into reading it - although my wife might offer a different opinion on how many hours I've spent with my head in this book since Friday! However, it's immediately clear that this is another well-written and comprehensive piece of work from a widely respected author.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Unsurprisingly, large chunks of content have been ported directly from his earlier WPF book, which was based on .NET 3.0 and has C# as the code behind. This makes complete sense as it is only the code behind aspects that need the different approach. We VB-ers are just as able to understand the general WPF and XAML explanations as our allegedly "sharper" developer brethren. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Because this is based on WPF 3.5 though, there are some additional items, such as binding to a LINQ expression in Chapter 16. There is also a completely new additional Chapter 26, which deals with the topics of Multithreading and Add-Ins.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I don't think Apress have the full chapter listing on their site yet (or at least I couldn't see it if they did). However, rest assured that the whole gamut of WPF topics is covered in this book, from Layout to Dependency Properties, Routed Events to Navigation - in fact everything from Animation to Z-Index. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The author has a very useful list of links that you can simply click on to save you (mis)typing them yourself from the book. These links and the downloadable code samples&amp;nbsp;are available &lt;A href="http://www.prosetech.com/"&gt;from here&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;In my opinion, you should buy this book for two reasons. &lt;BR&gt;&lt;BR&gt;&amp;nbsp;First, it is an excellent, wide ranging, clear description of what you will need to know in order to get fully to grips with this exciting (but not always intuitive) technology. &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;Second, there have been at least five WPF books published in the past year that have C# as the code behind. Publishers are in the business of selling books and they couldn't care less about whether C# or VB are "better". So I guess it's obvious that their stats show that they will sell more C# books. If you want to keep VB as a viable language, fully supported by authors and publishers, then the only way to ensure this is to make it worth their while to publish VB based books. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;So for either or both these reasons, I rate this book as one of the most worthwhile investments you can make if you are a VB developer (or student) and you want to fully embrace all the tempting offerings available in Windows Presentation Foundation.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9016.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Developer Day Scotland - 10th May 2008</title><link>http://blogs.vbcity.com/xtab/archive/2008/04/03/9011.aspx</link><pubDate>Thu, 03 Apr 2008 08:14:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/04/03/9011.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9011.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/04/03/9011.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9011.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9011.aspx</trackback:ping><description>&lt;P align=center&gt;&lt;A href="http://developerdayscotland.com/main/Default.aspx"&gt;&lt;IMG height=192 alt="Developer Day Scotland" src="http://developerdayscotland.com/images/badges/GetReady1-medium.png" width=144 border=0&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P align=center&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=4&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=4&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face=Verdana&gt;The day of&amp;nbsp;the first ever Developer Day Scotland is getting close!&amp;nbsp;&amp;nbsp; Registration is still open and I hear that there are still a few places left.&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&amp;nbsp; So if you are anywhere in reach of Glasgow on Saturday 10th May 2008&amp;nbsp;and want to take advantage of this&lt;STRONG&gt; FREE&lt;/STRONG&gt; event for developers, you can Register from &lt;/FONT&gt;&lt;A href="http://developerdayscotland.com/main/Default.aspx"&gt;&lt;FONT face=Verdana&gt;this page.&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; Wondering what's in it that might interest you?&amp;nbsp;&amp;nbsp; Check out the &lt;/FONT&gt;&lt;A href="http://developerdayscotland.com/main/Agenda/tabid/68/Default.aspx"&gt;&lt;FONT face=Verdana&gt;agenda here&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; Colin and the guys have put in a huge amount of effort to get this off the ground, so I'm sure it will be a great day and well worthwhile attending.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt; By the way, on a personal note, I'm hoping to use the event to assess if there is enough interest around to start a SouthWest Scotland (or even a SW Scotland and Cumbria) Section of Scottish Developers.&amp;nbsp;&amp;nbsp; So if you live in this general catchment area and think you could be interested in joining&amp;nbsp;such a group, please talk to me on the day.&amp;nbsp; And if you can't make the day and are still interested, contact me via this blog or email to xtab at vb(nospace)city dot com.&lt;/FONT&gt;&lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9011.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Murach's Visual Basic 2008 - Book Review</title><link>http://blogs.vbcity.com/xtab/archive/2008/03/09/8998.aspx</link><pubDate>Sun, 09 Mar 2008 15:07:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/03/09/8998.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/8998.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/03/09/8998.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/8998.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/8998.aspx</trackback:ping><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/MurachVB2008Book.jpg"&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The latest .NET book from the Murach publishing house is "Murach's Visual Basic 2008". This book is essentially an update of their earlier book for VB 2005 which now includes additional coverage of new features such as Anonymous Types, Object Initializers and LINQ.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I've just been re-reading my original review of the 2005 book and much of what I said then applies equally to this latest version. Here's what I said then: &lt;/P&gt;
&lt;P&gt;&amp;nbsp; We all find that some particular authoring styles work better than others for us individually. Some of us like a mass of detail and repetition; some like a few terse lines of explanation. Some of us like to be jollied along with the occasional funny; some like to stay serious. Some like the pages to be jam-packed with screenshots and diagrams; others think this is a waste of teaching space. &lt;BR&gt;&lt;BR&gt;&amp;nbsp; It's an individual thing. &lt;BR&gt;&lt;BR&gt;&amp;nbsp; Murach have developed their own particular individual style. They use a "facing page" technique. Open the book up at any section and on the left hand page you will find a discussion or explanation of a topic. On the facing page you will see more specific information about this topic, possibly with a diagram or screenshots, or very often demonstration code samples. &lt;BR&gt;&lt;BR&gt;&amp;nbsp; The benefit of this approach is that the author has two bites of the teaching cherry. She (in this case, author Anne Boehm) can introduce a topic on the left hand page, cover the essentials and be ready to move on. The value of the right hand facing page though is that it can be used to show additional detail, display sample code and example results and also (this I think being very important) can summarise the key points covered on the first page. The reinforcement of learning points in this way can be a very valuable tool. &lt;BR&gt;&lt;BR&gt;&amp;nbsp; Essentially, the reader can decide if he or she has understood enough from the left side page to be ready to move on, or if not then they can opt to read the right hand page for more info, samples or reinforcement and confirmation of their understanding. &lt;BR&gt;&lt;BR&gt;&amp;nbsp; Personally I quite like the approach. I tend to dive into books almost at random sometimes, but often just needing to refresh my memory on a particular point. If I'm using a Murach book and need further detail, I can dig into the additional facing page info as much as needed for any one topic, or even specific part of a topic. &lt;BR&gt;&lt;BR&gt;&amp;nbsp; If there is a down side to the Murach approach it's probably that because of the facing pages approach they have to limit the number of VB.NET topics they can actually cover in the 800+ pages limit . That said, what they do cover is fairly comprehensive and contains plenty of material to keep a VB.NET beginner engrossed for many a long night. And, most importantly, those topics are covered thoroughly and clearly in plain English. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;A full list of the book Contents can be seen &lt;A href="http://www.murach.com/books/vb08/toc.htm"&gt;&lt;STRONG&gt;here&lt;/STRONG&gt;&lt;/A&gt; and as you will see, you can drill further into each chapter to exactly what is covered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;If you want to know if the Murach style will suit you, then you can take a look at two Sample Chapters &lt;A href="http://www.murach.com/books/vb08/chapters.htm"&gt;&lt;STRONG&gt;here&lt;/STRONG&gt;&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;In summary, this is another clearly written, well laid out offering from the Murach stable. This book is particularly suitable for newcomers to VB.NET, whether total developer beginners or those moving to .NET from VB Classic.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8998.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Applications = Code + Markup : VB.NET Code Samples     </title><link>http://blogs.vbcity.com/xtab/archive/2008/03/09/8997.aspx</link><pubDate>Sun, 09 Mar 2008 11:43:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/03/09/8997.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/8997.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/03/09/8997.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/8997.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/8997.aspx</trackback:ping><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp;I've been working in conjunction with Evan Lim and Young Joo to convert the source code examples from Charles Petzold into VB.NET. As at today there still doesn't seem to be a WPF book on the market that has&amp;nbsp; VB.NET as the code-behind (although I know there are a couple on the way). Add to this the fact that many C# to VB converters at the moment still get it wrong with several areas of the new Framework, this is a real stumbling block&amp;nbsp;for many VB developers who would otherwise be much more interested in digging into WPF. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp;The link to the final version of the conversion of Petzold samples is &lt;A href="http://blogs.msdn.com/vbteam/pages/Petzold-WPF-VB-Samples.aspx "&gt;&lt;STRONG&gt;here&lt;/STRONG&gt;&lt;/A&gt;. You can download individual chapter code or the whole shebang from the links in that blog item&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp;I've just started working on a similar project to convert the C# code of another WPF book. I'll post more once I've got something to show. In the meantime I hope that the Matthew McDonald and Billy Hollis WPF/VB books will be on the shelves before long.&lt;/FONT&gt;&lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8997.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>VB2005: How To Remove an Item from a ListBox</title><link>http://blogs.vbcity.com/xtab/archive/2008/02/19/8982.aspx</link><pubDate>Tue, 19 Feb 2008 17:26:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/02/19/8982.aspx</guid><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;This is something that seems to come up fairly often in the Forums. Someone wants to test for a particular value in a ListBox and if the list does (or doesn't) contain the value then that item is to be deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;One common way of doing this would be to use the Contains function to test if the target character or substring exists in the ListBox.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; This isn't a particular difficult task, but the thing that usually catches most beginners out here is that you have to traverse the ListBox items in reverse order. Well, you do if you actually end up deleting any items, anyway. The reason being that if you start from the top and work down, then as soon as you delete an item the ListBox's indices get out of sync and an ArgumentOutOfRange Exception will be thrown and you will get a message along the lines of the one shown below: &lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/ArgumentOutOfRange.jpg"&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; In a recent post, a slight variation on this theme was that the check was to be made in one ListBox and if the required search string wasn't found in that first ListBox then not only was the item in the first ListBox to be deleted but also the corresponding item number in a second ListBox was also to be deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Again this is straightforward and the following code will do the job :&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #eaeaea"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button1_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Object,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; Button1.Click&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/FONT&gt; i&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt; = ListBox1.Items.Count - 1&lt;FONT color=#0000ff&gt; To&lt;/FONT&gt; 0&lt;FONT color=#0000ff&gt; Step&lt;/FONT&gt; -1&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Not&lt;/FONT&gt; ListBox1.Items(i).Contains(TextBox1.Text)&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ListBox1.Items.RemoveAt(i)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ListBox2.Items.RemoveAt(i)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;As I say, it's not rocket science, but that traversing upwards trick&amp;nbsp;is something worth knowing for these kind of situations.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8982.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>WPF Basics: Video Demonstration - Creating A Simple ControlTemplate</title><link>http://blogs.vbcity.com/xtab/archive/2008/02/03/8968.aspx</link><pubDate>Sun, 03 Feb 2008 20:08:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/02/03/8968.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/8968.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/02/03/8968.aspx#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/8968.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/8968.aspx</trackback:ping><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/VideoTutorials/cs_logo.gif"&gt;   I've had a copy of the excellent Camtasia Studio from TechSmith on my system for a while now and I've been itching to find time to create a video demo or two. &lt;BR&gt;&lt;BR&gt;&lt;BR&gt;   Just as everyone warned me it would be, it's a lot harder and takes wa-ay longer than you think it will, but my first effort is finally finished.&lt;/P&gt;
&lt;P&gt;  As you can see from the blog heading, the video is a mini-tutorial on the subject of how to create a simple ControlTemplate in WPF.   The example I've used is a round button, just to show the techniques rather than try and get too fancy with the design.&lt;BR&gt;&lt;BR&gt;    It's 99.9%  XAML; just one tiny bit of VB used to demonstrate that the templated button is wired up to the standard button Click event.&lt;/P&gt;
&lt;P&gt;   The full-length video runs to just over 12 minutes and you can access it from &lt;A href="http://www.xtabvbcity.plus.com/VideoTutorials/WPFControlTemplate.wmv"&gt;HERE &lt;BR&gt;&lt;/A&gt;&lt;BR&gt;or download a zipped copy &lt;A href="http://www.xtabvbcity.plus.com/VideoTutorials/WPFControlTemplate.zip"&gt;HERE&lt;/A&gt; &lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/VideoTutorials/silverlight_logo_small.gif"&gt; &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;  In addition, I was greatly encouraged by Karl Shifflett's &lt;A href="http://www.codeproject.com/KB/scrapbook/HowToCreateArticleVideos.aspx"&gt;clear and comprehensive article &lt;/A&gt;(not forgetting his demo videos!) on CodeProject  to try and create a Silverlight version too. In order to keep within the bandwidth limitations, I've had to break the original demo into two parts.&lt;/P&gt;
&lt;P&gt;  You can view the same video streamed as a Silverlight application from the links below:&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;A href="http://silverlight.services.live.com/invoke/49907/ControlTemplates1/iframe.html" target=_blank&gt;WPF ControlTemplate Part 1 Video&lt;/A&gt; &lt;BR&gt;&lt;BR&gt;&lt;A href="http://silverlight.services.live.com/invoke/49907/ControlTemplates2/iframe.html" target=_blank&gt;WPF ControlTemplate Part 2 Video&lt;/A&gt; &lt;/P&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt;  If you want to view the Silverlight versions full screen, select the button shown in the image below for Full Screen and the Esc key to return to Default Size&lt;/P&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/VideoTutorials/videoplayercontrols.jpg"&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8968.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>First Chance Exception and XamlParseException</title><link>http://blogs.vbcity.com/xtab/archive/2008/02/01/8966.aspx</link><pubDate>Fri, 01 Feb 2008 11:44:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/02/01/8966.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/8966.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/02/01/8966.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/8966.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/8966.aspx</trackback:ping><description>&lt;FONT face=verdana size=2&gt;
&lt;P&gt;&lt;U&gt;First Chance Exceptions&lt;/U&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;I suspect that most of us have become so used to seeing the message that reads:- &lt;BR&gt;&lt;BR&gt;&lt;FONT face="courier new"&gt;" A first chance exception of type 'System.ApplicationException' occurred in WindowsApplication1.exe " &lt;/FONT&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;that it hardly hits our conscious thoughts any more.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;But did you ever check out just what this message really means or wonder whether you should or could do anything about it ? &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I suppose the first thing you need to know in order to answer those questions is just exactly what a First Chance Exception &lt;EM&gt;is&lt;/EM&gt;. Well, in it's simplest terms it's a mechanism that flags up that there is a possible error condition. The key point here though is that this flagging up takes place in the &lt;I&gt;Debugger&lt;/I&gt; and not in the running application itself. (We probably often think of the two as being the same thing, but in fact that's not really the case).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;When the Debugger detects an Exception situation it raises a First Chance Exception. In several situations it isn't always desirable for the program to come to a grinding halt each time. And of course, being a skilled developer, you will have built appropriate Try Catch exception handling blocks into the code which will sort the problem anyway! So, depending on the Debugging settings you have laid down (more on this later), the Debugger allows this First Chance Exception to pass and the program continues to run. &lt;/P&gt;
&lt;P&gt;&lt;U&gt;Second Chance Exceptions&lt;/U&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; At this point, where the &lt;I&gt;running program&lt;/I&gt; now has to deal with the Exception situation, the application will either handle the Exception or not handle it (your Structured Exception Handling code coming in to play here). If it does handle it, then the application will of course be allowed to proceed and it will happily keep running until the next problem is encountered or the application ends. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;If the program fails to deal with the Exception at this point, the Debugger is notified again of this unhandled error situation - and this is now the Second Chance Exception level. Hitting this point will result in the kind of Unhandled Exception Message Box that I know you will have seen many, many times: &lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/ErrorMessage.jpg"&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;An Exceptional Situation - XamlParseException&lt;/U&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;Although I'd never really given it much thought I guess I'd always assumed that by default the settings in Visual Studio were such that all First Chance Exceptions were allowed through to the program as described above. With some timely assistance from the Microsoft Visual Basic team, I discovered recently that this is not the case. There is one Exception which by default is set to Throw on a First Chance Exception - and that is the XamlParseException. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; When I say "by default" I'll have to qualify that slightly. I'm not sure that it is set by default in &lt;I&gt;every&lt;/I&gt; kind of installation of Visual Studio 2008, but certainly in the Pro and Express Editions if you have set the Profile to Visual Basic developer settings then this will be the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;As a result I spent quite a long time struggling with a particular project that had to deal with many situations where the Xaml at certain points in time was not valid and so would not parse. Even though I had built in a Try Catch block for this Exception, the program would never reach that exception handler because the Debugger would throw the Exception at the first chance stage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The route to finding this setting in the IDE is as follows:&lt;BR&gt;&lt;BR&gt;Select Debug &amp;gt; Exceptions &lt;BR&gt;then in the Window that appears, choose "Common language Runtime Exceptions" and expand the list. &lt;BR&gt;Navigate down the list until you reach "System.Windows.Markup". &lt;BR&gt;Next click on the plus symbol to show the Exceptions in this class. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;(Alternatively you can click the "Find..." button and enter the name of the Exception).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;You can see in the screenshot below that the "Thrown" checkbox is checked:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/XamlParseException.jpg"&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;which is of course the reason why my project was stopping in Debug mode whenever it first hit a XamlParseException.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;If you want your code to be allowed to handle this kind of Exception (as I did) then of course you just need to uncheck that box.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I did a quick sweep of all the settings for all Exceptions and that XamlParseException seems to be the only one that is checked by default. But if you should come across a similar puzzling situation yourself in the future, you should probably think of those Exception settings as one of the places you should first look.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8966.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>