<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://vbcity.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"><channel><title>vbCity - The .NET Developer Community</title><link>http://vbcity.com/blogs/</link><description>The .NET Developer Community</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Custom Groupbox - Part 3, Events and Properties</title><link>http://vbcity.com/blogs/steve/archive/2016/10/17/custom-groupbox-part-3-events-and-properties.aspx</link><pubDate>Tue, 18 Oct 2016 01:49:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:722699</guid><dc:creator>Steve</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;b&gt;&lt;span style="text-decoration:underline;"&gt;Part 3 - Events and Properties&lt;/span&gt;&lt;/b&gt;
&lt;p&gt;&amp;nbsp;&lt;b&gt;&lt;span style="text-decoration:underline;"&gt;Events&lt;/span&gt;&lt;/b&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Make a region called Events. Notice how these events are typed - they have not been selected from the drop-down box. This control is a derived class, and as the MSDN documentation states &amp;quot;&lt;i&gt;The ......&amp;nbsp; method also enables derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class&amp;quot;.&lt;/i&gt; Notice the first line of each event. This first line (ie the &amp;quot;MyBase&amp;quot; call) ensures the&lt;i&gt; &amp;quot;registered delegate receives the event&amp;quot;&lt;/i&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;1 OnPaint Event&lt;/span&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This event does the painting of the control &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;2 OnFontChanged Event&lt;/span&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The label font is mapped to the panel font property. A change of font could mean a different label length, so the labelLeft / centre subs are called again to realign the label as required. The &amp;quot;Invalidate&amp;quot; call makes the control repaint itself. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;3 OnForecolorChanged Event&lt;/span&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;The label forecolor is mapped to the panel forecolor property. Again, &amp;quot;Invalidate&amp;quot; is called to repaint the control. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Here is a screenshot of the events in their region..... &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6523.0301-Events.jpg"&gt;&lt;img height="369" width="593" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6523.0301-Events.jpg" border="0" alt="" /&gt;&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="text-decoration:underline;"&gt;Properties&lt;/span&gt;&lt;/b&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Properties will give us the option to change certain characteristics of the groupbox. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;At the start of part 1, we made a few imports statements that included the &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;ComponentModel&lt;/span&gt;&amp;quot;..... &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1030.0302-Imports-_2D00_-ComponentModel.jpg"&gt;&lt;img height="128" width="515" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1030.0302-Imports-_2D00_-ComponentModel.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This import gives us the ability to write attributes for the properties. Attributes are those few lines of text seen below the property window of a control that describe the control / what it does etc. &amp;nbsp;Here is an example... &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/0250.0303-Attribute.jpg"&gt;&lt;img height="225" width="405" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/0250.0303-Attribute.jpg" border="0" alt="" /&gt;&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;Below is a description of each property - and above the actual property is the attribute which is the description that is seen under the property window. Note the left and right arrows at the left and right side of the actual description. These are called delimiters. Then there is a space followed by an underscore. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Other attributes are possible but we are only doing the description type. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;We will be making 5 properties, hiding 1, shadowing 1, and putting them all in the &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;Properties&lt;/span&gt;&amp;quot; region. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6710.0304-Region-Properties.jpg"&gt;&lt;img height="278" width="515" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6710.0304-Region-Properties.jpg" border="0" alt="" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;1 Corner Radius Property&lt;/span&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/7128.0305-CornerRadius.jpg"&gt;&lt;img height="491" width="1017" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/7128.0305-CornerRadius.jpg" border="0" alt="" /&gt;&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This property will change the radius of the border corners. If desired, 0 can be chosen in which case the border will be drawn using the first part of the &amp;quot;If...else....endif&amp;quot; statement in the DrawBorder function, else the second part of the statement will be fired should an integer of 1 or greater be used. There is also a small statement to check if a negative number has been attempted. I decided to have a maximum value of 36 for the radius and if a larger number is attempted, a message appears and the value is ignored. This limitation is for personal preference and can be removed if wanted. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;2 Border WIdth Property&lt;/span&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/2821.0306-BorderWidth.jpg"&gt;&lt;img height="335" width="677" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/2821.0306-BorderWidth.jpg" border="0" alt="" /&gt;&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="font-family:verdana,geneva;"&gt;Inside the set area is a small statement that checks if a negative value is given. If yes, then the entered value is ignored and a message box is shown. Here, I decided to have a maximum value of 10 - a message box appears disallowing the number, and the value returns to the previous number.&lt;/span&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;3 Border Color Property&lt;/span&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6622.0307-BorderColor.jpg"&gt;&lt;img height="267" width="482" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6622.0307-BorderColor.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="font-size:10pt;line-height:115%;"&gt;This is the property that gives the border its&amp;rsquo; color. The value is passed to the PenColor variable which in turn is passed to the pen (in the drawborder function).&lt;/span&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:10pt;line-height:115%;"&gt;4 Back Color Property&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4643.0308-BackColor.jpg"&gt;&lt;img src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4643.0308-BackColor.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;This is the property where the box gets the backcolor. This is also where the label gets its&amp;#39; backcolor. This property is set to &amp;quot;shadows&amp;quot; to ensure the &amp;quot;original&amp;quot; property is completely void. The &amp;quot;mybase&amp;quot; call ensures the panel stays transparent. Shadows forces a specific element to be used even if an identically named element exists.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;5 Hiding the Panel BorderStyle Property&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/0434.0309-BorderStyle.jpg"&gt;&lt;img height="313" width="489" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/0434.0309-BorderStyle.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;
&lt;p&gt;Notice the browsable attribute has been set to false so this property is hidden.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;6 Title Text Property&lt;/span&gt;&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4075.0310-TitleText.jpg"&gt;&lt;img height="339" width="574" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4075.0310-TitleText.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Every time the header text is changed, the label position function fires and recalculates the label length - this ensures it always stays in the chosen position.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;7 Title Position Property&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6622.0311-TitlePosition.jpg"&gt;&lt;img height="370" width="556" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6622.0311-TitlePosition.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;This is the last property and gives the option of which position to have the label - either left or centered. Easy Peasy....&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=722699" width="1" height="1"&gt;</description><enclosure url="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.72.26.99/Groupbox.rar" length="96187" type="application/octet-stream" /></item><item><title>Custom Groupbox - Part 2, Drawing the Border</title><link>http://vbcity.com/blogs/steve/archive/2016/10/17/custom-groupbox-part-2-drawing-the-border.aspx</link><pubDate>Mon, 17 Oct 2016 23:45:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:722698</guid><dc:creator>Steve</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;&lt;b&gt;&lt;span style="text-decoration:underline;"&gt;Part 2. Adding the border&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;There are various methods to draw a border, this is my chosen method. I decided to do it this way for 2 reasons. The first is because of the rounded corners and what happens when I decide to use square corners occasionally. To overcome this I divided the code up into 2 sections in an &amp;quot;If...Else...End If&amp;quot; statement thus...&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6138.0201-If-Statement.jpg"&gt;&lt;img height="96" width="314" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6138.0201-If-Statement.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The second reason is because of the label - there may be occasions when I want the groupbox to be transparent. When this happens, the border line would show through the label and look rather ugly. To overcome this, I added 2 waypoints to the rectangle (xll and xlr) which are on the left and right side of the label. The label has 2 groupbox positions - left or centre, and as it is moved, the border will automatically move with the label. Thus, the border does not completely circumvent the box, but stops at either side of the label, and the border line will not show behind the label.&lt;/p&gt;
&lt;p&gt;To draw an arc in GUI, it gets drawn inside its&amp;#39; own &amp;quot;imaginary&amp;quot; rectangle (I&amp;#39;m not talking about the panel perimeter and not the border). To do this, 6 things need to be known, the top left X and Y coordinates of the imaginary rectangle, the length and width of the rectangle sides, the start angle and the sweep (how much) of the angle. For our corners, each sweep (arc) is 90&amp;deg;.&lt;/p&gt;
&lt;p&gt;To make the explanation of the border a little easier, I drew a diagram of the border - it looks a little intimidating at first, but study it for a few moments and you can see what is happening......&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1016.0202-Diagram.jpg"&gt;&lt;img height="433" width="655" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1016.0202-Diagram.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Below is the sub to do the drawing of the border. Note that I&amp;#39;ve initialized the graphics path inside the sub. Further down the code I&amp;#39;ve initialized the pen (for drawing) and brush (for fill color). At the end of the sub, I&amp;#39;ve disposed of these items. Whenever you&amp;#39;ve finished with graphics, it&amp;#39;s important to dispose of them - they take up system resources, and can eventually slow down the system if left to accumulate. Near the beginning is a small &amp;quot;If..Else...EndIf&amp;quot; statement for the label. This is saying &amp;quot;&lt;i&gt;if there is no text then hide the label, so the XLL and XLR points must be in the same position&lt;/i&gt;&amp;quot;. The border is drawn via the groupbox paint event which we&amp;#39;ll come to in part 3.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6523.0203-DrawBorder.jpg"&gt;&lt;/a&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/7128.0203-DrawBorder-a.jpg"&gt;&lt;img height="708" width="925" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/7128.0203-DrawBorder-a.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A small but important detail with the above drawborder function - notice near the end the &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;e.FillRegion&lt;/span&gt;&amp;quot; occurs &lt;span style="text-decoration:underline;"&gt;BEFORE&lt;/span&gt; &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;e.DrawPath&lt;/span&gt;&amp;quot;. If the border is drawn before the filling, then it does not look like a solid / even line - it appears rather jagged and untidy. Doing it as shown will ensure the border keeps a clean and even look. Also, the pen alignment is set to &amp;quot;Inset&amp;quot; to ensure the border is always drawn on the &amp;quot;inside&amp;quot; of the path.&lt;/p&gt;
&lt;p&gt;The actual drawing is done via the &lt;span style="font-family:courier new,courier;"&gt;onpaint&lt;/span&gt; event which we will get to in part 3...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=722698" width="1" height="1"&gt;</description><enclosure url="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.72.26.98/Groupbox.rar" length="96187" type="application/octet-stream" /></item><item><title>Custom Groupbox - Part 1, basic setup</title><link>http://vbcity.com/blogs/steve/archive/2016/10/17/custom-groupbox-part-1-basic-setup.aspx</link><pubDate>Mon, 17 Oct 2016 22:57:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:722697</guid><dc:creator>Steve</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;Hi Everyone,
&lt;p&gt;This is a short series on designing a custom groupbox using a panel as the base. A groupbox is a container for grouping together other controls (buttons, textboxes etc) and the Visual Studio supplied groupbox doesn&amp;#39;t have the features I was wanting - border color / width, rounded border corners, moving the header label, so I decided to make my own. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;There are many other blogs out there that also describe how to customize a groupbox - often by inheriting and then overriding / adjusting the groupbox code. I decided on a different approach, and used a panel, a label and used graphics to do the border, and then added additional properties to alter the look of the box as needed. I&amp;#39;ve hidden the panel border property as this does not get used. A panel is another container, which is why I decided to use one as the basis for the groupbox. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Feel free to change the names of the various variables etc, but while following this, I suggest you use the same names to avoid confusion, then change them later. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;During the writing of the project, I added lots of comments in the code and have left them in here as an aid, so I haven&amp;#39;t explained too much in the blog, but left it up to the coding comments as explanations. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Finally, I thank Ged Mead (also of VBCity) who was kind enough to read through the code, and for suggesting a few alterations which I have included. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;I&amp;#39;ve attached&amp;nbsp;a zip file containing 2 folders. 1 contains the whole project: to use, just open it as normal. The 2nd file contains just the class file: to use this one, start a new project, and then go to &amp;quot;add an existing item&amp;quot; and add the file to the project. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Here are a few screenshots of the completed groupbox... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4075.0101-Wheat.jpg"&gt;&lt;img height="152" width="485" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4075.0101-Wheat.jpg" border="0" alt="" /&gt;&lt;/a&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; &lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6710.0102-Thistle.jpg"&gt;&lt;img height="156" width="545" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6710.0102-Thistle.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/7215.0103-Light-Blue.jpg"&gt;&lt;img height="147" width="459" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/7215.0103-Light-Blue.jpg" border="0" alt="" /&gt;&lt;/a&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; &lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/8863.0105-Tan.jpg"&gt;&lt;img height="131" width="600" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/8863.0105-Tan.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/8688.0104-Pale-Green.jpg"&gt;&lt;img height="154" width="531" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/8688.0104-Pale-Green.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In this first part we will.... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;1 Start a new project and add an empty class &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;2 Add all the various imports statements and inherit a panel &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;3 Make all the necessary declarations for this project &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;4 Set up the panel with default settings using &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="color:#0000ff;"&gt;Initialize Component&lt;/span&gt;&lt;/span&gt;&amp;quot; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;5 Add a label, which will become the header label, create the default settings and add the position subs &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;In parts 2 and 3 we&amp;#39;ll add the border, events and properties. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Here is a screenshot of the project - I&amp;#39;ve collapsed the regions so they can all be seen.... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1348.0106-The-Entire-Code-collapsed.jpg"&gt;&lt;img height="413" width="584" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1348.0106-The-Entire-Code-collapsed.jpg" border="0" alt="" /&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;1 Create a new project and Class&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;First, create a new project and give it a suitable name - I call mine &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;GroupboxProject&lt;/span&gt;&amp;quot;. Next, create a new class by going to project &amp;gt; new class. I call mine &amp;quot;CustomGroupbox&amp;quot;. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;2 Add Imports Statements and inherit a panel&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;At the top of the code page of the new class, and outside the code block, make the following imports statements. Inside the class, inherit a panel as shown in the screenshot... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/2553.0107-Imports-Statements.jpg"&gt;&lt;img height="156" width="585" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/2553.0107-Imports-Statements.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The first two (drawing) statements are needed to enable drawing of the border. The third statement will let us write attributes for the properties (covered in a later part).
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;3 Make all Declarations&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;I like to use regions in my code - they package up long sets of code and can be expanded / collapsed as needed to show / hide code, so make a region at the top of the code block and name it &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;Declarations&lt;/span&gt;&amp;quot; - this will hold all the variables we need for this project. It&amp;#39;s easier to write all the variables now instead of continually having to return to add them individually. Below is a screenshot showing the complete list - I&amp;#39;ve left in the comments I wrote during the project so you can read them. Inside the declarations region, add the following variables.... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4075.0108-Declarations.jpg"&gt;&lt;img height="358" width="625" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4075.0108-Declarations.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;4 Set up the Panel&lt;/span&gt;
&lt;p&gt;In solution explorer, right-click on the new class and select &amp;quot;view designer&amp;quot;. Now move to the property window and select the following defaults.... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="font-size:9.5pt;"&gt;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="font-size:x-small;"&gt;BorderStyle = None&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p&gt;&lt;span style="font-size:9.5pt;"&gt;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="font-size:x-small;"&gt;BackColor = Wheat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p&gt;&lt;span style="font-size:9.5pt;"&gt;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="font-size:x-small;"&gt;Font = Microsoft Sans Serif 9, Regular&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p&gt;&lt;span style="font-size:9.5pt;"&gt;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="font-size:x-small;"&gt;ForeColor = Black&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p&gt;&lt;span style="font-size:9.5pt;"&gt;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="font-size:x-small;"&gt;Size = (216, 146)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p&gt;&lt;span style="font-size:9.5pt;"&gt;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="font-size:x-small;"&gt;TabStop = True&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;These are all personal preferences, but make sure you change the borderstyle property to None - the border style is not used in this project.
&lt;p&gt;Now return to the class code page where you should see the &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="color:#0000ff;"&gt;Initialize Component&lt;/span&gt;&lt;/span&gt;&amp;quot; sub. In order for these defaults take effect, type &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="color:#0000ff;"&gt;Initialize Component&lt;/span&gt;&lt;/span&gt;&amp;quot; into &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="color:#0000ff;"&gt;Sub New&lt;/span&gt;&lt;/span&gt;&amp;quot;...... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/0250.0109-How-to-select-SubNew.jpg"&gt;&lt;img height="141" width="597" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/0250.0109-How-to-select-SubNew.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/2402.0109a-InitializeComponent.jpg"&gt;&lt;img height="318" width="640" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/2402.0109a-InitializeComponent.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1016.0109b-subnew-_2D00_-part-of-it.jpg"&gt;&lt;img height="122" width="651" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1016.0109b-subnew-_2D00_-part-of-it.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4062.0109b-subnew-_2D00_-part-of-it.jpg"&gt;&lt;/a&gt;
&lt;p&gt;Notice the 4 additional lines of code - the use of the &amp;quot;&lt;span style="color:#0000ff;"&gt;&lt;span style="font-family:courier new,courier;"&gt;Me.SetStyle&lt;/span&gt;&lt;/span&gt;&amp;quot; lines. These will ensure the control is painted as / when required. The doublebuffer will ensure flickering does not happen. The resize/redraw - well, it redraws when the panel is resized!! As per the MSDN documentation, when using doublebuffer, it&amp;#39;s best to also use allpainting and WMPaint &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;7 Add the Label&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Finally, in this section we add a label, make a default setup sub, and add the position subs - one for &amp;quot;left aligned&amp;quot; and one for &amp;quot;centre aligned&amp;quot;. An enumeration is also written - in the next part this will be used in the &amp;quot;TitlePosition&amp;quot; property. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;First is the setup sub... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/0755.0110-Label-Setup.jpg"&gt;&lt;img height="299" width="697" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/0755.0110-Label-Setup.jpg" border="0" alt="" /&gt;&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Notice we will use the panel font property for the label font. Also, the autosize is set to true - this is to allow for different text entries so the label will change its&amp;#39; size as necessary. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Here are the position subs for the label - add another region called &amp;quot;Label Positons&amp;quot; and insert the following subs and enumeration - a variable of the enumeration is also made..... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6710.0111-Label-Positions.jpg"&gt;&lt;img height="365" width="643" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/6710.0111-Label-Positions.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the label position code, the left X co-ordinate is LP.X which is either hard-coded as 36 or calculated to ensure the label remains in the center of the top border line. The Y co-ordinate is hard-coded to 2 in both functions to ensure it always remains below the top of the panel. I chose 36 for the left position only because the label looks good at that position - feel free to change if you prefer a position further left or right.
&lt;p&gt;Enumerations are useful things - they give multiple choices but for us, there are only 2 entries for the label position - left and center. Left is first and center is second. If you prefer to have them the other way, that&amp;#39;s ok. All it does is define which one will initially appear in the property window. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The label has already been declared (in the declarations region), so we need to initialize it; return to &amp;quot;Sub New&amp;quot; and add the label setup and &amp;quot;label left&amp;quot; position subs - note we also create a new LP (label position) point - the new label is added to the project via &amp;quot;&lt;span style="color:#0000ff;"&gt;&lt;span style="font-family:courier new,courier;"&gt;Me.Controls.Add(TitleLabel)&amp;quot;.&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The sub &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="color:#0000ff;"&gt;LabelLeft&lt;/span&gt;&lt;/span&gt;&amp;quot; is added here to give the label an initial position. Later, when the property has been added, the label will have the option of a centre position. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Now is a good time to add all the other items to &amp;quot;&lt;span style="font-family:courier new,courier;"&gt;&lt;span style="color:#0000ff;"&gt;Sub New&lt;/span&gt;&lt;/span&gt;&amp;quot; - here is a screenshot. We add the corner radius and border width - these 2 are used when drawing the border. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4064.0112-SubNew-code-_2D00_-all-of-it.jpg"&gt;&lt;img height="342" width="665" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/4064.0112-SubNew-code-_2D00_-all-of-it.jpg" border="0" alt="" /&gt;&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;As you know, when building a project, you need to press F5 first. If you decide to change the name of the class, it&amp;#39;s important to remember to delete any box on the form before making any changes and building, else an exception could happen. Also, ensure you change the name via the property window, then the class name will be changed everywhere.... &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1030.0113-ChangeClassName.jpg"&gt;&lt;img height="297" width="270" src="http://vbcity.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/steve/1030.0113-ChangeClassName.jpg" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;If you decide to change the variable names, it&amp;#39;s best to change them in the declarations region - that way the variable name will be changed throughout the project. &lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=722697" width="1" height="1"&gt;</description><enclosure url="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.72.26.97/Groupbox.rar" length="96187" type="application/octet-stream" /></item><item><title>Reading in a value from 1 table and writing that value to another table</title><link>http://vbcity.com/blogs/mikenewman/archive/2016/10/03/reading-in-a-value-from-1-table-and-writing-that-value-to-another-table.aspx</link><pubDate>Mon, 03 Oct 2016 09:53:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:722655</guid><dc:creator>Michael Newman</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Introduction&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;A VBCity member recently posted a request for help on how to take a value from one field in a database table and insert that value into another table.&amp;nbsp; Although the code is comparatively simple, I decided to create a small project including a database to show how it is done.&amp;nbsp; I have used SQL Server 2008 R2 Express, Visual Basic 2010 Express and SQL Server Management Studio 2008 Express (SSMS).&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I have broken some conventions in the code but this was done deliberately.&amp;nbsp; For example, I have not declared the variables at the top of the form where they would be accessible to all of the code as this explanation only has one sub that uses them.&amp;nbsp; I could then declare a variable immediately before its use making it easier to follow.&amp;nbsp; I have also opened and closed the connection to the database twice.&amp;nbsp; This way a newcomer to the subject can clearly see the way that data is retrieved from a database and then inserted into a different table in&amp;nbsp;the same database.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;Note 2&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Be aware that a database can only be accessed by your Project OR SSMS at one time i.e. you will have to save and close the project before looking at the database in SSMS and vice verse.&amp;nbsp; If you do not follow this procedure, you will probably get some very cryptic error messages.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Create your database&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Create a new database in SQL Server using SSMS and give it a name.&amp;nbsp; As you will see in images later in this text, mine was called &amp;rdquo;JEtling&amp;rdquo;&amp;nbsp; I prefer to do this in SSMS for development purposes as, if you crash your project code beyond redemption, you still have your database.&amp;nbsp; If you create a large database inside the project and the project crashes, it will be a little more than just an irritation re-creating everything.&lt;/p&gt;
&lt;p&gt;Open SSMS and login.&amp;nbsp; Start by adding a new database and create two tables, the first being &amp;ldquo;Load_Info&amp;rdquo; and the second &amp;ldquo;Fuel&amp;rdquo;.&amp;nbsp; Both have the same design structure as shown below:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/8838.Load_5F00_Info_2D00_Table_2D00_Design_5F00_13202C07.jpg"&gt;&lt;img height="261" width="738" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/8838.Load_5F00_Info_2D00_Table_2D00_Design_5F00_thumb_5F00_5CCF03F7.jpg" alt="Load_Info Table Design" border="0" style="background-image:none;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-width:0px;" title="Load_Info Table Design" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The ID column is just what it says.&amp;nbsp; A primary key, not null, is identity with increment 1.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Add data to the tables using table edit top 200 rows. Enter the data exactly as I have done.&amp;nbsp; The reason will become apparent when the application is run. See images for &amp;ldquo;Fuel&amp;rdquo; and &amp;ldquo;Load_Info&amp;rdquo; below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/6278.Fuel_2D00_Table_2D00_Data_5F00_22738E16.jpg"&gt;&lt;img height="248" width="744" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/4213.Fuel_2D00_Table_2D00_Data_5F00_thumb_5F00_61650EB1.jpg" alt="Fuel Table Data" border="0" style="background-image:none;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-width:0px;" title="Fuel Table Data" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/4520.LOad_5F00_Info_2D00_Table_2D00_Data_5F00_791C4617.jpg"&gt;&lt;img height="254" width="746" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/2061.LOad_5F00_Info_2D00_Table_2D00_Data_5F00_thumb_5F00_65FB196B.jpg" alt="LOad_Info Table Data" border="0" style="background-image:none;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-width:0px;" title="LOad_Info Table Data" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When the tables have been saved, close all windows to the right of the Object Explorer in SSMS and Detach the database from SQL Server or you will not be able to use it in the project.&lt;/p&gt;
&lt;p&gt;Create a new project in Visual Basic, give it a name and save the project.&lt;/p&gt;
&lt;p&gt;Add a split container to Form1&amp;rsquo;s design surface and set the Orientation property to Horizontal.&amp;nbsp; I also set the backcolour of the windows to different colours.&amp;nbsp; It just helps to see what is going on in each window.&lt;/p&gt;
&lt;p&gt;Create the datasets from the database. With every new project and connection string, as you add the first dataset, it asks if you want to copy the connection string into the project. See image below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/8738.Connection_2D00_string_2D00_options_5F00_449B73CF.jpg"&gt;&lt;img height="378" width="772" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/7774.Connection_2D00_string_2D00_options_5F00_thumb_5F00_4356DAF0.jpg" alt="Connection string options" border="0" style="background-image:none;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-width:0px;" title="Connection string options" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In this case, select &amp;ldquo;No&amp;rdquo; because we will be using the database as an external source. This gives you the ability to open the database in SSMS to see any changes that have been made. You can also change the data in the tables and see those changes when you run the project again. &lt;/p&gt;
&lt;p&gt;You will finally get to the part where you can choose tables for the datasets. Use the &amp;ldquo;Load_Info&amp;rdquo; table first and change the default dataset name name to &amp;ldquo;Load_InfoDataSet&amp;rdquo;. After a few more mouse clicks on &amp;ldquo;Next&amp;rdquo;, your dataset will appear in your Data Sources window. From the dataset, use the&amp;rdquo; Details&amp;rdquo; configuration, Drag and drop that onto the top section of the Split Container. You will now have the four columns as individual text boxes and a binding navigator at the top of the upper container.&lt;/p&gt;
&lt;p&gt;Do something similar for the &amp;ldquo;Fuel&amp;rdquo; table and call the dataset &amp;ldquo;FuelDataSet&amp;rdquo;. Drag and drop the Details onto the lower widow in the split container. Again you will get the four text boxes but this time, no binding navigator.&amp;nbsp; Add a binding navigator to the lower window of the split container. Select it and, in the Properties window, set its binding source to &amp;ldquo;FuelBindingSource&amp;rdquo;.&amp;nbsp; The last thing to add to the form is a timer which will automatically be assigned the name: &amp;ldquo;Timer1&amp;rdquo;. Leave the default values as they are. Enabled: False and Interval: 100. I had to introduce a timer to cater for any delay&amp;nbsp;between clicking on the arrows on the left of the &amp;ldquo;Load_Info Binding Navigator&amp;rdquo; and the record being visible in the text boxes. If you do not do this, as you step through the records, you actually land up reading data from the previous record. &lt;/p&gt;
&lt;p&gt;Apart from the two labels (Load_Info Data and Fuel Data) , your form should now look like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/7762.Design_2D00_view_5F00_28AA3ED7.jpg"&gt;&lt;img height="502" width="777" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/0728.Design_2D00_view_5F00_thumb_5F00_1207F090.jpg" alt="Design view" border="0" style="background-image:none;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border:0px;" title="Design view" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I always add a class to my data centric projects to deal with the connection string.&amp;nbsp; In so doing, you only have to write the connection string once and it is called into the code where it is needed in the entire project.&amp;nbsp; Create a class and name it &amp;ldquo;ConnectionString.vb&amp;rdquo;. Here is the code that I used:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;Imports&lt;/span&gt; System.Data.Sql
&lt;span style="color:#0000ff;"&gt;Imports&lt;/span&gt; System.Data.SqlClient

&lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ConnectionString&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Shared&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Function&lt;/span&gt; GetConnection() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlConnection

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; SQLCon &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = (&lt;span style="color:#a31515;"&gt;&amp;quot;Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\JEtling.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True&amp;quot;&lt;/span&gt;)

        &lt;span style="color:#0000ff;"&gt;Return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SqlConnection(SQLCon)

    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Function&lt;/span&gt;

&lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Class&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Note: You will have to change the connection string to cater for your set_up. To get your connection string, use the add data source routine until you see:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/1882.connection_2D00_string_5F00_4563B3EC.jpg"&gt;&lt;img height="593" width="771" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikenewman.metablogapi/3630.connection_2D00_string_5F00_thumb_5F00_0A9C0B16.jpg" alt="connection string" border="0" style="background-image:none;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-width:0px;" title="connection string" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Copy the code and then press the &amp;ldquo;Cancel&amp;rdquo; button.&amp;nbsp; Go back to your Class code and using paste, replace my connection string with the copied code.&lt;/p&gt;
&lt;p&gt;Go back to the design surface of Form1 and double click on the navigation arrows on the Load_Info Binding Navigator. Each time you double click it will take you to the code view. Put the following code into each click event:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;1
2&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;Timer1.Enabled = &lt;span style="color:#0000ff;"&gt;True&lt;/span&gt;
        Timer1.Start()&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The next step is to code &amp;ldquo;Timer1&amp;rdquo;. Double click on Timer1 which will open the code window.&amp;nbsp; Insert the following code into the &amp;ldquo;Timer1&amp;rdquo; tick event:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;1
2&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;  Timer1.Enabled = &lt;span style="color:#0000ff;"&gt;False&lt;/span&gt;
        RunQuery()&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Note that the RunQuery() command lets you know that it does not yet exist.&amp;nbsp; Here is the code for RunQuery:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt; &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; RunQuery()

        Timer1.Enabled = &lt;span style="color:#0000ff;"&gt;False&lt;/span&gt;

        Timer1.Stop()

        &lt;span style="color:#008000;"&gt;&amp;#39;Check for Pets_Load_Number&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; Pets_Load_NumberTextBox.Text = &lt;span style="color:#a31515;"&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
            MessageBox.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;There is no data in PETS_Load_NumberTextBox for Load_Info&amp;quot;&lt;/span&gt;)
            &lt;span style="color:#0000ff;"&gt;Exit&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;
        End &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;


        &lt;span style="color:#008000;"&gt;&amp;#39;Check for Fuel cost per mile&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; Cost_Per_MileTextBox.Text = &lt;span style="color:#a31515;"&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
            MessageBox.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;No cost per mile value&amp;quot;&lt;/span&gt;)
            &lt;span style="color:#0000ff;"&gt;Exit&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;
        End &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; X &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;  &lt;span style="color:#008000;"&gt;&amp;#39;Pets_Load_Number&lt;/span&gt;
        X = Pets_Load_NumberTextBox.Text

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; connection &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlConnection = ConnectionString.GetConnection
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; command &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlClient.SqlCommand = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SqlClient.SqlCommand()

        command.Connection = connection

        command.CommandText = &lt;span style="color:#a31515;"&gt;&amp;quot;SELECT Cost_Per_Mile FROM Load_Info WHERE PETS_Load_Number = &amp;#39;&amp;quot;&lt;/span&gt; &amp;amp; X &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;#39; &amp;quot;&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; A &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Double&lt;/span&gt;
        connection.Open()

        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; da1 &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlDataAdapter = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SqlDataAdapter(command)
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; dt1 &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DataTable(&lt;span style="color:#a31515;"&gt;&amp;quot;cpm&amp;quot;&lt;/span&gt;)
            da1.Fill(dt1)

            connection.Close()


            A = dt1(0)(0)  &lt;span style="color:#008000;"&gt;&amp;#39;Gets Cost_Per_Mile from the Load_Info database table&lt;/span&gt;

            &lt;span style="color:#008000;"&gt;&amp;#39;REMEMBER the dt.Rows column is one cell only as that is all that has been called for in the SELECT statement&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;&amp;#39;This is slightly different in the next section which puts &amp;quot;A&amp;quot; into the &amp;quot;Fuel&amp;quot; table&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MsgBox(ex.Message)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
     

        &lt;span style="color:#008000;"&gt;&amp;#39;&amp;#39;Now we deal with updating the Fuel database table&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; Fuel2DataSet &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DataSet()

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; cmdBuilder &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlCommandBuilder

        command.Connection = connection

        connection.Open()

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; da2 &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlDataAdapter

        da2 = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SqlDataAdapter(&lt;span style="color:#a31515;"&gt;&amp;quot;SELECT ID, Cost_Per_Mile FROM Fuel WHERE Pets_Load_Number =  &amp;#39;&amp;quot;&lt;/span&gt; &amp;amp; X &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;#39; &amp;quot;&lt;/span&gt;, connection)


        cmdBuilder = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SqlCommandBuilder(da2)


        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; dt2 &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DataTable(&lt;span style="color:#a31515;"&gt;&amp;quot;fuel&amp;quot;&lt;/span&gt;)

        da2.Fill(Fuel2DataSet, &lt;span style="color:#a31515;"&gt;&amp;quot;fuel&amp;quot;&lt;/span&gt;)
        da2.Fill(dt2)

        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; dt2.Rows.Count = 0 &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;

            MessageBox.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;Do you have a PETS_Load_Number in the Fuel table?&amp;quot;&lt;/span&gt;)

            &lt;span style="color:#0000ff;"&gt;Exit&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;

        End &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; dt2.Rows.Count &amp;gt; 0 &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; dt2.Rows(0)(1) &lt;span style="color:#0000ff;"&gt;Is&lt;/span&gt; DBNull.Value &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;

                Fuel2DataSet.Tables(&lt;span style="color:#a31515;"&gt;&amp;quot;Fuel&amp;quot;&lt;/span&gt;).Rows(0)(&lt;span style="color:#a31515;"&gt;&amp;quot;Cost_Per_Mile&amp;quot;&lt;/span&gt;) = A

                da2.Update(Fuel2DataSet, &lt;span style="color:#a31515;"&gt;&amp;quot;Fuel&amp;quot;&lt;/span&gt;)

            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.FuelTableAdapter.Fill(&lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.FuelDataSet.Fuel)

        connection.Close()

    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;One last thing is to add&amp;nbsp; RunQuery just before &amp;ldquo;End Sub&amp;rdquo; in the Load_InfoBindingNavigatorSaveItem_Click code and in the Form_Load code.&lt;/p&gt;
&lt;p&gt;Here is the full code for Form1:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#008000;"&gt;&amp;#39;Imports System&lt;/span&gt;
&lt;span style="color:#008000;"&gt;&amp;#39;Imports System.Data&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;Imports&lt;/span&gt; System.Data.SqlClient
&lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Form1&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; Load_InfoBindingNavigatorSaveItem_Click(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.Object, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:#0000ff;"&gt;Handles&lt;/span&gt; Load_InfoBindingNavigatorSaveItem.Click
        &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Validate()
        &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Load_InfoBindingSource.EndEdit()
        &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.TableAdapterManager.UpdateAll(&lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Load_InfoDataSet)

        RunQuery()  &lt;span style="color:#008000;"&gt;&amp;#39;This will run the RunQuery code every time the data is saved&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; Form1_Load(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.Object, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:#0000ff;"&gt;Handles&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;MyBase&lt;/span&gt;.Load
        &lt;span style="color:#008000;"&gt;&amp;#39;TODO: This line of code loads data into the &amp;#39;FuelDataSet.Fuel&amp;#39; table. You can move, or remove it, as needed.&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.FuelTableAdapter.Fill(&lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.FuelDataSet.Fuel)
        &lt;span style="color:#008000;"&gt;&amp;#39;TODO: This line of code loads data into the &amp;#39;Load_InfoDataSet.Load_Info&amp;#39; table. You can move, or remove it, as needed.&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Load_InfoTableAdapter.Fill(&lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Load_InfoDataSet.Load_Info)

        RunQuery()   &lt;span style="color:#008000;"&gt;&amp;#39;This will run the RunQuery code every time the form is loaded&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; BindingNavigatorMoveFirstItem_Click(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.Object, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:#0000ff;"&gt;Handles&lt;/span&gt; BindingNavigatorMoveFirstItem.Click
        Timer1.Enabled = &lt;span style="color:#0000ff;"&gt;True&lt;/span&gt;
        Timer1.Start()
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; BindingNavigatorMovePreviousItem_Click(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.Object, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:#0000ff;"&gt;Handles&lt;/span&gt; BindingNavigatorMovePreviousItem.Click
        Timer1.Enabled = &lt;span style="color:#0000ff;"&gt;True&lt;/span&gt;
        Timer1.Start()
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; BindingNavigatorMoveNextItem_Click(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.Object, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:#0000ff;"&gt;Handles&lt;/span&gt; BindingNavigatorMoveNextItem.Click
        Timer1.Enabled = &lt;span style="color:#0000ff;"&gt;True&lt;/span&gt;
        Timer1.Start()
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; BindingNavigatorMoveLastItem_Click(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.Object, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:#0000ff;"&gt;Handles&lt;/span&gt; BindingNavigatorMoveLastItem.Click
        Timer1.Enabled = &lt;span style="color:#0000ff;"&gt;True&lt;/span&gt;
        Timer1.Start()
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; Timer1_Tick(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.Object, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:#0000ff;"&gt;Handles&lt;/span&gt; Timer1.Tick
        Timer1.Enabled = &lt;span style="color:#0000ff;"&gt;False&lt;/span&gt;
        RunQuery()
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; RunQuery()

        Timer1.Enabled = &lt;span style="color:#0000ff;"&gt;False&lt;/span&gt;

        Timer1.Stop()

        &lt;span style="color:#008000;"&gt;&amp;#39;Check for Pets_Load_Number&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; Pets_Load_NumberTextBox.Text = &lt;span style="color:#a31515;"&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
            MessageBox.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;There is no data in PETS_Load_NumberTextBox for Load_Info&amp;quot;&lt;/span&gt;)
            &lt;span style="color:#0000ff;"&gt;Exit&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;
        End &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;


        &lt;span style="color:#008000;"&gt;&amp;#39;Check for Fuel cost per mile&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; Cost_Per_MileTextBox.Text = &lt;span style="color:#a31515;"&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
            MessageBox.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;No cost per mile value&amp;quot;&lt;/span&gt;)
            &lt;span style="color:#0000ff;"&gt;Exit&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;
        End &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; X &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;  &lt;span style="color:#008000;"&gt;&amp;#39;Pets_Load_Number&lt;/span&gt;
        X = Pets_Load_NumberTextBox.Text

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; connection &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlConnection = ConnectionString.GetConnection
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; command &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlClient.SqlCommand = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SqlClient.SqlCommand()

        command.Connection = connection

        command.CommandText = &lt;span style="color:#a31515;"&gt;&amp;quot;SELECT Cost_Per_Mile FROM Load_Info WHERE PETS_Load_Number = &amp;#39;&amp;quot;&lt;/span&gt; &amp;amp; X &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;#39; &amp;quot;&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; A &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Double&lt;/span&gt;
        connection.Open()

        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; da1 &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlDataAdapter = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SqlDataAdapter(command)
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; dt1 &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DataTable(&lt;span style="color:#a31515;"&gt;&amp;quot;cpm&amp;quot;&lt;/span&gt;)
            da1.Fill(dt1)

            connection.Close()


            A = dt1(0)(0)  &lt;span style="color:#008000;"&gt;&amp;#39;Gets Cost_Per_Mile from the Load_Info database table&lt;/span&gt;

            &lt;span style="color:#008000;"&gt;&amp;#39;REMEMBER the dt.Rows column is one cell only as that is all that has been called for in the SELECT statement&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;&amp;#39;This is slightly different in the next section which puts &amp;quot;A&amp;quot; into the &amp;quot;Fuel&amp;quot; table&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MsgBox(ex.Message)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
     

        &lt;span style="color:#008000;"&gt;&amp;#39;&amp;#39;Now we deal with updating the Fuel database table&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; Fuel2DataSet &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DataSet()

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; cmdBuilder &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlCommandBuilder

        command.Connection = connection

        connection.Open()

        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; da2 &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; SqlDataAdapter

        da2 = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SqlDataAdapter(&lt;span style="color:#a31515;"&gt;&amp;quot;SELECT ID, Cost_Per_Mile FROM Fuel WHERE Pets_Load_Number =  &amp;#39;&amp;quot;&lt;/span&gt; &amp;amp; X &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;#39; &amp;quot;&lt;/span&gt;, connection)


        cmdBuilder = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SqlCommandBuilder(da2)


        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; dt2 &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DataTable(&lt;span style="color:#a31515;"&gt;&amp;quot;fuel&amp;quot;&lt;/span&gt;)

        da2.Fill(Fuel2DataSet, &lt;span style="color:#a31515;"&gt;&amp;quot;fuel&amp;quot;&lt;/span&gt;)
        da2.Fill(dt2)

        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; dt2.Rows.Count = 0 &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;

            MessageBox.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;Do you have a PETS_Load_Number in the Fuel table?&amp;quot;&lt;/span&gt;)

            &lt;span style="color:#0000ff;"&gt;Exit&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;

        End &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; dt2.Rows.Count &amp;gt; 0 &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; dt2.Rows(0)(1) &lt;span style="color:#0000ff;"&gt;Is&lt;/span&gt; DBNull.Value &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;

                Fuel2DataSet.Tables(&lt;span style="color:#a31515;"&gt;&amp;quot;Fuel&amp;quot;&lt;/span&gt;).Rows(0)(&lt;span style="color:#a31515;"&gt;&amp;quot;Cost_Per_Mile&amp;quot;&lt;/span&gt;) = A

                da2.Update(Fuel2DataSet, &lt;span style="color:#a31515;"&gt;&amp;quot;Fuel&amp;quot;&lt;/span&gt;)

            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.FuelTableAdapter.Fill(&lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.FuelDataSet.Fuel)

        connection.Close()

    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;

&lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Class&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Do not forget to have the Imports System.Data.SqlClient at the top of the form.&lt;/p&gt;
&lt;p&gt;Before you can run this project with any success, you will have to select &amp;ldquo;Build&amp;rdquo; from the menu strip at the top of the IDE and then click &amp;ldquo;Build &amp;lt;name of project&amp;gt;&amp;rdquo;. &lt;/p&gt;
&lt;p&gt;Press F5 to run the project.&amp;nbsp; You will have a message box pop to let you know that the Cost_Per_Mile textbox is empty in the Load_Info window.&amp;nbsp; Click OK on the message box.&amp;nbsp; Enter a value into the Load_Info Cost_Per_Mile textbox, say 0.50, and click the save button on the Load_Info binding navigator.&amp;nbsp; As you do this, the same amount will be visible in the Cost_Per_Mile textbox in the Fuel window of the split container.&lt;/p&gt;
&lt;p&gt;If you change the value in the Load_Info Cost_Per_Mile textbox and save it again, The value in the Fuel Cost_Per_Mile textbox does not change.&amp;nbsp; This was done to prevent inadvertent over writing of data into the Fuel table.&amp;nbsp; Line 130 in the code above deals with this.&amp;nbsp; It will only allow data to be&amp;nbsp;entered into the Fuel table Cost_Per_Mile&amp;nbsp;field if it is NULL in the database.&amp;nbsp; If you want it to be over written every time, remove lines 130 and 136. To be 100 per cent correct, I also should have used &amp;ldquo;Exit sub&amp;rdquo;&amp;nbsp; at line 135 if this block was production code but in this case all it allows is the FuelTableAdapter in line 139 to be filled again.&amp;nbsp; In this explanation of how things work, it is better to have lines 130 and 136 in the code or you would not see that the Fuel table had not been updated when the Cost_Per_Mile textbox value was changed in the Load_Info window&amp;nbsp;and saved.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=722655" width="1" height="1"&gt;</description></item><item><title>WPF ListBox SelectedItem Colors</title><link>http://vbcity.com/blogs/xtab/archive/2016/04/27/wpf-listbox-selecteditem-colors.aspx</link><pubDate>Wed, 27 Apr 2016 13:52:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721301</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I wrote &lt;a href="http://vbcity.com/blogs/xtab/archive/2009/06/28/background-color-for-wpf-listbox-selected-item.aspx"&gt;a post a few years ago&lt;/a&gt; that shows quite a long-winded way of changing the Background color of the currently selected item in a WPF ListBox.&amp;#160; Although I think that approach is still valid, I now use a slightly less verbose way.&amp;#160; As well as showing you&amp;#160; that, I also wanted to include something new:&amp;#160; How you can change the default Background color of the currently selected item &lt;em&gt;once it has lost focus.&amp;#160;&amp;#160; &lt;/em&gt;The examples shown here are good for .Net Framework 4.6 onwards but not for all earlier versions because the name of the System brush(es) have changed somewhere along the way.&amp;#160; You can of course research for the earlier version Brush names and use this same approach.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Create The UI      &lt;br /&gt;&lt;/u&gt;Create a DataTemplate for the ListBox – maybe something like this one:&lt;/p&gt;   &lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt; &amp;lt;ListBox.ItemTemplate&amp;gt;
                &amp;lt;DataTemplate&amp;gt;
                    &amp;lt;Border  BorderThickness=&lt;span style="color:#a31515;"&gt;&amp;quot;2&amp;quot;&lt;/span&gt; BorderBrush=&lt;span style="color:#a31515;"&gt;&amp;quot;Navy&amp;quot;&lt;/span&gt; HorizontalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt;  VerticalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; 
                                   Height=&lt;span style="color:#a31515;"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt; Width=&lt;span style="color:#a31515;"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt; MinWidth=&lt;span style="color:#a31515;"&gt;&amp;quot;134&amp;quot;&lt;/span&gt; CornerRadius=&lt;span style="color:#a31515;"&gt;&amp;quot;5&amp;quot;&lt;/span&gt;
                                  Margin=&lt;span style="color:#a31515;"&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &amp;gt;
                        &amp;lt;TextBlock HorizontalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; Height=&lt;span style="color:#a31515;"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt; TextWrapping=&lt;span style="color:#a31515;"&gt;&amp;quot;Wrap&amp;quot;&lt;/span&gt; 
                                    Text=&lt;span style="color:#a31515;"&gt;&amp;quot;{Binding}&amp;quot;&lt;/span&gt; 
                                    VerticalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; MinWidth=&lt;span style="color:#a31515;"&gt;&amp;quot;90&amp;quot;&lt;/span&gt; FontSize=&lt;span style="color:#a31515;"&gt;&amp;quot;17&amp;quot;&lt;/span&gt; FontWeight=&lt;span style="color:#a31515;"&gt;&amp;quot;DemiBold&amp;quot;&lt;/span&gt; Foreground=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF09099B&amp;quot;&lt;/span&gt; 
                                    Margin=&lt;span style="color:#a31515;"&gt;&amp;quot;5,3&amp;quot;&lt;/span&gt;  TextAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; /&amp;gt;
                    &amp;lt;/Border&amp;gt;
                &amp;lt;/DataTemplate&amp;gt;
            &amp;lt;/ListBox.ItemTemplate&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This DataTemplate goes inside the Xaml for the ListBox.&amp;#160; (I’ll post all the Xaml for the ListBox shortly so you can see how this works).&lt;/p&gt;

&lt;p&gt;Then for example’s sake I’ll create some data and populate the ListBox at run time:&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="font-weight:bold;color:#228899;"&gt;Class&lt;/span&gt; &lt;span style="font-weight:bold;color:#ee99ee;"&gt;MainWindow&lt;/span&gt;
    &lt;span style="font-weight:bold;color:#228899;"&gt;Dim&lt;/span&gt; MyItems &lt;span style="font-weight:bold;color:#000000;"&gt;As&lt;/span&gt; &lt;span style="font-weight:bold;color:#228899;"&gt;New&lt;/span&gt; List(&lt;span style="font-weight:bold;color:#228899;"&gt;Of&lt;/span&gt; &lt;span style="font-weight:bold;color:#6666ff;"&gt;String&lt;/span&gt;)

    &lt;span style="font-weight:bold;color:#228899;"&gt;Sub&lt;/span&gt; &lt;span style="font-weight:bold;color:#55eedd;"&gt;New&lt;/span&gt;()

        &lt;span style="color:#666666;font-style:italic;"&gt;&amp;#39; This call is required by the designer.&lt;/span&gt;
        InitializeComponent()

        &lt;span style="color:#666666;font-style:italic;"&gt;&amp;#39;  Create some data&lt;/span&gt;
        &lt;span style="font-weight:bold;color:#228899;"&gt;With&lt;/span&gt; MyItems
            &lt;span style="font-weight:bold;color:#228899;"&gt;For&lt;/span&gt; i &lt;span style="font-weight:bold;color:#000000;"&gt;As&lt;/span&gt; &lt;span style="font-weight:bold;color:#6666ff;"&gt;Integer&lt;/span&gt; &lt;span style="color:#333333;"&gt;=&lt;/span&gt; &lt;span style="font-weight:bold;color:#6666ff;"&gt;1&lt;/span&gt; &lt;span style="font-weight:bold;color:#228899;"&gt;To&lt;/span&gt; &lt;span style="font-weight:bold;color:#6666ff;"&gt;10&lt;/span&gt;
                .Add(&lt;span style="background-color:#e0e0ff;"&gt;&amp;quot;Item #&amp;quot;&lt;/span&gt; &lt;span style="color:#333333;"&gt;&amp;amp;&lt;/span&gt; i.ToString)
            &lt;span style="font-weight:bold;color:#228899;"&gt;Next&lt;/span&gt;
        &lt;span style="font-weight:bold;color:#228899;"&gt;End&lt;/span&gt; &lt;span style="font-weight:bold;color:#228899;"&gt;With&lt;/span&gt;

        &lt;span style="color:#666666;font-style:italic;"&gt;&amp;#39;   Assign as data source&lt;/span&gt;
        DemoList.ItemsSource &lt;span style="color:#333333;"&gt;=&lt;/span&gt; MyItems

    &lt;span style="font-weight:bold;color:#228899;"&gt;End&lt;/span&gt; &lt;span style="font-weight:bold;color:#228899;"&gt;Sub&lt;/span&gt;
&lt;span style="font-weight:bold;color:#228899;"&gt;End&lt;/span&gt; &lt;span style="font-weight:bold;color:#228899;"&gt;Class&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;At this point, the Window will look like this at run time:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4137.WPF_2D00_LIstBox_2D00_SelectedItem_2D00_Color_2D00_1_5F00_3BB16572.png"&gt;&lt;img title="WPF LIstBox SelectedItem Color 1" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="WPF LIstBox SelectedItem Color 1" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/1401.WPF_2D00_LIstBox_2D00_SelectedItem_2D00_Color_2D00_1_5F00_thumb_5F00_18A0F402.png" width="381" height="285" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ll see that I’ve also placed a button in the Window.&amp;#160; This is just so I can remove focus from the ListBox for the demo.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Currently Selected Item (When Focused) 
    &lt;br /&gt;&lt;/u&gt;What the ListBox needs is a Style that’s applied whenever an item is selected.&amp;#160; This Style has to have two distinct parts – one for when the ListBox is focused and one for when it’s not.&lt;/p&gt;

&lt;p&gt;The Xaml for the focused state looks like this:&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;        &amp;lt;Grid.Resources&amp;gt;
            &amp;lt;Style x:Key=&lt;span style="color:#a31515;"&gt;&amp;quot;ListBoxSelectedItemsStyle&amp;quot;&lt;/span&gt; TargetType=&lt;span style="color:#a31515;"&gt;&amp;quot;{x:Type ListBoxItem}&amp;quot;&lt;/span&gt;&amp;gt;
                &amp;lt;Style.Resources&amp;gt;
                    &amp;lt;LinearGradientBrush x:Key=&lt;span style="color:#a31515;"&gt;&amp;quot;{x:Static SystemColors.HighlightBrushKey}&amp;quot;&lt;/span&gt;
                            EndPoint=&lt;span style="color:#a31515;"&gt;&amp;quot;1,0.5&amp;quot;&lt;/span&gt; StartPoint=&lt;span style="color:#a31515;"&gt;&amp;quot;0,0.25&amp;quot;&lt;/span&gt; &amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF808FF0&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FFFFFFFF&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.865&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FFE1F0E8&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.394&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF99A9F0&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.663&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF5B6EE6&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.962&amp;quot;&lt;/span&gt;/&amp;gt;
                    &amp;lt;/LinearGradientBrush&amp;gt;
                 
                &amp;lt;/Style.Resources&amp;gt;
            &amp;lt;/Style&amp;gt;

        &amp;lt;/Grid.Resources&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I’ve opted to use a gradient brush, but of course you can use a solid color brush instead.&amp;#160; The important thing to notice is the&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; x:Key=&lt;span style="color:#a31515;"&gt;&amp;quot;{x:Static SystemColors.HighlightBrushKey}&amp;quot;&lt;/span&gt; &lt;/p&gt;

&lt;p&gt;in the fourth line which overrides the System’s built-in HighlightBrushKey for items in this Grid.&amp;#160; (I’ve set it at the Grid level so you can have the style applied to multiple ListBoxes in one Grid.&amp;#160; If you prefer, you can use ListBox.Resources level instead).&lt;/p&gt;

&lt;p&gt;As promised above, here’s the full code for the ListBox, including that earlier DataTemplate:&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;        &amp;lt;ListBox x:Name=&lt;span style="color:#a31515;"&gt;&amp;quot;DemoList&amp;quot;&lt;/span&gt;  HorizontalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt;
                     ItemContainerStyle=&lt;span style="color:#a31515;"&gt;&amp;quot;{StaticResource ListBoxSelectedItemsStyle}&amp;quot;&lt;/span&gt; &amp;gt;
            &amp;lt;ListBox.ItemTemplate&amp;gt;
                &amp;lt;DataTemplate&amp;gt;
                    &amp;lt;Border  BorderThickness=&lt;span style="color:#a31515;"&gt;&amp;quot;2&amp;quot;&lt;/span&gt; BorderBrush=&lt;span style="color:#a31515;"&gt;&amp;quot;Navy&amp;quot;&lt;/span&gt; HorizontalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt;  VerticalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; 
                                   Height=&lt;span style="color:#a31515;"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt; Width=&lt;span style="color:#a31515;"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt; MinWidth=&lt;span style="color:#a31515;"&gt;&amp;quot;134&amp;quot;&lt;/span&gt; CornerRadius=&lt;span style="color:#a31515;"&gt;&amp;quot;5&amp;quot;&lt;/span&gt;
                                  Margin=&lt;span style="color:#a31515;"&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &amp;gt;
                        &amp;lt;TextBlock HorizontalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; Height=&lt;span style="color:#a31515;"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt; TextWrapping=&lt;span style="color:#a31515;"&gt;&amp;quot;Wrap&amp;quot;&lt;/span&gt; 
                                    Text=&lt;span style="color:#a31515;"&gt;&amp;quot;{Binding}&amp;quot;&lt;/span&gt; 
                                    VerticalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; MinWidth=&lt;span style="color:#a31515;"&gt;&amp;quot;90&amp;quot;&lt;/span&gt; FontSize=&lt;span style="color:#a31515;"&gt;&amp;quot;17&amp;quot;&lt;/span&gt; FontWeight=&lt;span style="color:#a31515;"&gt;&amp;quot;DemiBold&amp;quot;&lt;/span&gt; Foreground=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF09099B&amp;quot;&lt;/span&gt; 
                                    Margin=&lt;span style="color:#a31515;"&gt;&amp;quot;5,3&amp;quot;&lt;/span&gt;  TextAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; /&amp;gt;
                    &amp;lt;/Border&amp;gt;
                &amp;lt;/DataTemplate&amp;gt;
            &amp;lt;/ListBox.ItemTemplate&amp;gt;
        &amp;lt;/ListBox&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The important item this time is in the second line: 
  &lt;br /&gt;

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ItemContainerStyle=&lt;span style="color:#a31515;"&gt;&amp;quot;{StaticResource ListBoxSelectedItemsStyle}&amp;quot;&lt;/span&gt; &lt;/p&gt;

&lt;p&gt;The ItemContainerStyle property controls the look of the individual items in the ListBox.&lt;/p&gt;

&lt;p&gt;Now when this application runs and a ListBox item is selected, we get:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0815.ListBoxItem_2D00_Selected_5F00_2F139289.png"&gt;&lt;img title="ListBoxItem Selected" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="ListBoxItem Selected" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6281.ListBoxItem_2D00_Selected_5F00_thumb_5F00_117191BD.png" width="447" height="314" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;All good so far.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Currently Selected Item (When Focus Lost) 
    &lt;br /&gt;&lt;/u&gt;The trick here is to change another System Brush, this time the InactiveSelectionHighlightBrushKey.&amp;#160; And again the place to put your version of the Xaml for the replacement brush is inside that Style I created earlier.&amp;#160; So here’s the updated version:&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;table&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td&gt;
          &lt;pre style="margin:0px;line-height:125%;"&gt;            &amp;lt;Style x:Key=&lt;span style="color:#a31515;"&gt;&amp;quot;ListBoxSelectedItemsStyle&amp;quot;&lt;/span&gt; TargetType=&lt;span style="color:#a31515;"&gt;&amp;quot;{x:Type ListBoxItem}&amp;quot;&lt;/span&gt;&amp;gt;
                &amp;lt;Style.Resources&amp;gt;
                    &amp;lt;LinearGradientBrush x:Key=&lt;span style="color:#a31515;"&gt;&amp;quot;{x:Static SystemColors.HighlightBrushKey}&amp;quot;&lt;/span&gt;
                            EndPoint=&lt;span style="color:#a31515;"&gt;&amp;quot;1,0.5&amp;quot;&lt;/span&gt; StartPoint=&lt;span style="color:#a31515;"&gt;&amp;quot;0,0.25&amp;quot;&lt;/span&gt; &amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF808FF0&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FFFFFFFF&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.865&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FFE1F0E8&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.394&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF99A9F0&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.663&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF5B6EE6&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.962&amp;quot;&lt;/span&gt;/&amp;gt;
                    &amp;lt;/LinearGradientBrush&amp;gt;
                    &amp;lt;LinearGradientBrush x:Key=&lt;span style="color:#a31515;"&gt;&amp;quot;{x:Static SystemColors.InactiveSelectionHighlightBrushKey}&amp;quot;&lt;/span&gt;
                            EndPoint=&lt;span style="color:#a31515;"&gt;&amp;quot;0.25,1&amp;quot;&lt;/span&gt; StartPoint=&lt;span style="color:#a31515;"&gt;&amp;quot;0,0.5&amp;quot;&lt;/span&gt; &amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FFD9DCEC&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FFFFFFFF&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.865&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#B2C6CCF0&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.394&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FFD8DDF1&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.663&amp;quot;&lt;/span&gt;/&amp;gt;
                        &amp;lt;GradientStop Color=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF888FF0&amp;quot;&lt;/span&gt; Offset=&lt;span style="color:#a31515;"&gt;&amp;quot;0.962&amp;quot;&lt;/span&gt;/&amp;gt;
                    &amp;lt;/LinearGradientBrush&amp;gt;
                &amp;lt;/Style.Resources&amp;gt;
            &amp;lt;/Style&amp;gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;I’ve included line numbers so that I can highlight the block of Xaml from lines 11 to 18 as being the ones that have been added.&lt;/p&gt;

&lt;p&gt;It’s a similar approach and again I’ve gone for a gradient look.&amp;#160; Line 11 is the one you should home in on because that’s the one that tells the System that you want to use a different color for non-focused selected items in ListBoxes in this grid.&lt;/p&gt;

&lt;p&gt;Now when the application runs, if the user selects an item and then moves on to the button to lose the focus, you get:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6366.ListBox_2D00_Lost_2D00_Focus_5F00_34DE1055.png"&gt;&lt;img title="ListBox Lost Focus" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="ListBox Lost Focus" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/1882.ListBox_2D00_Lost_2D00_Focus_5F00_thumb_5F00_63741937.png" width="420" height="300" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reselecting that item or another one will cause the HighlightBrushKey gradient to be used (i.e. the one I first described).&lt;/p&gt;

&lt;p&gt;And that’s pretty much all there is to it.&amp;#160; I find these kind of gradients work quite well and if you have several ListBoxes that the user can select items from consecutively you get a kind of breadcrumbs look that – I think anyway – looks OK:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4135.Breadcrumbs_5F00_6012818F.png"&gt;&lt;img title="Breadcrumbs" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Breadcrumbs" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8738.Breadcrumbs_5F00_thumb_5F00_0F34475A.png" width="470" height="256" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721301" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic.NET/default.aspx">Visual Basic.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Style/default.aspx">Style</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic/default.aspx">Visual Basic</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic+WPF/default.aspx">Visual Basic WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/ListBox/default.aspx">ListBox</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Focus/default.aspx">Focus</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/SelectedItem/default.aspx">SelectedItem</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/ListBoxItem/default.aspx">ListBoxItem</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/InactiveSelectionHighlightBrushKey/default.aspx">InactiveSelectionHighlightBrushKey</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/System+Colors/default.aspx">System Colors</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/SelectedItem+Color/default.aspx">SelectedItem Color</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/SystemColors.HighlightBrushKey/default.aspx">SystemColors.HighlightBrushKey</category></item><item><title>How To Upload and Download Files with FTP from a VB.NET Application</title><link>http://vbcity.com/blogs/xtab/archive/2016/04/13/how-to-upload-and-download-files-with-ftp-from-a-vb-net-application.aspx</link><pubDate>Wed, 13 Apr 2016 18:49:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721280</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In these days of Azure and The Cloud this post might seem to be a bit out of date, but if you want to access files on your web hosting service then here’s how you can do it with FTP.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Uploading      &lt;br /&gt;&lt;/u&gt;Here’s a method that takes the four key pieces of information:&lt;/p&gt;  &lt;p&gt;The name of the file to be uploaded    &lt;br /&gt;The web address of the ftp server on your web host     &lt;br /&gt;Your FTP username on your web host     &lt;br /&gt;The FTP Password&lt;/p&gt;  &lt;p&gt;I’ve been surprised at how many examples only cover cases where Anonymous FTP is allowed.&amp;#160; I’m not sure that’s very realistic in a scenario where you are uploading your personal files.&lt;/p&gt;  &lt;p&gt;Anyway, here’s the method that takes those four parameters:&lt;/p&gt;   &lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;   &lt;table&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td&gt;           &lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td&gt;
          &lt;pre style="margin:0px;line-height:125%;"&gt;    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; FtpUploadFile(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; filetoupload &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; ftpuri &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; ftpusername &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; ftppassword &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;)
        &lt;span style="color:#008000;"&gt;&amp;#39; Create a web request that will be used to talk with the server and set the request method to upload a file by ftp.&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; ftpRequest &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; FtpWebRequest = &lt;span style="color:#0000ff;"&gt;CType&lt;/span&gt;(WebRequest.Create(ftpuri), FtpWebRequest)

        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile

            &lt;span style="color:#008000;"&gt;&amp;#39; Confirm the Network credentials based on the user name and password passed in.&lt;/span&gt;
            ftpRequest.Credentials = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; NetworkCredential(ftpusername, ftppassword)

            &lt;span style="color:#008000;"&gt;&amp;#39; Read into a Byte array the contents of the file to be uploaded &lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; bytes() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Byte&lt;/span&gt; = System.IO.File.ReadAllBytes(filetoupload)

            &lt;span style="color:#008000;"&gt;&amp;#39; Transfer the byte array contents into the request stream, write and then close when done.&lt;/span&gt;
            ftpRequest.ContentLength = bytes.Length
            &lt;span style="color:#0000ff;"&gt;Using&lt;/span&gt; UploadStream &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Stream = ftpRequest.GetRequestStream()
                UploadStream.Write(bytes, 0, bytes.Length)
                UploadStream.Close()
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Using&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MessageBox.Show(ex.Message)
            &lt;span style="color:#0000ff;"&gt;Exit&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;
        End &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;

        MessageBox.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;Process Complete&amp;quot;&lt;/span&gt;)
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;As you can see from the comments, a WebRequest is created and the FTP UploadFile method is called.&amp;#160; Then the web credentials are passed in to be checked by the web host.&amp;#160; The contents of the file are read into a Byte Array and then streamed up to the server.&amp;#160; The Try Catch block is advisable to warn the user if something has gone wrong.&amp;#160; The final Message Box confirms that the process has completed, hopefully successfully.&lt;/p&gt;

&lt;p&gt;When you create your method to call the method shown above, the only thing that might cause confusion is the ftpuri parameter.&amp;#160; This should in the following format:&lt;/p&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; “ftp://www.yourwebsitename.com/yourfilename.fileextension”&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;u&gt;Downloading 
    &lt;br /&gt;&lt;/u&gt;The method for downloading is fairly similar:&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;table&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td&gt;
          &lt;pre style="margin:0px;line-height:125%;"&gt;    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; FTPDownloadFile(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; downloadpath &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; ftpuri &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; ftpusername &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; ftppassword &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;)
        &lt;span style="color:#008000;"&gt;&amp;#39;Create a WebClient.&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; request &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; WebClient()

        &lt;span style="color:#008000;"&gt;&amp;#39; Confirm the Network credentials based on the user name and password passed in.&lt;/span&gt;
        request.Credentials = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; NetworkCredential(ftpusername, ftppassword)

        &lt;span style="color:#008000;"&gt;&amp;#39;Read the file data into a Byte array&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; bytes() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Byte&lt;/span&gt; = request.DownloadData(ftpuri)

        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;&amp;#39;  Create a FileStream to read the file into&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; DownloadStream &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; FileStream = IO.File.Create(downloadpath)
            &lt;span style="color:#008000;"&gt;&amp;#39;  Stream this data into the file&lt;/span&gt;
            DownloadStream.Write(bytes, 0, bytes.Length)
            &lt;span style="color:#008000;"&gt;&amp;#39;  Close the FileStream&lt;/span&gt;
            DownloadStream.Close()

        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MessageBox.Show(ex.Message)
            &lt;span style="color:#0000ff;"&gt;Exit&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;
        End &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;

        MessageBox.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;Process Complete&amp;quot;&lt;/span&gt;)

    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;The downloadpath parameter is the path and name of the file you want saved on your hard drive, e.g. C:\TempMyFile.dat.&lt;/p&gt;

&lt;p&gt;The ftpuri parameter is in the format:&lt;/p&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; “ftp://ftp.yourwebsitename/nameoffileonserver.fileext&lt;/p&gt;

&lt;p&gt;Username and password are those for your FTP service on your web host.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721280" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/File/default.aspx">File</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic.NET/default.aspx">Visual Basic.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic/default.aspx">Visual Basic</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Write/default.aspx">Write</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/upload/default.aspx">upload</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Download/default.aspx">Download</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/FTP/default.aspx">FTP</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/FileStream/default.aspx">FileStream</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Credentials/default.aspx">Credentials</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Web+Host/default.aspx">Web Host</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/GetRequestStream/default.aspx">GetRequestStream</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/FtpWebRequest/default.aspx">FtpWebRequest</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/WebRequest/default.aspx">WebRequest</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/WebClient/default.aspx">WebClient</category></item><item><title>How To Use a Previously Created Custom Control in a New Application</title><link>http://vbcity.com/blogs/xtab/archive/2016/03/30/how-to-use-a-previously-created-custom-control-in-a-new-application.aspx</link><pubDate>Wed, 30 Mar 2016 14:16:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721262</guid><dc:creator>Ged Mead</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In &lt;a href="http://vbcity.com/blogs/xtab/archive/2016/03/01/how-to-create-a-custom-tab-control.aspx"&gt;this earlier post&lt;/a&gt; I created a Class Library that contained a custom control.&amp;#160; I also added a Windows Forms project to that same solution and was able to use the control on the Form.&lt;/p&gt;  &lt;p&gt;When it comes to working with a new project where I want to use this same custom control, I used to think that if I added a Reference to this control’s DLL as shown in the screenshots below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2555.Add_2D00_Reference_5F00_0BEB54CC.png"&gt;&lt;img title="Add Reference" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Add Reference" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0676.Add_2D00_Reference_5F00_thumb_5F00_73B7C4A3.png" width="426" height="361" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6765.Browse_5F00_55A990E2.png"&gt;&lt;img title="Browse" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Browse" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6355.Browse_5F00_thumb_5F00_00A17EE8.png" width="437" height="240" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3632.Browse_2D00_to_2D00_the_2D00_DLL_5F00_216541CF.png"&gt;&lt;img title="Browse to the DLL" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Browse to the DLL" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0508.Browse_2D00_to_2D00_the_2D00_DLL_5F00_thumb_5F00_69EF0AD3.png" width="438" height="208" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7838.DLL_2D00_Reference_2D00_Added_5F00_38A02073.png"&gt;&lt;img title="DLL Reference Added" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="DLL Reference Added" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0083.DLL_2D00_Reference_2D00_Added_5F00_thumb_5F00_2FEFA21A.png" width="437" height="240" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;that this would enable you to drag instances of the custom control from the Toolbox onto a Form.&amp;#160; But it turns out I was mistaken (not for the first time!) and this Note in MSDN seems to confirm it:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2161.Not_2D00_Following_2D00_Reference_2D00_Chains_5F00_0CDF30AA.png"&gt;&lt;img title="Not Following Reference Chains" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Not Following Reference Chains" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7848.Not_2D00_Following_2D00_Reference_2D00_Chains_5F00_thumb_5F00_39C46DB8.png" width="845" height="488" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Of course if you do add a reference as shown in the steps above then you can create an instance of the custom control in code and set its properties and add handlers for its events, but c’mon -&amp;#160; be realistic – do you really want to do that?&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7345.Yuk_5F00_3DDE1FB0.png"&gt;&lt;img title="Yuk" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Yuk" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4300.Yuk_5F00_thumb_5F00_46EAAB31.png" width="508" height="332" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;So the best way seems to be to go to the Solution Explorer, right-click on the solution name and then choose Add Existing Project: &lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7532.Add_2D00_Existing_2D00_Project_5F00_76E4D6E5.png"&gt;&lt;img title="Add Existing Project" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Add Existing Project" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8103.Add_2D00_Existing_2D00_Project_5F00_thumb_5F00_50069AD8.png" width="528" height="286" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Browse until you find the project that houses the class library and open it.&amp;#160; This will add the class library project to the solution.&lt;/p&gt;  &lt;p&gt;Build the solution.&amp;#160; Now when you go to the Windows Form in the Windows Forms project and open the Toolbox you will see the custom control in the list.&amp;#160; You can then drag and drop instances onto forms.&lt;/p&gt;  &lt;p&gt;In the unlikely event that it isn’t visible, one thing to check is that the ‘Automatically Populate Toolbox option is set to True.&amp;#160;&amp;#160; This is turned on by default now in recent editions of Visual Studio so you shouldn’t came across this problem very often.&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8015.Automatically_2D00_Populate_2D00_Toolbox_5F00_29285ECB.png"&gt;&lt;img title="Automatically Populate Toolbox" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Automatically Populate Toolbox" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4214.Automatically_2D00_Populate_2D00_Toolbox_5F00_thumb_5F00_7628A896.png" width="549" height="322" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721262" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic.NET/default.aspx">Visual Basic.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic/default.aspx">Visual Basic</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Windows++Forms/default.aspx">Windows  Forms</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Toolbox/default.aspx">Toolbox</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Class+Library/default.aspx">Class Library</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Custom+Control/default.aspx">Custom Control</category></item><item><title>Serialize and Deserialize a Collection of Objects to a Data File</title><link>http://vbcity.com/blogs/xtab/archive/2016/03/15/serialize-and-deserialize-a-collection-of-objects-to-a-data-file.aspx</link><pubDate>Tue, 15 Mar 2016 15:36:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721236</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re creating business objects for your application then it&amp;rsquo;s likely that you&amp;rsquo;ll need to save them to file at some point.&amp;nbsp; One of the easiest ways to do this is with Serialization and Deserialization.&amp;nbsp; In this post I&amp;rsquo;ll show you how it works.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;strong&gt;Business Objects Collection        &lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;To demonstrate this, firstly I&amp;rsquo;ll need to create a simple class and create a collection of instances of that class.&amp;nbsp; Here&amp;rsquo;s one I created earlier (as they used to say in all the best TV DIY programs):&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;   
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;Imports&lt;/span&gt; System.Collections.ObjectModel

&amp;lt;Serializable&amp;gt;
&lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DrinkProduct&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;Enum&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MaterialType&lt;/span&gt;
        Granules
        Leaf
        Liquid
        Paste
        Powder
        Other
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Enum&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; New(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; ID &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; Name &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; PackType &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;,
           &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; BaseMaterial &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; MaterialType, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; Sales &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; Qty &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;)
            &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.ProductID = ID
            &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.ProductName = Name
            &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.PackageType = PackType
            &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Material = Material
            &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.AnnualSales = Sales
            &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Quantity = Qty
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; _ProductID &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt; ProductID() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
                &lt;span style="color:#0000ff;"&gt;Return&lt;/span&gt; _ProductID
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;)
                _ProductID = value
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; _ProductName &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt; ProductName() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
                &lt;span style="color:#0000ff;"&gt;Return&lt;/span&gt; _ProductName
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;)
                _ProductName = value
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; _PackageType &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt; PackageType() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
                &lt;span style="color:#0000ff;"&gt;Return&lt;/span&gt; _PackageType
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;)
                _PackageType = value
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; _Material &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; MaterialType
        &lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt; Material() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; MaterialType
            &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
                &lt;span style="color:#0000ff;"&gt;Return&lt;/span&gt; _Material
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; MaterialType)
                _Material = value
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; _quantity &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt; Quantity() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
                &lt;span style="color:#0000ff;"&gt;Return&lt;/span&gt; _quantity
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;)
                _quantity = value
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; _annualsales &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt; AnnualSales() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
                &lt;span style="color:#0000ff;"&gt;Return&lt;/span&gt; _annualsales
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Get&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Integer&lt;/span&gt;)
                _annualsales = value
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Set&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Property&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Overrides&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Function&lt;/span&gt; ToString() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.ProductName
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Function&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Shared&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Function&lt;/span&gt; GetProducts() &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; ObservableCollection(&lt;span style="color:#0000ff;"&gt;Of&lt;/span&gt; DrinkProduct)
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; CurrentProducts &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; ObservableCollection(&lt;span style="color:#0000ff;"&gt;Of&lt;/span&gt; DrinkProduct)
        &lt;span style="color:#0000ff;"&gt;With&lt;/span&gt; CurrentProducts
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;CF1kg&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Coffee Powder&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;1 Kg&amp;quot;&lt;/span&gt;, MaterialType.Powder, 15684, 1276))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;CFB500&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Ground Coffee&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;500 g&amp;quot;&lt;/span&gt;, MaterialType.Powder, 22785, 12856))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;CFG500&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Coffee Granules&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;500 g&amp;quot;&lt;/span&gt;, MaterialType.Granules, 19233, 5907))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;MlkSkim4lt&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Skimmed Milk 4L&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;4 Litre&amp;quot;&lt;/span&gt;, MaterialType.Liquid, 13472, 2640))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;Te500&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Tea&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;500 g&amp;quot;&lt;/span&gt;, MaterialType.Leaf, 8544, 235))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;TeInst500&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Instant Tea&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;500 g&amp;quot;&lt;/span&gt;, MaterialType.Powder, 1009, 22))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;MlkSkim1lt&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Skimmed Milk 1L&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;1 Litre&amp;quot;&lt;/span&gt;, MaterialType.Liquid, 28012, 2650))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;HiJ300&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;HiJuice Drink Mix&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;300 g&amp;quot;&lt;/span&gt;, MaterialType.Other, 17523, 179))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;Sm400&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Smoothie&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;400ml&amp;quot;&lt;/span&gt;, MaterialType.Paste, 9346, 3284))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;Beef300&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Beef Drink&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;300 g&amp;quot;&lt;/span&gt;, MaterialType.Granules, 8316, 1965))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;Beef750&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Beef Drink&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;750 g&amp;quot;&lt;/span&gt;, MaterialType.Granules, 7612, 359))
            .Add(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; DrinkProduct(&lt;span style="color:#a31515;"&gt;&amp;quot;MlkFC1lt&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Full Cream Milk&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;1 Litre&amp;quot;&lt;/span&gt;, MaterialType.Liquid, 32686, 4128))
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;With&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Return&lt;/span&gt; CurrentProducts
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Function&lt;/span&gt;

&lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Class&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Of course, you&amp;rsquo;ll have your own class and can ignore everything in the code snippet above except for one very important attribute:&lt;/p&gt;
&lt;p&gt;Note that at the top of the file, after the Imports statement and before the class name, I&amp;rsquo;ve added the &lt;strong&gt;Serializable &lt;/strong&gt;attribute.&amp;nbsp; Don&amp;rsquo;t forget to add this and encase it in angle brackets as shown above otherwise the serilaization code won&amp;rsquo;t work.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;strong&gt;Imports Statements&lt;/strong&gt; 
    &lt;br /&gt;&lt;/span&gt;The code that follows needs the following Imports statements at the top of the file.&amp;nbsp; I&amp;rsquo;m using WPF for this example, which is why the first reference is needed, so you can leave that out if you&amp;rsquo;re using Windows Forms:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;Imports&lt;/span&gt; System.Collections.ObjectModel
&lt;span style="color:#0000ff;"&gt;Imports&lt;/span&gt; System.IO
&lt;span style="color:#0000ff;"&gt;Imports&lt;/span&gt; System.Runtime.Serialization.Formatters.Binary&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;strong&gt;Application Variables and Constructor&lt;/strong&gt; 
    &lt;br /&gt;&lt;/span&gt;In my WPF version I&amp;rsquo;m creating an app-wide variable that will hold the collection of DrinkProduct objects.&amp;nbsp; I&amp;rsquo;ve chosen to use an ObservableCollection, but you can just as easily use a List(of DrinkProduct) instead if that suits you better.&lt;/p&gt;
&lt;p&gt;In the constructor of the Window, I&amp;rsquo;m calling the DrinkProduct method shown above that creates some dummy data &amp;ndash; the GetProducts method.&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;Class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MainWindow&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; DemoCollection &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; ObservableCollection(&lt;span style="color:#0000ff;"&gt;Of&lt;/span&gt; DrinkProduct)

    &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; New()

        &lt;span style="color:#008000;"&gt;&amp;#39; This call is required by the designer.&lt;/span&gt;
        InitializeComponent()

        &lt;span style="color:#008000;"&gt;&amp;#39; Add any initialization after the InitializeComponent() call.&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
            DemoCollection = DrinkProduct.GetProducts()
        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MessageBox.Show(ex.Message)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;strong&gt;Method that Serializes the Data&lt;/strong&gt; 
    &lt;br /&gt;&lt;/span&gt;Now we&amp;rsquo;re getting down to the nitty-gritty.&amp;nbsp; Here&amp;rsquo;s a simple method that will take that collection of DrinkProducts and save them to a data file:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;    &lt;span style="color:#0000ff;"&gt;Friend&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; SerializeData(OC &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; ObservableCollection(&lt;span style="color:#0000ff;"&gt;Of&lt;/span&gt; DrinkProduct), &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; PathAndFileName &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;)
        &lt;span style="color:#008000;"&gt;&amp;#39;  Create a FileStream and get the file to write to.&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; FS &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; FileStream = File.Create(PathAndFileName)
        &lt;span style="color:#008000;"&gt;&amp;#39;  Create a Binary Formatter for Serialization process&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; BinFmtr &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; BinaryFormatter

        &lt;span style="color:#008000;"&gt;&amp;#39;  Serialize the complete OC to the file&lt;/span&gt;
        BinFmtr.Serialize(FS, OC)

        &lt;span style="color:#008000;"&gt;&amp;#39;  Close the FileStream&lt;/span&gt;
        FS.Close()
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the first step is to create a FileStream to transport the data from the application to the file storage.&amp;nbsp; Next, a Binary Formatter is created and the FileStream together with the collection of objects are passed to the Serialize method of the Binary Formatter.&amp;nbsp; To finish the process the FileStream is closed.&lt;/p&gt;
&lt;p&gt;I always put code like this inside a Try Catch block, so the slightly longer version will be:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;    &lt;span style="color:#0000ff;"&gt;Friend&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; SerializeData(OC &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; ObservableCollection(&lt;span style="color:#0000ff;"&gt;Of&lt;/span&gt; DrinkProduct), &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; PathAndFileName &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;)
        &lt;span style="color:#008000;"&gt;&amp;#39;  Create a FileStream and get the file to write to.&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; FS &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; FileStream = File.Create(PathAndFileName)

        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;&amp;#39;  Create a Binary Formatter for Serialization process&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; BinFmtr &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; BinaryFormatter

            &lt;span style="color:#008000;"&gt;&amp;#39;  Serialize the complete OC to the file&lt;/span&gt;
            BinFmtr.Serialize(FS, OC)
        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MessageBox.Show(ex.Message)

        &lt;span style="color:#0000ff;"&gt;Finally&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;&amp;#39;  Close the FileStream&lt;/span&gt;
            FS.Close()
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;To call this method, I&amp;rsquo;ve used a Button Click event:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; btnSave_Click(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; RoutedEventArgs)
        &lt;span style="color:#008000;"&gt;&amp;#39;  Folder to dave the data file into&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; SaveFolder &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\MyDrinksApp&amp;quot;&lt;/span&gt;
        &lt;span style="color:#008000;"&gt;&amp;#39;  Check if it already exists&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; Directory.Exists(SaveFolder) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
            Directory.CreateDirectory(SaveFolder)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        SerializeData(DemoCollection, SaveFolder &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DrinkProducts.Dat&amp;quot;&lt;/span&gt;)
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Note the check in lines 5 to 7 that test whether the folder exists.&amp;nbsp; Without this, a non-existent folder name would crash the application.&amp;nbsp; Again, I always put this kind of code inside a Try Catch block.&lt;/p&gt;
&lt;p&gt;Also somewhere at the end of this pair of methods I would add code that tells the user that the serialization has been successful (I like to display the message in a TextBlock in the Window rather than a MessageBox, but it&amp;rsquo;s personal preference).&amp;nbsp; The Try Catch blocks will highlight any failures.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;Deserialize the Data 
      &lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;All that&amp;rsquo;s left is to show you how to read that data back from the file into the collection in memory.&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;    &lt;span style="color:#0000ff;"&gt;Friend&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; DeSerializeData(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; PathAndFileName &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;)
        &lt;span style="color:#008000;"&gt;&amp;#39;  Create a FileStream to access the storage file&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; FS &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; FileStream = &lt;span style="color:#0000ff;"&gt;Nothing&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
            FS = File.Open(PathAndFileName, FileMode.Open)

            &lt;span style="color:#008000;"&gt;&amp;#39;  Create a Binary Formatter for Serialization process&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; BinFmtr &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; BinaryFormatter

            &lt;span style="color:#008000;"&gt;&amp;#39;  Clear any previous so we can be sure the load works&lt;/span&gt;
            DemoCollection.Clear()
            &lt;span style="color:#008000;"&gt;&amp;#39;  Populate collection with data read from file&lt;/span&gt;
            DemoCollection = &lt;span style="color:#0000ff;"&gt;CType&lt;/span&gt;(BinFmtr.Deserialize(FS), ObservableCollection(&lt;span style="color:#0000ff;"&gt;Of&lt;/span&gt; DrinkProduct))

        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MessageBox.Show(ex.Message)

        &lt;span style="color:#0000ff;"&gt;Finally&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;&amp;#39;  Close the FileStream&lt;/span&gt;
            FS.Close()
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;

        MessageBox.Show(DemoCollection.Count.ToString &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot; DrinkProduct items read from file&amp;quot;&lt;/span&gt;)

    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;As with the serialization, you create a FileStream and pass in the path and file name of the data storage file, together with the instruction to open that file.&amp;nbsp; Then a Binary Formatter is created&lt;/p&gt;
&lt;p&gt;Line 12 is there only to test that the file read/deserialization works.&amp;nbsp; This is only necessary in this particular example project because the collection is automatically populated on application startup.&lt;/p&gt;
&lt;p&gt;Line 14 deserializes the data from the file into the (now empty) collection.&lt;/p&gt;
&lt;p&gt;And the rest of that method just does the housekeeping and shows appropriate messages.&lt;/p&gt;
&lt;p&gt;In my demo project I call this method from a button click with:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;1
2
3
4&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; btnRead_Click(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; RoutedEventArgs)
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; StorageFile &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\MyDrinksApp\DrinkProducts.Dat&amp;quot;&lt;/span&gt;
        DeSerializeData(StorageFile)
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;Summary 
    &lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;The Serialize and Deserialize methods make it very quick and easy to store and read back collections of business objects.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721236" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Windows++Forms/default.aspx">Windows  Forms</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Serialize/default.aspx">Serialize</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Deserialize/default.aspx">Deserialize</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Deserialization/default.aspx">Deserialization</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Serialization/default.aspx">Serialization</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Collection/default.aspx">Collection</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/List_2800_Of+T_2900_/default.aspx">List(Of T)</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/BinaryFormatter/default.aspx">BinaryFormatter</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Business+Objects/default.aspx">Business Objects</category></item><item><title>How To Create a Custom Tab Control</title><link>http://vbcity.com/blogs/xtab/archive/2016/03/01/how-to-create-a-custom-tab-control.aspx</link><pubDate>Tue, 01 Mar 2016 11:10:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721261</guid><dc:creator>Ged Mead</dc:creator><slash:comments>4</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I wrote &lt;a href="http://vbcity.com/blogs/xtab/archive/2014/09/14/windows-forms-how-to-bold-the-header-of-a-selected-tab.aspx"&gt;this post&lt;/a&gt; a while back that shows how to make the Header of any selected TabPage in a TabControl appear in Bold text.&amp;#160; Someone asked about using this approach multiple times across a Form or across the application.&amp;#160; In those cases, a better option is to create your own custom version of the TabControl which you can then select from the Toolbox whenever you need it.&lt;/p&gt;  &lt;p&gt;The first step is to create either a Windows Forms project or a Class Library.&amp;#160; Today. I’m going the Class Library route.&amp;#160; The reason for that decision is that it will make the custom control&amp;#160; more accessible from projects I might create in the future in which I want to use this same custom control.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4214.Create_2D00_Class_2D00_Library_5F00_78A61A55.png"&gt;&lt;img title="Create Class Library" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Create Class Library" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2577.Create_2D00_Class_2D00_Library_5F00_thumb_5F00_60DEBD22.png" width="571" height="375" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Next, use the Add &amp;gt; New Item menu to add a Custom Control to the Library:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8400.Add_2D00_Custom_2D00_Control_2D00_to_2D00_Library_5F00_3FDB24AE.png"&gt;&lt;img title="Add Custom Control to Library" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Add Custom Control to Library" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6355.Add_2D00_Custom_2D00_Control_2D00_to_2D00_Library_5F00_thumb_5F00_1B861A5F.png" width="574" height="379" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In the Solution Explorer, click on the Show All Files button, then expand the display of contents of the Custom Control as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7838.Show_2D00_all_2D00_files_5F00_2F035433.png"&gt;&lt;img title="Show all files" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Show all files" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2146.Show_2D00_all_2D00_files_5F00_thumb_5F00_6590FF77.png" width="432" height="346" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You will see that the Designer.vb file is now visible.&amp;#160;&amp;#160; Double click on this item to bring it up in the main edit area.&amp;#160; Now, edit the Inherits statement so that it reads as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8424.Edit_2D00_Inherits_2D00_Statement_5F00_4DC9A244.png"&gt;&lt;img title="Edit Inherits Statement" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Edit Inherits Statement" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/5775.Edit_2D00_Inherits_2D00_Statement_5F00_thumb_5F00_511B141F.png" width="727" height="588" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This control will now have all the attributes of the standard TabControl, but you can tweak its properties and behavior as you wish.&lt;/p&gt;  &lt;p&gt;Because this is a follow up to the Bold Page Header post, I’ll use that code for this example.&amp;#160; But of course you can make whatever changes you need for your purposes.&lt;/p&gt;  &lt;p&gt;In the Solution Explorer select the Custom Control’s code file.&amp;#160; In my example this will be the MyTabControl.vb file.&amp;#160; Then, in the code editor , type in:&amp;#160; Sub New()&amp;#160; below the Class name so that you can edit the constructor:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/1172.Add_2D00_constructor_5F00_0FCCBF2E.png"&gt;&lt;img title="Add constructor" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Add constructor" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2161.Add_2D00_constructor_5F00_thumb_5F00_06FCB6E2.png" width="576" height="285" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The End Sub should be added for you automatically when you hit the Enter key, but if it doesn’t appear, then type this in.&amp;#160; Next add code after the ‘Add any initialization after the InitializeComponent() call.&amp;#39;’ comment that will set the TabDrawMode property of this control.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3632.Set_2D00_the_2D00_TabDrawMode_2D00_property_5F00_1DFB1251.png"&gt;&lt;img title="Set the TabDrawMode property" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Set the TabDrawMode property" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4212.Set_2D00_the_2D00_TabDrawMode_2D00_property_5F00_thumb_5F00_478EDD84.png" width="585" height="227" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Now you can add the code that I described in the earlier post which will make any selected tab page’s header appear in bold:&lt;/p&gt;   &lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;   &lt;table&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td&gt;           &lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td&gt;
          &lt;pre style="margin:0px;line-height:125%;"&gt;    &lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; MyTabControl_DrawItem(sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Windows.Forms.DrawItemEventArgs) &lt;span style="color:#0000ff;"&gt;Handles&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.DrawItem
        &lt;span style="color:#008000;"&gt;&amp;#39;  Identify which TabPage is currently selected&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; SelectedTab &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Windows.Forms.TabPage = &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.TabPages(e.Index)

        &lt;span style="color:#008000;"&gt;&amp;#39;  Get the area of the header of this TabPage&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; HeaderRect &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Drawing.Rectangle = &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.GetTabRect(e.Index)

        &lt;span style="color:#008000;"&gt;&amp;#39;  Create a Brush to paint the Text&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; TextBrush &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; Drawing.SolidBrush(Drawing.Color.Black)

        &lt;span style="color:#008000;"&gt;&amp;#39;  Set the Alignment of the Text&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; sf &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; Drawing.StringFormat()
        sf.Alignment = Drawing.StringAlignment.Center
        sf.LineAlignment = Drawing.StringAlignment.Center

        &lt;span style="color:#008000;"&gt;&amp;#39;  Paint the Text using the appropriate Bold setting &lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; Convert.ToBoolean(e.State &lt;span style="color:#0000ff;"&gt;And&lt;/span&gt; Windows.Forms.DrawItemState.Selected) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; BoldFont &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; Drawing.Font(&lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Font.Name, &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Font.Size, Drawing.FontStyle.Bold)
            e.Graphics.DrawString(SelectedTab.Text, BoldFont, TextBrush, HeaderRect, sf)
        &lt;span style="color:#0000ff;"&gt;Else&lt;/span&gt;
            e.Graphics.DrawString(SelectedTab.Text, e.Font, TextBrush, HeaderRect, sf)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        &lt;span style="color:#008000;"&gt;&amp;#39;  Job done - dispose of the Brush&lt;/span&gt;
        TextBrush.Dispose()
    &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;You can have a less verbose version if you add appropriate Imports statements to the class file.&lt;/p&gt;

&lt;p&gt;I won’t go through how this works here as there’s a full explanation in the earlier post.&amp;#160; &lt;/p&gt;

&lt;p&gt;Build the Solution.&amp;#160; &lt;/p&gt;

&lt;p&gt;For this example I’m going to add a new Windows Forms project to the solution that I’ve used to create the control.&amp;#160; In a later post I’ll talk about accessing the custom control in projects you might create later.&lt;/p&gt;

&lt;p&gt;In the Solution Explorer click on the Solution name and select Add New Project.&amp;#160; This time, add a Windows Forms project.&lt;/p&gt;

&lt;p&gt;Display the Windows Form in the design pane and then open the Toolbox.&amp;#160; The control you’ve just created should be visible under its own tab:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/5466.CustomControl_2D00_in_2D00_Toolbox_5F00_58466265.png"&gt;&lt;img title="CustomControl in Toolbox" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="CustomControl in Toolbox" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7851.CustomControl_2D00_in_2D00_Toolbox_5F00_thumb_5F00_4E31C13A.png" width="570" height="532" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Drag an instance onto the Form’s surface and you’ll see that the first TabPage is in bold.&amp;#160; That’s because by default this TabPage is selected.&amp;#160;&amp;#160; If you click on TabPage2 or any other pages you add, these will become Bold.&lt;/p&gt;

&lt;p&gt;If you try and run this solution now you’ll get an error message:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/1663.Cannot_2D00_start_2D00_with_2D00_Class_2D00_Library_5F00_45519321.png"&gt;&lt;img title="Cannot start with Class Library" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Cannot start with Class Library" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7851.Cannot_2D00_start_2D00_with_2D00_Class_2D00_Library_5F00_thumb_5F00_0D4F9F3E.png" width="550" height="257" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;To fix this, right click on the Windows Forms Project name in the Solution Explorer and select the Set as Startup Project option:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2158.Set_2D00_as_2D00_Startup_2D00_Project_5F00_6F416B7C.png"&gt;&lt;img title="Set as Startup Project" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Set as Startup Project" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3225.Set_2D00_as_2D00_Startup_2D00_Project_5F00_thumb_5F00_0282221C.png" width="280" height="394" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;







&lt;p&gt;And that’s it; you’re good to go.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721261" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic.NET/default.aspx">Visual Basic.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic/default.aspx">Visual Basic</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Windows++Forms/default.aspx">Windows  Forms</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/TabControl/default.aspx">TabControl</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/TabPage/default.aspx">TabPage</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Bold/default.aspx">Bold</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Class+Library/default.aspx">Class Library</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/A+project+with+an+Output+Type+of+Class+Library+cannot+be+started+directly/default.aspx">A project with an Output Type of Class Library cannot be started directly</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Custom+Control/default.aspx">Custom Control</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Set+as+Startup+Project/default.aspx">Set as Startup Project</category></item><item><title>‘The process cannot access the file’ Error–A Better Fix</title><link>http://vbcity.com/blogs/xtab/archive/2016/02/25/the-process-cannot-access-the-file-error-a-better-fix.aspx</link><pubDate>Thu, 25 Feb 2016 11:30:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721190</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In this earlier post I described an issue I was having with getting access to a file that I had created.&amp;#160; My solution at the time was to use the sledgehammer of GC.Collect.&amp;#160; While working on something else today, a better answer occurred to me.&lt;/p&gt;  &lt;p&gt;Here’s the code I’m using to create the new file:&lt;/p&gt;   &lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;   &lt;table&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td&gt;           &lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td&gt;
          &lt;pre style="margin:0px;line-height:125%;"&gt;        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;&amp;#39; Get the path to the Application Data folder&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; appData &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForget&amp;quot;&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; Directory.Exists(appData) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                Directory.CreateDirectory(appData)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; FileToSave &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForget\DFI_Data.Dat&amp;quot;&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; File.Exists(FileToSave) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                File.Create(FileToSave)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;
      

        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MessageBox.Show(ex.Message)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;And to fix the Process cannot access the file issue I added a couple more lines.&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;table&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td&gt;
          &lt;pre style="margin:0px;line-height:125%;"&gt;        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;&amp;#39; Get the path to the Application Data folder&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; appData &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForget&amp;quot;&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; Directory.Exists(appData) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                Directory.CreateDirectory(appData)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; FileToSave &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForget\DFI_Data.Dat&amp;quot;&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; File.Exists(FileToSave) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                File.Create(FileToSave)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

            &lt;span style="color:#008000;"&gt;&amp;#39;  To get round the access denied issue.&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; DownloadedFile &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; FileStream = IO.File.OpenRead(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForget\DFI_Data.Dat&amp;quot;&lt;/span&gt;)
            DownloadedFile.Close()

        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MessageBox.Show(ex.Message)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;Line 16 creates a new FileStream object that references the file I just created.&amp;#160; Then line 17 closes that file.&lt;/p&gt;

&lt;p&gt;Now the method I run to serialize data into that file runs immediately after the file creation without any problem.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721190" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/File/default.aspx">File</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic.NET/default.aspx">Visual Basic.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/problem/default.aspx">problem</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Exception/default.aspx">Exception</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/CreateFile/default.aspx">CreateFile</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Access+a+file/default.aspx">Access a file</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/because+it+is+being+used+by+another+process/default.aspx">because it is being used by another process</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/The+process+cannot+access+the+file/default.aspx">The process cannot access the file</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/File.Close/default.aspx">File.Close</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/FileStream/default.aspx">FileStream</category></item><item><title>‘The process cannot access the file’ Error after creating a new file in code</title><link>http://vbcity.com/blogs/xtab/archive/2016/02/24/the-process-cannot-access-the-file-error-after-creating-a-new-file-in-code.aspx</link><pubDate>Wed, 24 Feb 2016 16:53:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721179</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I got this error message while I was trying to write some user data to the hard drive.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3323.Process_2D00_cannot_2D00_access_2D00_this_2D00_file_5F00_3BC2BB2A.png"&gt;&lt;img height="207" width="551" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2158.Process_2D00_cannot_2D00_access_2D00_this_2D00_file_5F00_thumb_5F00_7D2D47B6.png" alt="Process cannot access this file" border="0" style="background-image:none;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-width:0px;" title="Process cannot access this file" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now this is a fairly common problem and usually comes about because you&amp;rsquo;ve been careless and left an IO object such as a FileStream open.&amp;nbsp; In this case though things were a bit different.&amp;nbsp; Firstly, here&amp;rsquo;s the code I was using to creae a new file that would hold some application data:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;   
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;&amp;#39; Get the path to the Application Data folder&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; appData &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForget&amp;quot;&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; Directory.Exists(appData) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                Directory.CreateDirectory(appData)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; FileToSave &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForget\DFI_Data.Dat&amp;quot;&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; File.Exists(FileToSave) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                File.Create(FileToSave)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MessageBox.Show(ex.Message)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;So no opportunity to use the Using block approach or to close any IO objects or processes there.&lt;/p&gt;
&lt;p&gt;When I then try and access that file and write to it I get the error shown above.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I searched for ways of accessing the internal code that does the Directory and File creation and tried all sorts of things to get round this issue.&amp;nbsp; In the end, a search brought up the answer on StackOverflow &amp;ndash; manually doing a garbage collection.&amp;nbsp;&amp;nbsp;&amp;nbsp; Now I know this issue is contentious, a source of a lost of argument and there are a ton of detailed posts that advise against doing GC.Collect.&amp;nbsp; One of the key reasons is that it&amp;rsquo;s a blocking call; that is to say it will usually hold up the running of your application until the collection is completed.&amp;nbsp; Time is also taken to scan all live objects.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m reasonably sure though that my relatively simple application will only have a tiny bit of garbage to collect at the point I run it.&amp;nbsp; So I don&amp;rsquo;t see this as an issue in this case.&amp;nbsp; The first line of the code call just before I try to write to the file will be:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt; System.GC.Collect()&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now although this will do the job, there&amp;rsquo;s another failsafe that it&amp;rsquo;s always wise to include if you use GC.Collect.&amp;nbsp;&amp;nbsp; That is to also call GC.WaitForPendingFinalizers after GC.Collect and before going to call any methods that access the troublesome file.&amp;nbsp;&amp;nbsp; In spite of GC.Collect being a blocking call, there are some situations (though not in my simple example) where the Collect method can be interrupted.&amp;nbsp; So GC.WaitForPendingFinalizers does what its name suggests &amp;ndash; it won&amp;rsquo;t let the code move on until the current garbage collection is completed.&lt;/p&gt;
&lt;p&gt;So my final code looks like:&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;1
2
3
4
5
6
7
8
9&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;

            System.GC.Collect()
            System.GC.WaitForPendingFinalizers()

            SerializeData(TestCollection2, FileToSave)
        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; IOException
            Debug.WriteLine(ex.Message)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;where &amp;lsquo;SerializeData&amp;rsquo; is a separate method I have that serializes a collection of objects to the file.&amp;nbsp; I&amp;rsquo;ll be showing that method in a later post to describe how to serialize data to a file, but any method you use to access the file and write to it will now work if you use the approach described here.&lt;/p&gt;
&lt;p&gt;I suspect there may be way that doesn&amp;rsquo;t require the sledgehammer approach of GC.Collect but I haven&amp;rsquo;t found it yet, so am happy to use it in this case.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721179" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/Write/default.aspx">Write</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Exception/default.aspx">Exception</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Error/default.aspx">Error</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/CreateDirectory/default.aspx">CreateDirectory</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/CreateFile/default.aspx">CreateFile</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/GC.Collect/default.aspx">GC.Collect</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Access+a+file/default.aspx">Access a file</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/GarbageCollection/default.aspx">GarbageCollection</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/because+it+is+being+used+by+another+process/default.aspx">because it is being used by another process</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/GC.WaitForPendingFinalization/default.aspx">GC.WaitForPendingFinalization</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/The+process+cannot+access+the+file/default.aspx">The process cannot access the file</category></item><item><title>Adding Files to the AppData Folder</title><link>http://vbcity.com/blogs/xtab/archive/2016/02/22/adding-files-to-the-appdata-folder.aspx</link><pubDate>Mon, 22 Feb 2016 15:39:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721177</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In &lt;a href="http://vbcity.com/blogs/xtab/archive/2016/02/17/problem-finding-those-special-folders.aspx"&gt;this previous post&lt;/a&gt; I mentioned one of the possible pitfalls when accessing the Environment.SpecialFolder collection.&amp;#160; One of the things you’ll probably need to do is create a sub-folder for your particular application’s data and one or more files to hold whatever data you want to store.&amp;#160; This is perfectly straightforward using the methods in the System.IO namespace.&lt;/p&gt;   &lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt;  &lt;span style="font-weight:bold;color:#008800;"&gt;Dim&lt;/span&gt; MyAppData &lt;span style="font-weight:bold;color:#000000;"&gt;As&lt;/span&gt; &lt;span style="font-weight:bold;color:#333399;"&gt;String&lt;/span&gt; &lt;span style="color:#333333;"&gt;=&lt;/span&gt; Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &lt;span style="color:#333333;"&gt;&amp;amp;&lt;/span&gt; &lt;span style="background-color:#fff0f0;"&gt;&amp;quot;\DontForgetIt&amp;quot;&lt;/span&gt;
          &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;(If you’re mumbling to yourself “Should’ve used Path.Combine”, I know that’s usually best practice.&amp;#160; For some reason – probably because of the way the enumeration is translated into a Path string internally - Path.Combine doesn’t work here.)&lt;/p&gt;

&lt;p&gt;Speaking of best practice, you should really build in a test that such a folder doesn’t exist before you try and create one, although AFAIK you won’t get an Exception if you don’t do this.&amp;#160; Code might be along the lines of:&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; MyAppData &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)&amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForgetIt&amp;quot;&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; Directory.Exists(MyAppData) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                Directory.CreateDirectory(MyAppData)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Still on the subject of testing if a folder exists, I was surprised to find that if you miss out the test for existence and a folder of that name and location does already exist, then a new one is created, but files in the original folder are not lost!&amp;#160; I always thought that was one of the key reasons why we run this test before creating new – but again maybe it’s because the special folders are, well, special!&lt;/p&gt;

&lt;p&gt;Once you’ve located the folder, you’ll need to create a file to use to store the data.&amp;#160; Here’s the kind of code you can use for this step:&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; FileToSave &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForgetIt\DFI_Data.Dat&amp;quot;&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; File.Exists(FileToSave) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                File.Create(FileToSave)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You’ll see that again I’ve built in that test for previous existence of the file.&amp;#160; Note also that I’ve written the path out in full – again this is because trying to use MyAppData String as a shortcut doesn’t work.&amp;#160; And again Path.Combine isn’t an option that works.&lt;/p&gt;

&lt;p&gt;Once you have the file in place you can write to it using the usual System.IO Write methods or – if you’re saving objects rather than plain text – to Serialize the objects to that file.&lt;/p&gt;

&lt;p&gt;As a general rule of good practice – and&amp;#160; in my experience when working with System.IO in particular - you should put your code in a Try Catch block.&amp;#160; So the final code for this example would be:&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;        &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; MyAppData &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForgetIt&amp;quot;&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; Directory.Exists(MyAppData) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                Directory.CreateDirectory(MyAppData)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; FileToSave &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) &amp;amp; &lt;span style="color:#a31515;"&gt;&amp;quot;\DontForgetIt\DFI_Data.Dat&amp;quot;&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Not&lt;/span&gt; File.Exists(FileToSave) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
                File.Create(FileToSave)
            &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;

        &lt;span style="color:#0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; Exception
            MessageBox.Show(ex.Message)
        &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Try&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721177" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Exception+handling/default.aspx">Exception handling</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Exception/default.aspx">Exception</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Try-Catch/default.aspx">Try-Catch</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/GetFolderPath/default.aspx">GetFolderPath</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/LocalApplicationData/default.aspx">LocalApplicationData</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Environment.SpecialFolder/default.aspx">Environment.SpecialFolder</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Exists/default.aspx">Exists</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Path.Combine/default.aspx">Path.Combine</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/File.Exists/default.aspx">File.Exists</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/CreateDirectory/default.aspx">CreateDirectory</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/CreateFile/default.aspx">CreateFile</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Directory.Exists/default.aspx">Directory.Exists</category></item><item><title>Problem Finding Those Special Folders?</title><link>http://vbcity.com/blogs/xtab/archive/2016/02/17/problem-finding-those-special-folders.aspx</link><pubDate>Wed, 17 Feb 2016 14:59:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721176</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You probably know that since VS2010 (I think it was) you can get quick access to what are known as the ‘Special Folders’.&amp;#160; These are folders like My Documents, My Pictures, My Music, etc.&amp;#160; The list of Special Folders also includes sub-folders in the C:\Users\&amp;lt;UserName&amp;gt;\AppData folder – specifically the Local&amp;#160; and Roaming folders that are meant to be used to store data specific to individual applications.&lt;/p&gt;  &lt;p&gt;On the face of it, you would think the syntax to access these folders is straightforward.&amp;#160; Maybe something like:&lt;/p&gt;   &lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt; &lt;span style="font-weight:bold;color:#008800;"&gt;Dim&lt;/span&gt; MyAppData &lt;span style="font-weight:bold;color:#000000;"&gt;As&lt;/span&gt; &lt;span style="font-weight:bold;color:#333399;"&gt;String&lt;/span&gt; &lt;span style="color:#333333;"&gt;=&lt;/span&gt; Environment.SpecialFolder.LocalApplicationData&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And if you were to build and run the application with this code, you won’t get an exception.&lt;/p&gt;

&lt;p&gt;The trouble is that you won’t really be getting an accessible path to the folder on your hard either.&amp;#160; To show you what I mean, try this code:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;            &lt;span style="font-weight:bold;color:#008800;"&gt;Dim&lt;/span&gt; MyAppData &lt;span style="font-weight:bold;color:#000000;"&gt;As&lt;/span&gt; &lt;span style="font-weight:bold;color:#333399;"&gt;String&lt;/span&gt; &lt;span style="color:#333333;"&gt;=&lt;/span&gt; Environment.SpecialFolder.LocalApplicationData

            Debug.WriteLine(MyAppData)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;You’ll find that in the Debug Window that you won’t get the C:\Users\&amp;lt;UserName&amp;gt;\AppData string that you might expect.&amp;#160; What you will get is a number – in this case 28.&amp;#160; &lt;/p&gt;

&lt;p&gt;And the reason for this result is that Environment.SpecialFolder.LocalApplicationData is not the actual folder, but is just a member of the Environment.SpecialFolder &lt;strong&gt;Enumeration.&amp;#160; &lt;/strong&gt;Which is why you get a number returned – that 28 is the Enum value of the particular folder.&amp;#160; These special folders can be stored in different places under different Windows OS versions and sometimes even between different locales.&lt;/p&gt;

&lt;p&gt;So, all very interesting, but how do you use this to get to the &lt;em&gt;actual&lt;/em&gt; folder itself, you ask.&amp;#160; The answer is that you use the &lt;strong&gt;GetFolderPath&lt;/strong&gt; method.&amp;#160;&amp;#160; Try this:&lt;/p&gt;


&lt;div style="overflow:auto;border-top:gray 0.1em solid;border-right:gray 0.1em solid;width:auto;background:#ffffff;border-bottom:gray 0.1em solid;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;border-left:gray 0.8em solid;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;            &lt;span style="font-weight:bold;color:#008800;"&gt;Dim&lt;/span&gt; MyAppData &lt;span style="font-weight:bold;color:#000000;"&gt;As&lt;/span&gt; &lt;span style="font-weight:bold;color:#333399;"&gt;String&lt;/span&gt; &lt;span style="color:#333333;"&gt;=&lt;/span&gt; Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

            Debug.WriteLine(MyAppData)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now you’ll see the appropriate file path for the local data for the current user on your machine.&amp;#160; And armed with that correct path name to an actual folder you can happily go on and access it and do whatever it is you need to do.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721176" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Tips/default.aspx">Tips</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic.NET/default.aspx">Visual Basic.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic/default.aspx">Visual Basic</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/problem/default.aspx">problem</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/GetFolderPath/default.aspx">GetFolderPath</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/My+Documents/default.aspx">My Documents</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/SpecialFolder/default.aspx">SpecialFolder</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/LocalApplicationData/default.aspx">LocalApplicationData</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Environment.SpecialFolder/default.aspx">Environment.SpecialFolder</category></item><item><title>System Utility:  EasyBCD</title><link>http://vbcity.com/blogs/xtab/archive/2016/02/14/system-utility-easybcd.aspx</link><pubDate>Sun, 14 Feb 2016 11:33:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:721175</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This isn’t one of my VB posts, but I’ve had a few tough days installing and setting up Windows 10 so that I can start working on – and writing about – Windows 10 Universal Windows Platform apps.&amp;#160; One of the biggest hurdles I had was to get my system to boot into Windows 10.&amp;#160;&amp;#160; Many frustrating hours of installing, reinstalling, re-reinstalling…etc, etc. got me nowhere.&amp;#160; I even convinced myself my hard drive was pooped, so splashed out on a new one, which turned out not to be needed.&lt;/p&gt;  &lt;p&gt;Anyway, the point of this post is to highlight a great little utility called EasyBCD.&amp;#160; I just love utilities that do one thing and do it well.&amp;#160;&amp;#160; And EasyBCD definitely falls into that category.&amp;#160; I won’t go into the various features (not that I know them all!) but I was able to use their Edit Boot Menu option to immediately and painlessly fix the boot problem I’d been cursing at for days.&lt;/p&gt;  &lt;p&gt;You can visit their home page&amp;#160; &lt;a href="https://neosmart.net/EasyBCD/"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2642.EasyBCD1_5F00_0951B0EC.png"&gt;&lt;img title="EasyBCD1" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="EasyBCD1" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2148.EasyBCD1_5F00_thumb_5F00_5FC9F786.png" width="672" height="580" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The menu is easy to understand, even for those of us who only vaguely understand system booting technology.&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7444.EasyBCD2_5F00_63B3F9BE.png"&gt;&lt;img title="EasyBCD2" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="EasyBCD2" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6371.EasyBCD2_5F00_thumb_5F00_20887B91.png" width="689" height="578" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;If you want to try out their free version, then just click on the ‘SEE PLANS’ option.&amp;#160; If, like me, you only needed it the one time (I hope, anyway!), you can use it to restore your sanity and then make whatever donation you think appropriate via PayPal.&amp;#160;&amp;#160; Alternatively of course you can buy the commercial version if your needs are greater or you are using it for commercial, not just personal usage.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=721175" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/System/default.aspx">System</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Installation+issues/default.aspx">Installation issues</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Boot+Manager/default.aspx">Boot Manager</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Windows+10/default.aspx">Windows 10</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/EasyBCD/default.aspx">EasyBCD</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Hard+Drive/default.aspx">Hard Drive</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Problems/default.aspx">Problems</category></item><item><title>How to Find and Close a Window in a Running WPF application</title><link>http://vbcity.com/blogs/xtab/archive/2015/12/04/how-to-find-and-close-a-window-in-a-running-wpf-application.aspx</link><pubDate>Fri, 04 Dec 2015 14:07:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720999</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I recently had a case where I was using the ShowDialog() method to display an options form to the user.&amp;#160; One of the options was to close the parent form and move on somewhere else and show a different Window.&amp;#160; I thought at first that I’d come up with some form of code along the lines of:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Me.Parent.Close()&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;or more likely something like:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;CType(Me.Parent, Window).Close()&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;But I couldn’t find a way of using this approach that worked.&lt;/p&gt;  &lt;p&gt;However I knew that the Application instance keeps a running record of its loaded Windows, so I fell back on the good old For/Next route and enumerated through that collection of Window instances :&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;For Each wind As Window In Application.Current.Windows     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; If wind.Name = &amp;quot;MainWindow1&amp;quot; Then wind.Close()      &lt;br /&gt;Next      &lt;br /&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;and checked for the name of the particular Window I wanted to close and closed it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720999" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Window/default.aspx">Window</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF+Window/default.aspx">WPF Window</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Application.Current/default.aspx">Application.Current</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Close/default.aspx">Close</category></item><item><title>‘Sound API only supports playing PCM wave files’ Problem</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/19/sound-api-only-supports-playing-pcm-wave-files-problem.aspx</link><pubDate>Thu, 19 Nov 2015 17:18:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720962</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I hit this problem while working on a game in WPF.&amp;#160; As far as I knew before this, a wav file was just a wav file, but it turns out that there are some sub-formats too.&amp;#160; So I had this happy little sound bite that I’d found on my hard drive and thought I’d use it.&amp;#160; My preference has been to use a SoundPlayer element for these kind of tasks.&amp;#160; The code that instantiates a new SoundPlayer object looks like:&lt;/p&gt;   &lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt;   &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; HappyRiffSound &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SoundPlayer(&lt;span style="color:#a31515;"&gt;&amp;quot;Sounds/FunkyMusic1.wav&amp;quot;&lt;/span&gt;)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When I tried to Play the SoundPlayer at run time with:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;      HappyRiffSound.Play()&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The following error popped up:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6518.PCMfilesOnly_5F00_645AD002.png"&gt;&lt;img title="PCMfilesOnly" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="PCMfilesOnly" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0638.PCMfilesOnly_5F00_thumb_5F00_34507E81.png" width="571" height="214" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;I had a quick look round the web for help and saw that it is possible to get hold of converters that would maybe convert what I had here to the required PCM format.&amp;#160; But my needs were small and I didn’t want to pay for a tool I’d hardly ever need.&amp;#160; I also try to avoid free tools these days as they’re often more trouble than they’re worth.&amp;#160; &lt;/p&gt;

&lt;p&gt;So I fiddled around a bit and then remembered that I’d used a MediaPlayer object instead of SoundPlayer one time, so tried this code:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;    &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; MedPlayer &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; MediaPlayer&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Followed by this code in an event handler:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;   MedPlayer.Open(&lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; Uri(&lt;span style="color:#a31515;"&gt;&amp;quot;Sounds/FunkyMusic1.wav&amp;quot;&lt;/span&gt;, UriKind.Relative)) MedPlayer.Play()&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And I was back in business.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720962" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Tips/default.aspx">Tips</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Sounds/default.aspx">Sounds</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/SoundPlayer/default.aspx">SoundPlayer</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/MediaPlayer/default.aspx">MediaPlayer</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Gotcha/default.aspx">Gotcha</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/PCM+Format/default.aspx">PCM Format</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/WAV+Files/default.aspx">WAV Files</category></item><item><title>LineBreak and RenderTransform Issue</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/18/linebreak-and-rendertransform-issue.aspx</link><pubDate>Wed, 18 Nov 2015 08:52:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720961</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I came across an interesting issue today when I was creating&amp;#160; TextBlock that was to be animated.&amp;#160; To facilitate the animation, the TextBlock has to include a TransformGroup in the element’s RenderTransform Property in the Xaml.&amp;#160; &lt;/p&gt;  &lt;p&gt;I also needed this TextBlock to have two separate lines of text.&amp;#160; To achieve this, I used a LineBreak.&amp;#160; When you use a LineBreak, you can’t put it inside the &lt;strong&gt;explicit&lt;/strong&gt; property declaration – i.e. you’re not allowed to do this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4035.Nesting_2D00_Not_2D00_Allowed_5F00_7AFC935F.png"&gt;&lt;img title="Nesting Not Allowed" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Nesting Not Allowed" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2772.Nesting_2D00_Not_2D00_Allowed_5F00_thumb_5F00_1D247919.png" width="952" height="87" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This is because LineBreak is an Object in its own right and you can’t have anything other than string values in that Text property when it’s explicitly placed in the opening tag as an attribute..&lt;/p&gt;  &lt;p&gt;However, if you put that same Substring/LineBreak Object/Substring combination in between the opening and closing tags of the TextBlock then the Xaml parser will allow it.&amp;#160; So this would work:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2705.No_2D00_Nesting_2D00_Here_5F00_481C671E.png"&gt;&lt;img title="No Nesting Here" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="No Nesting Here" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3872.No_2D00_Nesting_2D00_Here_5F00_thumb_5F00_06A1B4C5.png" width="954" height="135" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Now, as I mentioned,&amp;#160; in this project my TextBlock also needs to have its RenderTransform property set.&amp;#160; (Actually, I used Blend for the animation so this was inserted automatically).&amp;#160; So now (currently without the Text property but ready to allow animation)&amp;#160; the Xaml for the TextBlock looks like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7266.RenderTRansform_2D00_Added_5F00_235B29DA.png"&gt;&lt;img title="RenderTRansform Added" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="RenderTRansform Added" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4861.RenderTRansform_2D00_Added_5F00_thumb_5F00_4CEEF50D.png" width="968" height="233" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Everything is OK so far.&amp;#160; But when I added the Text property to the Xaml, unthinkingly I placed it in (what felt like) the same position in the Xaml constructor as the earlier example, i.e. directly at the end of the opening tag:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8640.Unthinking_2D00_Positioning_5F00_282DB7C9.png"&gt;&lt;img title="Unthinking Positioning" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Unthinking Positioning" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0333.Unthinking_2D00_Positioning_5F00_thumb_5F00_1B5361AB.png" width="984" height="249" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;But of course that position highlighted by the arrow isn’t the end of the opening tag.&amp;#160; It’s the end of the list of attributes that are translated into properties and before the complex RenderTransform property definition.&amp;#160; Putting it here caused a screenfull of what at first seemed to be apparently unrelated error messages.&amp;#160; However, the first message helps clarify the issue:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0652.Multiple_2D00_Errors_5F00_27B15F07.png"&gt;&lt;img title="Multiple Errors" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Multiple Errors" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6014.Multiple_2D00_Errors_5F00_thumb_5F00_3A85E2B1.png" width="733" height="281" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The ‘Property elements cannot be in the middle of an element’s content’’ tells me what I’ve done wrong.&amp;#160; And of course what I need to do to fix it is simply to move that Text property so that it is the final property in the collection of properties described in the Xaml as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/5123.Correct_2D00_Location_5F00_0FD9DC07.png"&gt;&lt;img title="Correct Location" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Correct Location" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3463.Correct_2D00_Location_5F00_thumb_5F00_6DA1D080.png" width="1008" height="268" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;With this done, everything works as it should.&amp;#160;&amp;#160; &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720961" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Tips/default.aspx">Tips</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Animation/default.aspx">Animation</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/TransformGroup/default.aspx">TransformGroup</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/RenderTransform/default.aspx">RenderTransform</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Transform/default.aspx">Transform</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Gotcha/default.aspx">Gotcha</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/LineBreak/default.aspx">LineBreak</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Text+Property/default.aspx">Text Property</category></item><item><title>Overriding the Default EventTrigger in a Blend Animation</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/17/overriding-the-default-eventtrigger-in-a-blend-animation.aspx</link><pubDate>Tue, 17 Nov 2015 07:57:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720959</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;When you create an animation using Blend for Visual Studio, Blend tries its hardest to do everything it thinks you need.&amp;#160; To take an example, I just created a simple TranslateTransform animation that moves a Canvas up and slightly across from an area outside the visible screen up to a point about halfway up the screen.&amp;#160; &lt;/p&gt;  &lt;p&gt;The Objects and Timeline window in the Animation Workspace in Blend displays this animation:&lt;/p&gt; &lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/1803.SimpleAnimation_5F00_07814AAB.png"&gt;&lt;img title="SimpleAnimation" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="SimpleAnimation" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3365.SimpleAnimation_5F00_thumb_5F00_49580A2C.png" width="577" height="410" /&gt;&lt;/a&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I’ve written other posts that describe how to create an animation from start to finish in Blend, so I won’t go into it here.&amp;#160; But what I do want to mention is that one of the things Blend does for you is to insert an EventTrigger in the Xaml.&amp;#160; An EventTrigger is just what its name implies - an action is triggered when a certain event happens.&amp;#160; In this case the default event is the Window.Loaded event.&amp;#160; You can actually see this setting as it is created for you in Blend:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3858.Default_2D00_Event_2D00_Trigger_5F00_0E60B196.png"&gt;&lt;img title="Default Event Trigger" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Default Event Trigger" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4846.Default_2D00_Event_2D00_Trigger_5F00_thumb_5F00_5AB84537.png" width="578" height="267" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;So of course this means that when you run the project back in Visual Studio, this animation will automatically start.&amp;#160; &lt;/p&gt;  &lt;p&gt;You may not want this to happen.&amp;#160; Maybe you want some user action or the reaching of some particular value to be the cause of the animation starting to run.&amp;#160; In the latter case you’ll need to remove this EventTrigger.&amp;#160; I’m no Blend expert, so I may have missed it, but I couldn’t find a way of removing the EventTrigger here in the Animation pane.&amp;#160; So when I want to make this kind of change I will either select the code display option in Blend – this is the button that displays two angle brackets and is located towards the top right of the design pane:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3858.Code_2D00_Display_2D00_Button_5F00_4FCB3E22.png"&gt;&lt;img title="Code Display Button" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Code Display Button" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2705.Code_2D00_Display_2D00_Button_5F00_thumb_5F00_0E508BC9.png" width="586" height="229" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Or of course you can just access the Xaml pane in Visual Studio.&lt;/p&gt;  &lt;p&gt;The Xaml in this case looked like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4848.Event_2D00_Trigger_2D00_Xaml_5F00_4255054F.png"&gt;&lt;img title="Event Trigger Xaml" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Event Trigger Xaml" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7288.Event_2D00_Trigger_2D00_Xaml_5F00_thumb_5F00_41106C70.png" width="584" height="142" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Note its location in the Xaml file, where it is always inserted after the end of the Window.Resources collection.&amp;#160; (The animation itself is by default placed by Blend inside that Resources collection.)&lt;/p&gt;  &lt;p&gt;Once you’ve deleted the EventTrigger, you’ll need either to create a new trigger in Xaml to tie the animation to another event of your choosing or, as in my case, to start off the animation in code when a certain value is reached:&lt;/p&gt;   &lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt;            &lt;span style="color:#008000;"&gt;&amp;#39;  Target score reached so Animate the Level Up display&lt;/span&gt;
            &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; MessageStoryboard &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; Storyboard()
            MessageStoryboard = &lt;span style="color:#0000ff;"&gt;CType&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.Resources(&lt;span style="color:#a31515;"&gt;&amp;quot;cvsBrickWallSlideUp&amp;quot;&lt;/span&gt;), Storyboard)
            MessageStoryboard.Begin()&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You’ll see that you can’t just reference the Storyboard directly in the code behind, but must first find it in the Window’s Resources and then cast it to a new Storyboard object.&amp;#160; To avoid confusion, I probably should have made it clear that you have to reference the Storyboard object and not the individual animation/s.&amp;#160;&amp;#160;&amp;#160; The Storyboard object holds one or more animation objects as its children.&amp;#160; In this example the Storyboard contains two animations:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4540.Storyboard_2D00_Xaml_5F00_13AED6A0.png"&gt;&lt;img title="Storyboard Xaml" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="Storyboard Xaml" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2308.Storyboard_2D00_Xaml_5F00_thumb_5F00_1EC83B1D.png" width="918" height="184" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that’s it.&amp;#160; You can get Blend to do the tricky graphical stuff and then in Visual Studio tweak the event or action that fires the animation storyboard when you want it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720959" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Animation/default.aspx">Animation</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/EventTrigger/default.aspx">EventTrigger</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Storyboard/default.aspx">Storyboard</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Blend+for+Visual+Studio/default.aspx">Blend for Visual Studio</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Window.Resources/default.aspx">Window.Resources</category></item><item><title>TextBlock “Run Text” is Not The Same as TextBlock.Text</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/16/textblock-run-text-is-not-the-same-as-textblock-text.aspx</link><pubDate>Mon, 16 Nov 2015 10:04:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720958</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I had a strange problem this morning where I was trying to extract the Text value from a TextBlock. Simple enough, you would think.&amp;#160; But although I could clearly see the Text on screen at runtime, my call to grab its Text property always returned an empty string.&lt;/p&gt;  &lt;p&gt;Here’s the Xaml that creates the TextBlock:&lt;/p&gt;   &lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt;                &amp;lt;TextBlock x:Name=&lt;span style="color:#a31515;"&gt;&amp;quot;tbWordLengthBonus&amp;quot;&lt;/span&gt; Height=&lt;span style="color:#a31515;"&gt;&amp;quot;32&amp;quot;&lt;/span&gt; HorizontalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; Foreground=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF2B58CF&amp;quot;&lt;/span&gt; 
                           FontSize=&lt;span style="color:#a31515;"&gt;&amp;quot;17&amp;quot;&lt;/span&gt; Margin=&lt;span style="color:#a31515;"&gt;&amp;quot;3,1&amp;quot;&lt;/span&gt; Padding=&lt;span style="color:#a31515;"&gt;&amp;quot;3,0&amp;quot;&lt;/span&gt;  FontStyle=&lt;span style="color:#a31515;"&gt;&amp;quot;Italic&amp;quot;&lt;/span&gt; 
                           FontWeight=&lt;span style="color:#a31515;"&gt;&amp;quot;ExtraBold&amp;quot;&lt;/span&gt; &amp;gt;&amp;lt;Run Text=&lt;span style="color:#a31515;"&gt;&amp;quot;+ 50&amp;quot;&lt;/span&gt;/&amp;gt; &amp;lt;/TextBlock&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;and here’s the simple line of code that wants to see what’s in that TextBlock:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; temp &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt; = tbWordLengthBonus.Text&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The problem of the missing text was caused by the way the Xaml had constructed the TextBlock.&amp;#160; Instead of assigning a string value to its Text property, it uses a Run, as you can see on the third line of that Xaml snippet.&amp;#160; This Run was created - unbeknown to me -&amp;#160; at some stage when I was laying out the UI.&amp;#160; I really don’t know how it came to be constructed this way.&amp;#160; Maybe I unknowingly dragged something in?&amp;#160; But whatever the cause, the visual effect is the same so in most cases this will pass by unnoticed.&lt;/p&gt;

&lt;p&gt;A Run of course is an element in its own right, albeit a very low level one.&amp;#160; So my search for a string in the VB snippet above is looking in the wrong place, because the populated string is inside the Run, not the parent TextBlock.&amp;#160; The TextBlock.Text property is indeed an empty string.&amp;#160; &lt;/p&gt;

&lt;p&gt;The fix in this case is simply to remove that Run element and change the Xaml so that the Text property of the TextBlock contains a populated string:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;                &amp;lt;TextBlock x:Name=&lt;span style="color:#a31515;"&gt;&amp;quot;tbWordLengthBonus&amp;quot;&lt;/span&gt; Height=&lt;span style="color:#a31515;"&gt;&amp;quot;32&amp;quot;&lt;/span&gt; HorizontalAlignment=&lt;span style="color:#a31515;"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; Foreground=&lt;span style="color:#a31515;"&gt;&amp;quot;#FF2B58CF&amp;quot;&lt;/span&gt; 
                           FontSize=&lt;span style="color:#a31515;"&gt;&amp;quot;17&amp;quot;&lt;/span&gt; Margin=&lt;span style="color:#a31515;"&gt;&amp;quot;3,1&amp;quot;&lt;/span&gt; Padding=&lt;span style="color:#a31515;"&gt;&amp;quot;3,0&amp;quot;&lt;/span&gt;  FontStyle=&lt;span style="color:#a31515;"&gt;&amp;quot;Italic&amp;quot;&lt;/span&gt; 
                           FontWeight=&lt;span style="color:#a31515;"&gt;&amp;quot;ExtraBold&amp;quot;&lt;/span&gt; Text=&lt;span style="color:#a31515;"&gt;&amp;quot;+ 50&amp;quot;&lt;/span&gt; &amp;gt;&amp;lt;/TextBlock&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It’s just another of those quirky little Gotchas that can catch you out if you’re not careful.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720958" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Text/default.aspx">Text</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Tips/default.aspx">Tips</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/TextBlock/default.aspx">TextBlock</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Gotcha/default.aspx">Gotcha</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Run/default.aspx">Run</category></item><item><title>WPF: Programmatically Select the Last Item in a ListBox</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/12/wpf-programmatically-select-the-last-item-in-a-listbox.aspx</link><pubDate>Thu, 12 Nov 2015 11:54:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720941</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I wanted a user to be able always to see the latest item added to a ListBox even if the Height of the ListBox was too short.&amp;#160; As this is WPF, the actual height could and would change from time to time.&amp;#160; Clearly then I just needed to add code that would automatically scroll to the end each time a new item is added, regardless of length.&amp;#160;&amp;#160; Initially&amp;#160; this didn’t turn out to be as straightforward as I’d thought it would.&lt;/p&gt;  &lt;p&gt;WPF has a method named ScrollIntoView on the ListBox (and other controls).&amp;#160; So I thought that this code would do the trick:&lt;/p&gt;  &lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt;               lstWords.ScrollIntoView(lstWords.Items.Count - 1)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But it didn’t.&amp;#160; It seems logical enough to me but for whatever reason, ListBox just sat there with the first ten or so items showing.&lt;/p&gt;

&lt;p&gt;A bit of research into how to fix this brought up some very complicated suggestions in some places, including one that showed how to create your own attached property for a list box.&amp;#160; While I’m sure that approach works fine, I really felt there had to be something simpler.&lt;/p&gt;

&lt;p&gt;And of course there is.&amp;#160; Changing my code to force the ListBox to first select the last item in the list and then passing this item to that ScrollIntoView method did the trick:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;                lstWords.SelectedIndex = lstWords.Items.Count - 1 
                lstWords.ScrollIntoView(lstWords.SelectedItem)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;(Intriguingly, though, this doesn’t):&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;                lstWords.SelectedIndex = lstWords.Items.Count - 1
                lstWords.ScrollIntoView(lstWords.SelectedIndex)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The difference being SelectedIndex and not SelectedItem at the end of the second line.&lt;/p&gt;

&lt;p&gt;I did also find an alternative that did work in my particular scenario:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;lstWords.Items.MoveCurrentToLast() 
lstWords.ScrollIntoView(lstWords.Items.CurrentItem)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But of course that MoveCurrentToLast instruction in the above snippet might not always be right for all needs.&amp;#160; So I think I’ll be sticking with my first solution in most cases.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720941" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Visual+Basic.NET/default.aspx">Visual Basic.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/ListBox/default.aspx">ListBox</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/ScrollIntoView/default.aspx">ScrollIntoView</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/SELECT/default.aspx">SELECT</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/SelectedIndex/default.aspx">SelectedIndex</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/MoveCurrentToLast/default.aspx">MoveCurrentToLast</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/SelectedItem/default.aspx">SelectedItem</category></item><item><title>How To Identify the Foreground Color of a Control in WPF at Runtime</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/10/how-to-identify-the-foreground-color-of-a-control-in-wpf-at-runtime.aspx</link><pubDate>Tue, 10 Nov 2015 16:18:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720918</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In the game I’m currently developing, one of the things I’m doing is letting the player select a tile and, once it’s selected, changing the font color on that tile from Black to Transparent.&amp;#160; That way, the code can run tests on the content but as far as the player is concerned it’s gone – until there’s a reset and the text becomes visible again.&lt;/p&gt;  &lt;p&gt;One of the problems I’ve found with doing this is that you can easily set the Foreground property to Black in code with:&lt;/p&gt;   &lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt; diamond.Foreground = &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SolidColorBrush(Colors.Black)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That is, you can use the named color.&amp;#160; But when you later want to test if the Foreground is Black, you can’t use:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; diamond.Foreground = SolidColorBrush(Colors.Black) &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;or&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; diamond.Foreground = Colors.Black &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It seems that you have to find the hex value for the named color and use this in place of (in this case) Colors.Black.&amp;#160;&amp;#160; So, as Black is of course #FF000000 you can use:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; diamond.Foreground.ToString = &lt;span style="color:#a31515;"&gt;&amp;quot;#FF000000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Black is easy; we all know that value.&amp;#160; But what if you’ve used some other named color?&amp;#160; Well, an easy way of identifying it is to run this code:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;Debug.WriteLine(diamond.Foreground.ToString)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;at design time and you’ll see the hex value displayed in the Output window.&amp;#160; You can then just use that in your test line.&lt;/p&gt;

&lt;p&gt;Although this post deals specifically with the Foreground property, you can of course use the same approach for any color based property of a control or element in WPF.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720918" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Tips/default.aspx">Tips</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Colors/default.aspx">Colors</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Foreground/default.aspx">Foreground</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Color/default.aspx">Color</category></item><item><title>How to Remove the Final Character From a TextBox</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/09/how-to-remove-the-final-character-from-a-textbox.aspx</link><pubDate>Mon, 09 Nov 2015 07:50:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720919</guid><dc:creator>Ged Mead</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you want to delete the last character in a TextBox in code, an easy way to do this is to create a substring of the text that starts at the first character and select a length value that is one less than the total length of the string.&amp;nbsp; So in the case below, where the text is in a TextBox named InputTextBox, you can find that string with:&lt;/p&gt;

&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;InputTextBox.Text.Substring(0, InputTextBox.Text.Length - 1)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;In this snippet the parameters are 0 and InputTextBox.Text.Length &amp;ndash; 1.&amp;nbsp; The 0 represents the start point for the substring (i.e. the first character) and the second parameter sets the length as one character less than the complete text in that TextBox.&lt;/p&gt;
&lt;p&gt;To use this as a way of removing the last character, you simply rewrite the Text property of the TextBox:&lt;/p&gt;

&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;InputTextBox.Text = InputTextBox.Text.Substring(0, InputTextBox.Text.Length - 1)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Of course you can take this general approach and tweak it for other deletion tasks.&amp;nbsp; For example, you may want to remove the first letter from the TextBox, in which case, you would use:&lt;/p&gt;

&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;InputTextBox.Text = InputTextBox.Text.Substring(1, InputTextBox.Text.Length - 1)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;In this case, the first parameter is set to 1, which means that the substring will start from the second character in the TextBox and scoop up the rest.&lt;/p&gt;
&lt;p&gt;Finally, you&amp;rsquo;re not limited to decreasing the length of the Substring by only a value of 1.&amp;nbsp; If in somewhat bizarre circumstances you needed to remove the final two characters from the Text Box then you just need to tweak that second parameter:&lt;/p&gt;

&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
&lt;pre style="margin:0px;line-height:125%;"&gt;InputTextBox.Text = InputTextBox.Text.Substring(0, InputTextBox.Text.Length - 2)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As it happens, I&amp;rsquo;m using this character removal code in a WPF project, but the same approach will work for Windows Forms and, I believe, Windows Store apps.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720919" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Text/default.aspx">Text</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Tips/default.aspx">Tips</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/TextBox/default.aspx">TextBox</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Windows++Forms/default.aspx">Windows  Forms</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Remove/default.aspx">Remove</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Character/default.aspx">Character</category></item><item><title>Sound Files not playing in a WPF Project?</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/08/sound-files-not-playing-in-a-wpf-project.aspx</link><pubDate>Sun, 08 Nov 2015 11:06:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720915</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I haven’t been able to do much developing for a while and it’s quite scary how quickly you forget even the most basic things!&amp;#160; I’m working on a game today so of course it’s going to need lots of sound effects.&amp;#160; I copied and pasted in&amp;#160; a whole bunch of sound files and stored them in a folder named Sounds.&lt;/p&gt;  &lt;p&gt;After adding the required reference to System.Media namespace at the top of the file with an Imports statement,&lt;/p&gt;   &lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;Imports&lt;/span&gt; System.Media&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I then created a variable for one of the sounds:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt; &lt;span style="color:#0000ff;"&gt;Dim&lt;/span&gt; PositiveSound &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt; SoundPlayer(&lt;span style="color:#a31515;"&gt;&amp;quot;Sounds/PositiveSound.wav&amp;quot;&lt;/span&gt;)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next, I tried to play this sound using:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;   PositiveSound.Play()&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But all I got was silence.&amp;#160; &lt;/p&gt;

&lt;p&gt;The problem is that when you add a new sound file or files, the default settings are as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7776.WPF_2D00_Do_2D00_Not_2D00_Copy_2D00_Setting_5F00_5E9DE03E.png"&gt;&lt;img title="WPF Do Not Copy Setting" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="WPF Do Not Copy Setting" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6886.WPF_2D00_Do_2D00_Not_2D00_Copy_2D00_Setting_5F00_thumb_5F00_4F1ACE6F.png" width="450" height="270" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Build Action is ‘Content’ and the Copy To Output Directory setting is ‘Do not copy’.&amp;#160;&amp;#160; It’s this second setting that causes the problem.&lt;/p&gt;

&lt;p&gt;Content files are described in MSDN as ‘standalone data files that have an explicit association with and executable WPF assembly’.&amp;#160; Basically then, files that the WPF application might call upon at some time but not files that are crucial to the running of the core application itself.&amp;#160; (Those crucial files, for the record, are shown with a Build Action of ‘Resource’ in the file properties and as a result will be compiled directly into the WPF assembly.)&lt;/p&gt;

&lt;p&gt;So my sound files are correctly categorized because they’ll only be called upon now and again as a kind of peripheral task.&amp;#160; In order for the compiled application to know where they are, the Copy to Output Directory setting has to be changed from ‘Do not copy’ to either ‘Copy Always’ or ‘Copy if newer’.&amp;#160;&amp;#160; To be honest, I’ve read quite a few articles about Content files and haven’t yet seen a case where Do not copy would have been a useful setting, so it does make me wonder why it’s set as the default.&amp;#160; But there’s probably a good reason somewhere.&amp;#160; &lt;/p&gt;

&lt;p&gt;The fix then is to make this change in the Properties pane.&amp;#160; By the way, if – as I did – you copy and paste in a whole raft of files, you can select them all in the Solution Explorer and change the setting for all of them in one go.&amp;#160; You don’t have to spend ages dibbing through them one by one.&lt;/p&gt;

&lt;p&gt;Which of these two settings is best?&amp;#160; The consensus is that usually ‘Copy if newer’ is the better option most times, unless you expect the file content to change at or between run times.&amp;#160; The reason clearly is that if the files are not going to change then there’s no point in using unnecessary processing power to rebuild and copy them every time the project is built.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720915" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Tips/default.aspx">Tips</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Sounds/default.aspx">Sounds</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/SoundPlayer/default.aspx">SoundPlayer</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Content/default.aspx">Content</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Do+Not+Copy/default.aspx">Do Not Copy</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Copy+if+newer/default.aspx">Copy if newer</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/System.Media/default.aspx">System.Media</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Copy+Always/default.aspx">Copy Always</category></item><item><title>The WPF MouseLeftButtonDown Anomaly</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/07/the-wpf-mouseleftbuttondown-anomaly.aspx</link><pubDate>Sat, 07 Nov 2015 17:33:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720914</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I was developing a game in WPF in which the player has to select from a display of buttons.&amp;#160; The pressing of any of these buttons causes various checks to be carried out.&amp;#160; As it is the same check for each button, I did of course create one event handler and wired all the buttons to this.&amp;#160; Then, as the first line of code of the handler I identified which button is was that was pressed:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt; Dim btn As Button = CType(sender, Button)&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;and then started the check on that individual button.&lt;/p&gt;  &lt;p&gt;Because I wanted the checks to take place only if the user used the left mouse button, I picked what I thought to be the logical event handler for this – the MouseLeftButtonDown event.&amp;#160; But when I ran the project, this event handler was simply ignored.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;At first, I thought it might be because I was using a button that I’d changed pretty dramatically from the standard button, using a new control template in a resource dictionary.&amp;#160; But it turned out not to be that.&lt;/p&gt;  &lt;p&gt;So then I thought that maybe the MouseDown event would be the answer.&amp;#160; Once again though the handler didn’t work when I left clicked.&amp;#160; It did work with a right click, but I didn’t need that.&lt;/p&gt;  &lt;p&gt;It turns out to be all to do with way WPF deals with routed events.&amp;#160; As soon as the button is pressed, the MouseLeftButtonDown event is handled internally by the button.&amp;#160; So the process never gets as far as my handling code.&amp;#160; &lt;/p&gt;  &lt;p&gt;There are at least two ways of fixing this problem.&amp;#160; The first is the rather obvious one that you’ve probably already thought of– it’s a button, so use its Click event!&amp;#160; It’s the default event and there’s no good reason I know of for not using it.&amp;#160; This event ignores right-clicks of course, so is ideal in this situation.&lt;/p&gt;  &lt;p&gt;The other fix, if you should ever really need to do something tricky with the event handling of a WPF button, is to use the &lt;strong&gt;Preview&lt;/strong&gt;MouseLeftButtonDown event handler.&amp;#160; As its name implies, this will kick in as the very first action when the user left-clicks the button.&amp;#160; You might need this in a situation where you want to carry out a test before allowing some result to happen.&amp;#160; Note though that when the user left-clicks on the button, both the PreviewMouseLeftButtonDown event and also the Click event will still fire and you can place separate code in both event handlers if you needed to.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720914" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Button/default.aspx">Button</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/MouseLeftButtonDown/default.aspx">MouseLeftButtonDown</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/PreviewMouseLeftButtonDown/default.aspx">PreviewMouseLeftButtonDown</category></item><item><title>Catching And Displaying Key Presses In A WPF Window</title><link>http://vbcity.com/blogs/xtab/archive/2015/11/06/catching-and-displaying-key-presses-in-a-wpf-window.aspx</link><pubDate>Fri, 06 Nov 2015 14:14:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:720916</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I had a scenario where I wanted a user to be able to hit any key on the keyboard and for this key press to be processed and checked to see if it is either an alphabetic key or the Enter key.&amp;#160; I wanted the key presses to be captured on a Window-wide basis – that is, regardless of which, if any, other controls or elements might have focus at the time.&amp;#160; Because of the way that WPF using tunnelling events, this is really easy to do.&lt;/p&gt;  &lt;p&gt;The trick is to use the Preview.. event handler in these kind of cases – in this case, the&amp;#160; PreviewKeyDown event of the Window itself.&lt;/p&gt;   &lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;   &lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Sub&lt;/span&gt; MainWindow_PreviewKeyDown(&lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:#0000ff;"&gt;As&lt;/span&gt; System.Windows.Input.KeyEventArgs) &lt;span style="color:#0000ff;"&gt;Handles&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Me&lt;/span&gt;.PreviewKeyDown&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;By using the PreviewKeyDown event handler you ensure that the Window PreviewKeyDown event is handled before any other key down events.&amp;#160; For instance, if the Window contains a Canvas, as mine does, then any instructions for the key down event of that canvas are ignored until the overriding Window key down event has been dealt with.&lt;/p&gt;

&lt;p&gt;There are lots of ways of testing that the user has hit a letter key, some of them quite complicated,&amp;#160; but in this case the following simple approach is all that’s needed:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt;&lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; e.Key.ToString &lt;span style="color:#0000ff;"&gt;Like&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;[A-Z]&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt; InputTextBox.Text += (e.Key.ToString)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The key to this (no pun intended) is the use of the ToString method on e.Key.&amp;#160; Without this, the code won’t function properly.&amp;#160; e.Key is a Double value representing an input key.&amp;#160; But ToString takes that value and converts it to the represented keyboard key – the letter, numeral, symbol, etc. – that it represents.&amp;#160; In the snippet above, I add any each letter of the alphabet that the user types in to the text in a TextBox.&amp;#160; &lt;/p&gt;

&lt;p&gt;Testing for the Enter key is equally easy:&lt;/p&gt;


&lt;div style="overflow:auto;width:auto;background:#ffffff;padding-bottom:0.2em;padding-top:0.2em;padding-left:0.6em;padding-right:0.6em;"&gt;
  &lt;pre style="margin:0px;line-height:125%;"&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt; e.Key = Key.Enter &lt;span style="color:#0000ff;"&gt;Then&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;&amp;#39;  Do something interesting&lt;/span&gt;
 &lt;span style="color:#0000ff;"&gt;End&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;If&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In this case of course there’s no need, nor any sense, in converting this key ToString.&amp;#160; &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=720916" width="1" height="1"&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/WPF/default.aspx">WPF</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Window/default.aspx">Window</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/PreviewKeyDown/default.aspx">PreviewKeyDown</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Key+Press/default.aspx">Key Press</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/e.Key.ToString/default.aspx">e.Key.ToString</category></item></channel></rss>