<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><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><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/vbCityBlogs" /><feedburner:info uri="vbcityblogs" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>A third party tools story</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/QdXg1p64JU8/a-third-party-tools-story.aspx</link><pubDate>Wed, 15 Feb 2012 14:06:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:707033</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’ve been doing a lot of work on Windows Phone 7 recently – more on that in upcoming blogs – so as a result, I was looking for a particular kind of list box functionality. Specifically, I wanted a ‘never-ending’ list box – that is, one that would keep looping round once the user reached the bottom (or top) of the list.&lt;/p&gt;  &lt;p&gt;The idea of the game I was working on is that the player has to line up words on the left with words on the right in a particular combination.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0753.word_2D00_game_5F00_675C0D43.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="word game" border="0" alt="word game" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0131.word_2D00_game_5F00_thumb_5F00_6AAD7F1E.jpg" width="136" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;And, I finally found just such a control, the Looping List Box from &lt;a href="http://www.mindscapehq.com/products/phone-elements/controls/wp7-looping-list-box" target="_blank"&gt;Mindscape&lt;/a&gt;.&amp;#160; But, you know how it is with controls, somehow you always want some additional functionality or other.&amp;#160; In my case, I just needed a way of identifying and accessing the inner content of an item in the looping list box if the player used an item correctly.&amp;#160; &lt;/p&gt;  &lt;p&gt;When I found I couldn’t do this very easily, except with a bit of long, roundabout hack, I thought I’d ask in the Mindscape forum if there was some easy way of doing this that I’d missed – a highly likely scenario most days for me!&amp;#160; Within a working day or so, I got more than I expected.&amp;#160; The tool developer added the functionality I needed as a core part of the control and made the new version available in that day’s nightly build.&amp;#160; (And for good measure, he also gave an example of how I could use the revised features).&lt;/p&gt;  &lt;p&gt;Now that, my fellow developers, is what I call customer service!&amp;#160; Well done, Mindscape guys!&amp;#160; (And in case you’re wondering, I don’t know of any of them, and don’t have any financial reason to praise them ;&amp;#160; I just thought that I couldn’t let such a good example of how to look after your customers go unblogged.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=707033" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/QdXg1p64JU8" height="1" width="1"/&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/Windows+Phone/default.aspx">Windows Phone</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/3rd+party+tools/default.aspx">3rd party tools</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2012/02/15/a-third-party-tools-story.aspx</feedburner:origLink></item><item><title>How to filter files in an OpenFileDialog</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/QQ--Dsnf1os/how-to-filter-files-in-an-openfiledialog.aspx</link><pubDate>Wed, 04 Jan 2012 11:50:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:706346</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction      &lt;br /&gt;&lt;/u&gt;I don’t know what it is about the syntax for the Filter property of the open file dialog, but it often seems to take me longer than it should to get it to work the way I want.&amp;#160; If you’ve had problems too, then this may help you.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Step 1: create the OpenFileDialog      &lt;br /&gt;&lt;/u&gt;Although you can drag an open file dialog from the toolbox, it’s just as easy to create it on the fly:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;#160; &lt;font style="background-color:#ffffff;"&gt;&lt;font color="#ff0000"&gt;Dim openfiledlg As New OpenFileDialog&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;u&gt;Step 2: Create a With block      &lt;br /&gt;&lt;/u&gt;You can write code that’s easier to follow if you use a With block:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt;With openfiledlg&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font color="#ff0000"&gt;End With&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;u&gt;Step 3: (Optionally) set a default path     &lt;br /&gt;&lt;/u&gt;You can make life easier by setting a value on the InitialDirectory property:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt;With openfiledlg       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .InitialDirectory = &amp;quot;C:\Images&amp;quot;        &lt;br /&gt;End With        &lt;br /&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;u&gt;Step 4: Set the Filter property     &lt;br /&gt;&lt;/u&gt;So here’s the usually tricky bit.&amp;#160; To start with a typical example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt;.Filter = &amp;quot;Image Files (*.png *.jpg *.bmp) |*.png; *.jpg; *.bmp|All Files(*.*) |*.*&amp;quot;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Then, to break this down, first look at the sub string from Image Files to the first pipe (|) symbol:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt;.Filter = &amp;quot;Image Files (*.png *.jpg *.bmp) &lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This is what the user will see in the file type combo box at the bottom right corner of the dialog. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8838.openfiledialog_5F00_0E1BA5A8.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="openfiledialog" border="0" alt="openfiledialog" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8764.openfiledialog_5F00_thumb_5F00_526B711A.png" width="643" height="424" /&gt;&lt;/a&gt;&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/4478.openfiledialog_5F00_6F80F357.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="openfiledialog" border="0" alt="openfiledialog" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0250.openfiledialog_5F00_thumb_5F00_6D63F48E.png" width="422" height="222" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It’s traditional to show the user the file extensions of a given category – such as the three shown here – but it isn’t mandatory.&amp;#160; You could just use the string ‘Image Files’ on its own, for instance.&lt;/p&gt;  &lt;p&gt;The next part:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt;|*.png; *.jpg; *.bmp&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;tells the dialog what file types to filter in and allow to be displayed.&amp;#160; In most cases you’ll use same file extensions as you used in the display.&amp;#160; Note that you need to insert semi-colons between the file extensions to identify where one extension ends and the next one begins. &lt;/p&gt;  &lt;p&gt;The last part of the Filter property code in this example simply adds a second choice of file type.&amp;#160; This time, it’s the standard catch-all of the universal wild card:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt;|All Files(*.*) |*.*&amp;quot;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You can see this as the second choice in the file type combo box above.&lt;/p&gt;  &lt;p&gt;Because there’s only one extension being used, there’s no need to include a semi-colon after the extension.&lt;/p&gt;  &lt;p&gt;So, to summarize the syntax of the Filter property, it’s:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Description of the first file type or types&amp;#160;&amp;#160; - e.g. “Image Files (*.png *.jpg *.bmp)&lt;/li&gt;    &lt;li&gt;followed by a Pipe symbol&lt;/li&gt;    &lt;li&gt;followed by an asterisk, a period, and the first extension – e.g. *.png&lt;/li&gt;    &lt;li&gt;followed by a semi-colon &lt;/li&gt;    &lt;li&gt;followed by further file extensions, with a semi-colon between each extension&lt;/li&gt;    &lt;li&gt;If you want to display more than one line in the file type combo box, repeat steps 1 to 5 above for each extension or group of extensions you want to include.&lt;/li&gt; &lt;/ol&gt;      &lt;p&gt;&lt;u&gt;Step 5: (Optionally) Set a value on the FilterIndex property     &lt;br /&gt;&lt;/u&gt;If you have a long list of extensions and you want to encourage the user to home in on a particular one first, you can set the FilterIndex property to make one of the file type combo box items display first.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt; .FilterIndex = 3&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Unusually, this value isn’t zero based, so you set it to 3, for example, to make the third item in the combo box the default selection.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Summary     &lt;br /&gt;&lt;/u&gt;And that’s all there is to it.&amp;#160; I think it’s combination of multiple pipes and semi-colons that makes this task look trickier than it really is.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=706346" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/QQ--Dsnf1os" height="1" width="1"/&gt;</description><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/.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/OpenFileDialog/default.aspx">OpenFileDialog</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2012/01/04/how-to-filter-files-in-an-openfiledialog.aspx</feedburner:origLink></item><item><title>Three and a half simple tips</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/1mALWxG_4jY/three-and-a-half-simple-tips.aspx</link><pubDate>Fri, 30 Dec 2011 14:31:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:705455</guid><dc:creator>LarryBlake</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Happy holidays.&amp;nbsp; Here are a few easy programming tips that can improve your technique.&amp;nbsp; They&amp;#39;re aimed at beginners, as well as more experienced programmers who started with VB Classic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TIP 1: &amp;nbsp;Combine variable declaration and initialization.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The following two code&amp;nbsp;snippets are equivalent:&lt;/p&gt;
&lt;p&gt;&lt;pre name="bf3f5a5690bc431a81b75d45c0f16541" class="VB.NET"&gt;
Dim sr5 As Decimal 
... 
sr5 = Math.Sqrt(5)
&lt;/pre&gt; &lt;/p&gt;
&lt;p&gt;&lt;pre name="cb70c37c3eae481dae623c8bdae8b589" class="VB.NET"&gt;
Dim sr5 As Decimal = Math.Sqrt(5)
&lt;/pre&gt; &lt;/p&gt;
&lt;p&gt;However, I strongly prefer the second usage, because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It&amp;#39;s less typing.&lt;/li&gt;
&lt;li&gt;The intention of the variable is clearer.&amp;nbsp; The reader doesn&amp;#39;t have to hunt through code, thinking &amp;quot;I wonder what sr5 means?&amp;quot;&lt;/li&gt;
&lt;li&gt;It avoids &amp;quot;variable not initialized&amp;quot; errors.&lt;/li&gt;
&lt;li&gt;By not&amp;nbsp;declaring a variable until you&amp;#39;re&amp;nbsp;ready to use&amp;nbsp;it, you can keep its scope smaller.&amp;nbsp; This code can be placed anywhere in a block, and the later you do so, the simpler your debugging becomes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;TIP 2: Use Booleans&amp;nbsp;to help document, and to avoid&amp;nbsp;recoding the same&amp;nbsp;test condition.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Again, here are the &amp;quot;before&amp;quot; and &amp;quot;after&amp;quot; versions:&lt;/p&gt;
&lt;p&gt;&lt;pre name="28eee3a2acd3407aa24e9f14a31358cd" class="VB.NET"&gt;
Dim emp As New EmployeeRec 
If (HoursWorked &amp;gt; 40) Then 
    emp.RegHours = 40 
    emp.HasOT = True 
    emp.OTHours = HoursWorked - 40 
Else 
    emp.RegHours = HoursWorked 
    emp.HasOT = False 
End If
&lt;/pre&gt; &lt;/p&gt;
&lt;p&gt;&lt;pre name="c8b570286b44416685b70c8be5dc559d" class="VB.NET"&gt;
Dim emp As New EmployeeRec 
Dim WorkedOT As Boolean = (HoursWorked &amp;gt; 40)
If (WorkedOT) Then
    emp.RegHours = 40 
    emp.OTHours = HoursWorked - 40 
Else 
    emp.RegHours = HoursWorked 
End If
emp.HasOT = WorkedOT
&lt;/pre&gt; &lt;/p&gt;
&lt;p&gt;In this case, the second version doesn&amp;#39;t&amp;nbsp;benefit from significantly&amp;nbsp;briefer code (although it often does).&amp;nbsp; The benefits are readability, and a more&amp;nbsp;obvious mapping of the condition to the EmployeeRec.HasOT attribute.&amp;nbsp; I could have shortened this further by using emp.HasOT directly&amp;nbsp;instead of introducing the Boolean, but it would have been less illustrative.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TIP 2A: Use the shorthand&amp;nbsp;boolean assignment.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The assignment of&amp;nbsp;WorkedOT in the above&amp;nbsp;example is much shorter than the code below.&amp;nbsp; So don&amp;#39;t do it this way:&lt;/p&gt;
&lt;p&gt;&lt;pre name="ccd27b57d5054c1ebd0e1493e026d1ac" class="VB.NET"&gt;
Dim WorkedOT As Boolean
If (HoursWorked &amp;gt; 40) Then
    WorkedOT = True
Else 
    WorkedOT = False
End If
&lt;/pre&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TIP 3: Use variables to prevent multiple calls to functions.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Again, before and after:&lt;/p&gt;
&lt;p&gt;&lt;pre name="1b45da8381da4d6884b21f31f05f0139" class="VB.NET"&gt;
If CalculateOTPay(HoursWorked, HourlyRate) &amp;gt; 0 Then
   ...
   emp.OTPay = CalculateOTPay(HoursWorked, HourlyRate)
Else 
   emp.OTPay = 0
End If
&lt;/pre&gt; &lt;/p&gt;
&lt;p&gt;&lt;pre name="62a6f0e03ac149cba64e45faf2e18e41" class="VB.NET"&gt;
Dim PayOT As Decimal = CalculateOTPay(HoursWorked, HourlyRate)
If (PayOT &amp;gt; 0) Then
   ...
   emp.OTPay = PayOT
Else 
   emp.OTPay = 0
End If
&lt;/pre&gt; &lt;/p&gt;
&lt;p&gt;It&amp;#39;s really important to avoid the first usage.&amp;nbsp;&amp;nbsp;Running complicated functions multiple times can be expensive in run time.&amp;nbsp; And worse, in some cases you may get a different answer!&amp;nbsp; For example, if&amp;nbsp;the function returns &amp;quot;seats available&amp;quot; and someone else just booked a seat.&amp;nbsp; Or if the date changes.&amp;nbsp; Or if the function communicates with&amp;nbsp;a sensitive&amp;nbsp;hardware gauge that measures continually changing values like network traffic or&amp;nbsp;conveyor belt speed.&amp;nbsp; Trust me, there are more examples.&lt;/p&gt;
&lt;p&gt;Thanks for reading.&amp;nbsp; Happy new year to all.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
dp.SyntaxHighlighter.ClipboardSwf = &amp;#39;/dp.SyntaxHighlighter/Scripts/clipboard.swf&amp;#39;
dp.SyntaxHighlighter.HighlightAll(&amp;#39;bf3f5a5690bc431a81b75d45c0f16541&amp;#39;)
dp.SyntaxHighlighter.HighlightAll(&amp;#39;cb70c37c3eae481dae623c8bdae8b589&amp;#39;)
dp.SyntaxHighlighter.HighlightAll(&amp;#39;28eee3a2acd3407aa24e9f14a31358cd&amp;#39;)
dp.SyntaxHighlighter.HighlightAll(&amp;#39;c8b570286b44416685b70c8be5dc559d&amp;#39;)
dp.SyntaxHighlighter.HighlightAll(&amp;#39;ccd27b57d5054c1ebd0e1493e026d1ac&amp;#39;)
dp.SyntaxHighlighter.HighlightAll(&amp;#39;1b45da8381da4d6884b21f31f05f0139&amp;#39;)
dp.SyntaxHighlighter.HighlightAll(&amp;#39;62a6f0e03ac149cba64e45faf2e18e41&amp;#39;)

&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=705455" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/1mALWxG_4jY" height="1" width="1"/&gt;</description><category domain="http://vbcity.com/blogs/larryblake/archive/tags/Visual+Basic/default.aspx">Visual Basic</category><category domain="http://vbcity.com/blogs/larryblake/archive/tags/VB/default.aspx">VB</category><category domain="http://vbcity.com/blogs/larryblake/archive/tags/Beginner/default.aspx">Beginner</category><feedburner:origLink>http://vbcity.com/blogs/larryblake/archive/2011/12/30/three-and-a-half-simple-tips.aspx</feedburner:origLink></item><item><title>How to populate a combo box with an enumeration</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/P26QYsRVgIw/how-to-populate-a-combo-box-with-an-enumeration.aspx</link><pubDate>Tue, 27 Dec 2011 15:01:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:705414</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 is one of those simple things that comes into the category of “I know how to do it, but, umm, it’s been a while, and now I’ve forgotten how!”&lt;/p&gt;  &lt;p&gt;So, here’s an enumeration of strings:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff"&gt;Public Enum QuestionCategory     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Art      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Music      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; People      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Places      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Science      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Words      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Miscellaneous      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; BeginsWith      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Inventions      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; History      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Animals      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Legends      &lt;br /&gt;End Enum      &lt;br /&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;If I want to populate a combo box named cboCategories with these strings, I can use the following code:&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160; &lt;font color="#0000ff"&gt;Me.cboCategories.DataSource = System.Enum.GetValues(GetType(QuestionCategory))&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Then, when I want to know the user’s selection, I use:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Me.cboCategories.SelectedValue.ToString()&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000000"&gt;That’s it! &lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=705414" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/P26QYsRVgIw" height="1" width="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/Visual+Basic/default.aspx">Visual Basic</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Enumeration/default.aspx">Enumeration</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/ComboBox/default.aspx">ComboBox</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/12/27/how-to-populate-a-combo-box-with-an-enumeration.aspx</feedburner:origLink></item><item><title>VS 11 Developer Preview(s)</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/Lqrg3eLJvO8/vs-11-developer-preview-s.aspx</link><pubDate>Tue, 20 Dec 2011 12:08:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:705358</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;The descriptions on Microsoft&amp;rsquo;s site don&amp;rsquo;t make it very clear that there are two separate versions of the Visual Studio 11 preview that you can download and try. The one you&amp;rsquo;ll probably find first is the almost complete VS preview &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27543"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This is pretty much the whole package in terms of the changes they&amp;rsquo;ve made to the Windows 7/Win 32 base version. The best advice generally is to install it somewhere other than your main system &amp;ndash; a separate partition, on a spare machine, or in a virtual machine. VMware and Virtual Box both seem to do the job nicely. &lt;/p&gt;
&lt;p&gt;Just for the heck of it, I have tried installing a copy on my main machine, and uninstalled it without causing any problems to the original VS 2010 that was also installed. But that&amp;rsquo;s no guarantee that it&amp;rsquo;ll work as smoothly for you, so as they used to say on Hill Street Blues, &amp;ldquo;Be careful out there&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;I said &amp;lsquo;pretty much&amp;rsquo; the whole package, but of course the much talked about Metro UI is a Windows 8 feature and so you can&amp;rsquo;t get at it with the Windows 7 based preview. If you want to play with Metro then you need to download the much bigger package from &lt;a href="http://msdn.microsoft.com/en-us/windows/apps/br229516"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now this is an installation that you &lt;i&gt;definitely&lt;/i&gt; want to keep well away from your workaday system. Again, most people seem to install this in a virtual machine and the most successful one seems to be &lt;a href="https://www.virtualbox.org/wiki/Downloads"&gt;Oracle&amp;rsquo;s Virtual Box.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Although I&amp;rsquo;ve used both VMWare and Virtual Box extensively in the past, and found them both to be excellent, intuitive tools, my Windows 8 experience wasn&amp;rsquo;t quite so good. I&amp;rsquo;m sure this has more to do with the fact that it&amp;rsquo;s a developer preview than any fault with the VMs. But I had issues with speed (not unusual for VMs, but more obvious on this occasion) and also some problems with display resolution and size. &lt;/p&gt;
&lt;p&gt;So I thought I&amp;rsquo;d try and find an alternative that still let me use my main developer machine. Dual booting usually leads to frustration, and is especially tricky when one of the OSs isn&amp;rsquo;t fully baked. And you wouldn&amp;#39;t get good odds on a clean and successful uninstall at some later stage either. The route I took was to use the &lt;a href="http://www.paragon-software.com/home/hdm-personal/"&gt;Hard Disk Manager 11 Suite&lt;/a&gt; from Paragon-Software.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The key feature of this tool that I needed was the ability to hide a partition. That way I could have Win 7 on one partition, Win 8 on another, and when I booted up the system only the one I wanted at that particular time would be visible to the system.&lt;/p&gt;
&lt;p&gt;And so that&amp;rsquo;s what I did &amp;ndash; created two partitions on the drive, one of them hidden. I then installed Windows 8 preview on the second partition. I used a DVD I&amp;lsquo;d created from the original downloaded iso to install the Windows 8 OS. One thing I did find is that I had to leave this disk in the tray when I rebooted, although I ignored the &amp;lsquo;Boot from CD/DVD&amp;rsquo; message each time I ran it after the first installation. I really don&amp;rsquo;t know why it seemed to need that, but anyway, it worked OK.&lt;/p&gt;
&lt;p&gt;When I wanted to go back to my Windows 7 installation, I rebooted with the Paragon Recovery Disk in the CD tray (you&amp;rsquo;ve got a recovery disk right?). The Paragon menu software then let me switch the partitions back (i.e. hiding the Windows 8 one, and making the Windows 7 one available).&lt;/p&gt;
&lt;p&gt;Of course, this only works well if you&amp;rsquo;re happy to be restricted to the one OS at a time, which I was. Although I have the full Paragon Hard Disk Manager 11 Suite, which is an exceptionally useful tool, you can in fact create and hide partitions with their free version of Partition Manager (although currently on offer at $9.95 it probably wouldn&amp;rsquo;t break the bank to get the retail version, which has more features).&lt;/p&gt;
&lt;p&gt;Windows 8, with its Metro UI tiles, is interesting, but takes some getting used to. The &amp;lsquo;immersive&amp;rsquo; experience, that Microsoft like to talk up, generally means that you find yourself searching around for some way to minimize, resize, or even close down a running Metro UI app. Like most people, I find the way it switches back to desktop (Windows 7) mode a bit jarring, although I realise that you can&amp;rsquo;t magically &amp;ldquo;Metrofy&amp;rdquo; all those old applications.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m most interested in seeing if all those long hours I&amp;rsquo;ve put in learning WPF and Silverlight over the past few years will be useful for creating Metro apps with XAML. I&amp;rsquo;m optimistic that they will be. The namespaces and classes may have changed, but the underlying approach seems very similar.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=705358" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/Lqrg3eLJvO8" height="1" width="1"/&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/VS+11/default.aspx">VS 11</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Paragon+Hard+Disk+Manager/default.aspx">Paragon Hard Disk Manager</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Developer+Preview/default.aspx">Developer Preview</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/VMWare/default.aspx">VMWare</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Windows+8/default.aspx">Windows 8</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Virtual+Box/default.aspx">Virtual Box</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/12/20/vs-11-developer-preview-s.aspx</feedburner:origLink></item><item><title>Book Review:  Murach's ASP.NET 4 Web Programming with VB 2010</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/938s9ZyD9CA/book-review-murach-s-asp-net-4-web-programming-with-vb-2010.aspx</link><pubDate>Mon, 19 Dec 2011 18:59:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:702086</guid><dc:creator>Matt</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;strong&gt;&lt;span style="font-size:medium;"&gt;Murach&amp;#39;s ASP.NET 4 Web Programming with VB 2010&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is&amp;nbsp;the first Murach book that I have the chance to read and I love the &amp;quot;paired-paged&amp;quot; layout, the organisation and the overall flow of the book.&lt;/p&gt;
&lt;p&gt;The paired-paged layout gives the reader information and explanations on the left side and code samples, guidelines, descriptions on the right.&lt;/p&gt;
&lt;p&gt;Starting out the book takes you through the basics of ASP.NET and moves you right into real world scenerios and concepts.&lt;/p&gt;
&lt;p&gt;Reaching&amp;nbsp;the end of the book you will have learned a great deal about the core concepts of asp.net, making web applications responsive and secure.&lt;/p&gt;
&lt;p&gt;By&amp;nbsp;far one of the best programming books on my shelf. &lt;/p&gt;
&lt;p&gt;No matter the skill level, this book is a great read and full of knowledge!&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://murach.com/books/a4vb/index.htm"&gt;Click here&lt;/a&gt; to check out the page on Murach&amp;#39;s website.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Thanks Murach!!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=702086" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/938s9ZyD9CA" height="1" width="1"/&gt;</description><category domain="http://vbcity.com/blogs/coldfiretech/archive/tags/Murach/default.aspx">Murach</category><category domain="http://vbcity.com/blogs/coldfiretech/archive/tags/AJAX/default.aspx">AJAX</category><category domain="http://vbcity.com/blogs/coldfiretech/archive/tags/WCF/default.aspx">WCF</category><category domain="http://vbcity.com/blogs/coldfiretech/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://vbcity.com/blogs/coldfiretech/archive/tags/ASP.NET/default.aspx">ASP.NET</category><feedburner:origLink>http://vbcity.com/blogs/coldfiretech/archive/2011/12/19/book-review-murach-s-asp-net-4-web-programming-with-vb-2010.aspx</feedburner:origLink></item><item><title>How to identify an item or sub item the users selects in a ListView</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/IlgfVZ1EMh8/how-to-identify-an-item-or-sub-item-the-users-selects-in-a-listview.aspx</link><pubDate>Mon, 05 Dec 2011 17:29:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:705190</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction      &lt;br /&gt;&lt;/u&gt;Here’s a little ListView trick that might come in useful sometimes.&amp;#160; The user selects an individual sub item in a list view and you want to know what they selected.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The code      &lt;br /&gt;&lt;/u&gt;You can code the MouseDown event of the ListView to identify the selection:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; usersSelection &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt; = &lt;span style="color:blue;"&gt;String&lt;/span&gt;.Empty&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; ListView1_MouseDown(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Windows.Forms.&lt;span style="color:#2b91af;"&gt;MouseEventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; ListView1.MouseDown&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; info &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListViewHitTestInfo&lt;/span&gt; = ListView1.HitTest(e.X, e.Y)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; usersSelection = info.SubItem.ToString&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; usersSelection = usersSelection.Substring(18)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; usersSelection = usersSelection.Substring(0, usersSelection.Length - 1)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Me&lt;/span&gt;.TextBox1.Text = usersSelection&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;br /&gt;  &lt;p&gt;&lt;u&gt;How it works      &lt;br /&gt;&lt;/u&gt;The first line creates a String variable that will store the user’s selection.&lt;/p&gt;  &lt;p&gt;Then, in the MouseDown event, an instance of the ListViewHitTestInfo class named ‘info’ is created. This object can return information about the ListView if it’s passed information such as the current location of the cursor.&amp;#160; That’s where the ListView’s HitTest function is used and, as you can see, this function is passed in the current X and Y points of the mouse cursor (e,X and e.Y).&amp;#160; Alternatively, you can pass in e.Location in place of the X and Y values and this seems to work just as well.&lt;/p&gt;  &lt;p&gt;In the next few lines, I’ve purposely broken the code into three steps just to make it easier to read.&amp;#160; You could of course put this all in one line of code. It works like this:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Firstly, it gets the SubItem that the cursor is currently over. &lt;/li&gt;    &lt;li&gt;Then – because this particular property happens to return some additional information that we don’t need – the code strips off the first 18 characters.&amp;#160; (If you’re curious, you can comment out this line) &lt;/li&gt;    &lt;li&gt;For the same reason, the code removes the final characters from the string that the ListViewHitTestInfo returns. &lt;/li&gt;    &lt;li&gt;For demo purposes, this code sample then displays the selection in a text box. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;u&gt;The result&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;Here’s an example that shows this in action:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3603.ListViewHitTest_5F00_01BACDD5.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ListViewHitTest" border="0" alt="ListViewHitTest" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6232.ListViewHitTest_5F00_thumb_5F00_27342569.png" width="665" height="480" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;If the full row selection is confusing, you can turn it off in the properties window. That way, it’s a bit more obvious where the user is clicking:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2625.ListViewHitTestAlt_5F00_7785E10F.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ListViewHitTestAlt" border="0" alt="ListViewHitTestAlt" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2804.ListViewHitTestAlt_5F00_thumb_5F00_6F7E18E0.png" width="672" height="486" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You can see the cursor position here.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;u&gt;Summary      &lt;br /&gt;&lt;/u&gt;This code returns items (i.e. column 1 text) and sub items (any other column).&amp;#160; If – as in my example – you let the user see an area of the list view that doesn’t contain a column (i.e. the blank area at the right hand side), you’ll need to code agains the user clicking in that area.&amp;#160; As it stands, the system will crash. You can do this very simply by testing with:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&lt;span style="color:blue;"&gt;If&lt;/span&gt; &lt;span style="color:blue;"&gt;Not&lt;/span&gt; info.SubItem &lt;span style="color:blue;"&gt;Is&lt;/span&gt; &lt;span style="color:blue;"&gt;Nothing&lt;/span&gt; &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;&amp;#160;&amp;#160; . . .&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&amp;#160; It’s also possible to include code that returns the index number of the selected column text of the column header of the selected column, although that’s not really a requirement you’re likely to need often.&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=705190" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/IlgfVZ1EMh8" height="1" width="1"/&gt;</description><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/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/ListView/default.aspx">ListView</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/Hit+Test/default.aspx">Hit Test</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/12/05/how-to-identify-an-item-or-sub-item-the-users-selects-in-a-listview.aspx</feedburner:origLink></item><item><title>How to read text file content into a List (of String)</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/TKlA2_1D3PM/how-to-read-text-file-content-into-a-list-of-string.aspx</link><pubDate>Mon, 28 Nov 2011 16:19:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:705179</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction&lt;/u&gt;     &lt;br /&gt;I was looking through some questions I answered a while back and came across this problem where someone wanted to read text from a file, strip out the commas, and then store the individual items in an in-memory List.&amp;#160; Here’s the way I did it:&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The text file      &lt;br /&gt;&lt;/u&gt;Here’s the structure of a text file that we want to use.&amp;#160; This is just an example:&lt;/p&gt;  &lt;p&gt;All red berries, Blackcurrant, Strawberry, Cranberry    &lt;br /&gt;Banana, Pineapple,     &lt;br /&gt;&amp;#160; Rhubarb, Pear, Granny Smith Apple     &lt;br /&gt;Other apple,&amp;#160;&amp;#160;&amp;#160;&amp;#160; cherry, damson     &lt;br /&gt;grape     &lt;br /&gt;plum, mandarin orange&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Code that reads the file      &lt;br /&gt;&lt;/u&gt;The first step is to create the List that will hold the items: &lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; stockList &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color:blue;"&gt;Of&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Next, we need to access the text file and set up some variables that we’ll use to store the data as we process it:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; file_name &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt; = &lt;span style="color:#a31515;"&gt;&amp;quot;C:\Temp\stockList.txt&amp;quot;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; stream_reader &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; IO.&lt;span style="color:#2b91af;"&gt;StreamReader&lt;/span&gt;(file_name)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; line &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; ReadResult() &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;The first line identifies the path and name of the source file. The second line creates a StreamReader that will be used to get the text from the source file.&amp;#160; If you use an Imports statement for System.IO at the top of the file, you can drop the “IO.” part of the code.&lt;/p&gt;  &lt;p&gt;The variable named “line” will hold all the characters in each line of the file as it’s being read. Finally, the array “ReadResult” will temporarily hold each sub-string from a line of text in the file. So, to take the first line as an example, ReadResult would hold four strings - &lt;/p&gt;  &lt;p&gt;All red berries    &lt;br /&gt;Blackcurrant     &lt;br /&gt;Strawberry     &lt;br /&gt;Cranberry&lt;/p&gt;  &lt;p&gt;Now, the code for the reading, processing, and adding to the List tasks:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Read the file one line at a time.&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; line = stream_reader.ReadLine()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Do&lt;/span&gt; &lt;span style="color:blue;"&gt;While&lt;/span&gt; &lt;span style="color:blue;"&gt;Not&lt;/span&gt; (line &lt;span style="color:blue;"&gt;Is&lt;/span&gt; &lt;span style="color:blue;"&gt;Nothing&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&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; &lt;span style="color:green;"&gt;&amp;#39; Trim&amp;#160; the line in case there are any leading or trailing spaces in file line&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&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; line = line.Trim()&lt;/p&gt;    &lt;p style="margin:0px;"&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; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; split using comma chars&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&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; ReadResult = line.Split(&lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&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; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Populate List&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&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; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; ReadResult.Length - 1&lt;/p&gt;    &lt;p style="margin:0px;"&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; &lt;span style="color:blue;"&gt;If&lt;/span&gt; ReadResult(i).Length &amp;gt; 0 &lt;span style="color:blue;"&gt;Then&lt;/span&gt; stockList.Add(ReadResult(i).Trim.ToString)&lt;/p&gt;    &lt;p style="margin:0px;"&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; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&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; &lt;span style="color:green;"&gt;&amp;#39; Move on to the next line.&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&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; line = stream_reader.ReadLine()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Loop&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The ReadLine method does what you’d expect, and reads a complete line from the file. It then stores that line in the String variable named “line”.&lt;/p&gt;  &lt;p&gt;Next, the Do While Loop first removes any leading or trailing spaces from the line.&amp;#160; Don’t be confused here:&amp;#160; This only affects spaces at the very beginning of line, such as the third line in the file that has spaces before the first word, or spaces at the very end of the line.&lt;/p&gt;  &lt;p&gt;The Split function reads the line of text and splits it up whenever it finds a comma.&amp;#160; It then assigns each chunk of text to one of the elements in the ReadResult array.&lt;/p&gt;  &lt;p&gt;Now that ReadResult has the split contents of the line, the next step is pass those strings to the List. The code block begins with a For statement that ensures that each element of the ReadResult array will be read in turn.&amp;#160; &lt;/p&gt;  &lt;p&gt;The next line starts with an If statement and might look&amp;#160; a bit complicated.&amp;#160; But really all it does is:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Checks that the element isn’t empty. (This can happen, for instance, if the line ends with a comma – which line 2 does) &lt;/li&gt;    &lt;li&gt;Then adds that element to the List …. &lt;/li&gt;    &lt;li&gt;….While at the same time removing any unwanted spaces from the string (for example the spaces in front of cherry in the fourth line). &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The last action in the loop is to move the reader down to the next line in the text file.&lt;/p&gt;  &lt;p&gt;Finally in the reading and processing stage, the StreamReader is closed and a message box is used if there’s any problem accessing the file or processing the text:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Catch&lt;/span&gt; exc &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Report all errors.&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&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; &lt;span style="color:#2b91af;"&gt;MessageBox&lt;/span&gt;.Show(exc.Message.ToString, &lt;span style="color:#a31515;"&gt;&amp;quot;Error&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;MessageBoxButtons&lt;/span&gt;.OK, &lt;span style="color:#2b91af;"&gt;MessageBoxIcon&lt;/span&gt;.Error)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;h1&gt;&lt;u&gt;&lt;/u&gt;&lt;/h1&gt;   &lt;u&gt;&lt;/u&gt;&lt;/div&gt;  &lt;p&gt;&lt;u&gt;Displaying the result      &lt;br /&gt;&lt;/u&gt;For the purposes of demonstration, I’ve added code that will fill a list box with the individual items: &lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&lt;u&gt;&lt;/u&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt; &lt;/div&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;For&lt;/span&gt; &lt;span style="color:blue;"&gt;Each&lt;/span&gt; Str &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt; &lt;span style="color:blue;"&gt;In&lt;/span&gt; stockList&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ListBox1.Items.Add(Str)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The result in the windows form when this code runs 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/4380.ReadTextFileToList_5F00_7FA6D525.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ReadTextFileToList" border="0" alt="ReadTextFileToList" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8244.ReadTextFileToList_5F00_thumb_5F00_237F86B3.png" width="425" height="443" /&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=705179" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/TKlA2_1D3PM" height="1" width="1"/&gt;</description><category domain="http://vbcity.com/blogs/xtab/archive/tags/Text/default.aspx">Text</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/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/StreamReader/default.aspx">StreamReader</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Split+function/default.aspx">Split function</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/11/28/how-to-read-text-file-content-into-a-list-of-string.aspx</feedburner:origLink></item><item><title>Metro Visual Basic Hello World Application</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/1rYxIhSVgC4/metro-visual-basic-hello-world-application.aspx</link><pubDate>Sun, 27 Nov 2011 14:11:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:705078</guid><dc:creator>Mike McIntyre</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Looking for a hello world app for Metro written with Visual Basic?&lt;/p&gt;...(&lt;a href="http://vbcity.com/blogs/mike-mcintyre/archive/2011/11/27/metro-visual-basic-hello-world-application.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://vbcity.com/aggbug.aspx?PostID=705078" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/1rYxIhSVgC4" height="1" width="1"/&gt;</description><category domain="http://vbcity.com/blogs/mike-mcintyre/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://vbcity.com/blogs/mike-mcintyre/archive/tags/Metro/default.aspx">Metro</category><feedburner:origLink>http://vbcity.com/blogs/mike-mcintyre/archive/2011/11/27/metro-visual-basic-hello-world-application.aspx</feedburner:origLink></item><item><title>How to compress the size of an image file at runtime</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/TwGfZIiOFUA/how-to-compress-the-size-of-an-image-file-at-runtime.aspx</link><pubDate>Mon, 14 Nov 2011 16:23:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704915</guid><dc:creator>Ged Mead</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction      &lt;br /&gt;&lt;/u&gt;This came up as a question on VB City recently, where the poster wanted to reduce image file sizes before saving to a database as a BLOB. It turns out that it’s easier to do than you might think.&amp;#160; If you have an Image object, you can use its Save method to store it to your hard drive or other storage media.&amp;#160; At the same time, you can use an encoder parameter that sets the quality of the saved image. &lt;/p&gt;  &lt;p&gt;Here’s how to do it:&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The core code      &lt;br /&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;This method takes the image to be compressed, the path and file name that you want to use for the save, and a quality level between 1 and 100.&amp;#160; The lower the value of the quality level parameter, the greater the compression.&amp;#160; Of course, as you increase the compression (i.e. the lower the number), the quality of the saved file will reduce.&amp;#160; So you’ll often have to experiment to find the best compromise between size and quality.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; CompressAndSaveImage(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; img &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Image&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; fileName &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; quality &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Long&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; parameters &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;EncoderParameters&lt;/span&gt;(1)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; parameters.Param(0) = &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;EncoderParameter&lt;/span&gt;(System.Drawing.Imaging.&lt;span style="color:#2b91af;"&gt;Encoder&lt;/span&gt;.Quality, quality)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; img.Save(fileName, GetCodecInfo(&amp;quot;image/jpeg&amp;quot;), parameters)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;You’ll see that the second parameter of the Save method calls another method called GetCodecInfo and passes in a string that represents a mime type.&amp;#160; Here’s that second method, which loops through all image encoders on the system and returns the one that’s required for the selected image type.&amp;#160; In the event that a non-existent image type is passed in to this method, an error is thrown.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Shared&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt; GetCodecInfo(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; mimeType &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;) &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ImageCodecInfo&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;For&lt;/span&gt; &lt;span style="color:blue;"&gt;Each&lt;/span&gt; encoder &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ImageCodecInfo&lt;/span&gt; &lt;span style="color:blue;"&gt;In&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ImageCodecInfo&lt;/span&gt;.GetImageEncoders()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; encoder.MimeType = mimeType &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Return&lt;/span&gt; encoder&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Next&lt;/span&gt; encoder&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Throw&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color:blue;"&gt;String&lt;/span&gt;.Format(&lt;span style="color:#a31515;"&gt;&amp;quot;&amp;#39;{0}&amp;#39; not supported&amp;quot;&lt;/span&gt;, mimeType))&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Finally, you call the compress and save image method, passing in the required arguments.&amp;#160; For example:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;CompressAndSaveImage(selectedImage, reducedImage, 50)&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;where selectedImage is an image object, reducedImage is a string that defines the path and file name to save, and 50 represents the quality level.&lt;/p&gt;  &lt;p&gt;So, that’s all the core code you need to compress an image file.&amp;#160; Let’s take a look at how you can make it available to your users.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Adding a user interface      &lt;br /&gt;&lt;/u&gt;Here’s one way of using it:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7635.ImageCompress_5F00_15607047.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ImageCompress" border="0" alt="ImageCompress" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0564.ImageCompress_5F00_thumb_5F00_6D0D29F3.png" width="468" height="352" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The key controls on this form are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;A NumericUpDown named qualityLevel &lt;/li&gt;    &lt;li&gt;A button named btnCompress &lt;/li&gt;    &lt;li&gt;Two&amp;#160; picture boxes named PictureBox1 and PictureBox2 &lt;/li&gt;    &lt;li&gt;An OpenFileDialog named OpenFileDialog1 &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The code is as follows:&lt;/p&gt;  &lt;p&gt;First, add Imports statements :&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160; Imports System.Drawing.Imaging    &lt;br /&gt;&amp;#160;&amp;#160; Imports System.IO&lt;/p&gt;  &lt;p&gt;Then, declare a form level variable to keep track of the user’s choice of compression/quality level:&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; level &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Long&lt;/span&gt; = 50&lt;/p&gt;  &lt;p&gt;Catch the user’s choice of quality level when it changes from the default of 50:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; qualityLevel_ValueChanged(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; qualityLevel.ValueChanged&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; btnCompress.Enabled = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; level = &lt;span style="color:blue;"&gt;CLng&lt;/span&gt;(qualityLevel.Value)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Finally, when the user clicks the button, do the following:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Display the open file dialog &lt;/li&gt;    &lt;li&gt;If the user chooses a file, create an image using the selected file as the source. &lt;/li&gt;    &lt;li&gt;Display the image in its original form in the first picture box. &lt;/li&gt;    &lt;li&gt;Create a file name for the compressed file. &lt;/li&gt;    &lt;li&gt;Call the compress and save method on that image. &lt;/li&gt;    &lt;li&gt;Show the user that something’s happened by displaying the reduced image in the second picture box. This also lets them check the quality of the reduced size image. &lt;/li&gt; &lt;/ol&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; btnCompress_Click(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; btnCompress.Click&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Get user&amp;#39;s choice of file&lt;/span&gt;&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; OpenFileDialog1.ShowDialog = Windows.Forms.&lt;span style="color:#2b91af;"&gt;DialogResult&lt;/span&gt;.OK &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; selectedImage &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Image&lt;/span&gt; = &lt;span style="color:#2b91af;"&gt;Image&lt;/span&gt;.FromFile(OpenFileDialog1.FileName)&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Display it&lt;/span&gt;&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PictureBox1.Image = &lt;span style="color:#2b91af;"&gt;Image&lt;/span&gt;.FromFile(OpenFileDialog1.FileName)&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Create a name for the reduced file&lt;/span&gt;&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; reducedImage &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt; = &lt;span style="color:#a31515;"&gt;&amp;quot;C:\Temp\Reduced_&amp;quot;&lt;/span&gt; &amp;amp; OpenFileDialog1.SafeFileName&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Compress and save the reduced file&lt;/span&gt;&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CompressAndSaveImage(selectedImage, reducedImage, level)&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Display the reduced image so that user can compare quality&lt;/span&gt;&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PictureBox2.Image = &lt;span style="color:#2b91af;"&gt;Image&lt;/span&gt;.FromFile(reducedImage)&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PictureBox2.SizeMode = &lt;span style="color:#2b91af;"&gt;PictureBoxSizeMode&lt;/span&gt;.StretchImage&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;  &lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Final Note      &lt;br /&gt;&lt;/u&gt;That works well, although you still need to build in real world checks, such as making sure the user selects an image file type from the open file dialog, and so on. Another small problem is that the code will fail if the reduced file name already exists. You can code round this in several ways, according to your preference, but one way would be simply to tell the user about the problem:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; &lt;span style="color:blue;"&gt;Not&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;File&lt;/span&gt;.Exists(fileName) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; img.Save(fileName, GetCodecInfo(&lt;span style="color:#a31515;"&gt;&amp;quot;image/jpeg&amp;quot;&lt;/span&gt;), parameters)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Else&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#2b91af;"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;Reduced file already exists.&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;You could extend this code so that it either automatically deletes an existing file or asks the user to authorise the deletion before proceeding.&amp;#160; I’d also be tempted to add a couple more labels to the form and show the user the original file size and the reduced file size. You can use FileInfo objects for this.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;u&gt;Summary     &lt;br /&gt;&lt;/u&gt;This is a useful little procedure that you can use when you want to reduce the size of an image file, but want to be able to judge the loss of quality.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704915" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/TwGfZIiOFUA" height="1" width="1"/&gt;</description><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/.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/Image/default.aspx">Image</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/11/14/how-to-compress-the-size-of-an-image-file-at-runtime.aspx</feedburner:origLink></item><item><title>Web Essentials - a look at the VS Extension</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/3lIZpw91szk/web-essentials-a-look-at-the-vs-extension.aspx</link><pubDate>Sat, 12 Nov 2011 21:09:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704840</guid><dc:creator>Matt</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Recently I was browsing through the extensions available for Visual Studio when I came across this handly little extension called &lt;em&gt;Web Essentials.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Ive been using this extension for a few weeks now and figured it was time for me to share the wealth.&lt;/p&gt;
&lt;p&gt;For any web developer this extension is a nice tool to have in your arsenal, I know I enjoy having it in mine.&lt;/p&gt;
&lt;p&gt;A few features of the&amp;nbsp;&lt;em&gt;Web Essentials&lt;/em&gt; extension that&amp;nbsp;I&amp;nbsp;favor the most are&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Live Web Preview, right in Visual Studio.&amp;nbsp; (Allows you to preview your site in real time as your editing it)&lt;/li&gt;
&lt;li&gt;Support for Live CSS Updating in the Live Web Preview&amp;nbsp;(Allows you to preview css changes to your site as you make them)&lt;/li&gt;
&lt;li&gt;The Color Glyph (Shows a preview of color codes in your text editor.)&lt;br /&gt;&lt;img height="82" width="187" src="http://vbcity.com/cfs-filesystemfile.ashx/__key/CommunityServer.Components.UserFiles/00.00.04.84.71/colorglyph.bmp" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;The ability to add Javascript and CSS file directly from the context menu in the solution explorer.&lt;br /&gt;&lt;img height="194" width="625" src="http://vbcity.com/cfs-filesystemfile.ashx/__key/CommunityServer.Components.UserFiles/00.00.04.84.71/addcssorjs.bmp" alt="" /&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If your interested in checking out the full list of features you can find the documentation &lt;a target="_blank" href="http://visualstudiogallery.msdn.microsoft.com/6ed4c78f-a23e-49ad-b5fd-369af0c2107f?SRC=Home"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Happy Coding!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704840" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/3lIZpw91szk" height="1" width="1"/&gt;</description><category domain="http://vbcity.com/blogs/coldfiretech/archive/tags/Extensions/default.aspx">Extensions</category><category domain="http://vbcity.com/blogs/coldfiretech/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://vbcity.com/blogs/coldfiretech/archive/tags/web/default.aspx">web</category><feedburner:origLink>http://vbcity.com/blogs/coldfiretech/archive/2011/11/12/web-essentials-a-look-at-the-vs-extension.aspx</feedburner:origLink></item><item><title>How to Bold text in a Windows Forms ListView</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/jarVNObfCn0/how-to-bold-text-in-a-windows-forms-listview.aspx</link><pubDate>Mon, 07 Nov 2011 08:39:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704161</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Introduction &lt;br /&gt;&lt;/span&gt;Sometimes you&amp;rsquo;ll want to highlight items in a ListView to grab the user&amp;rsquo;s attention. One way you can do this is to programmatically apply Bold font weight to text.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Bold all items in the first column &lt;br /&gt;&lt;/span&gt;Let&amp;rsquo;s say you have a ListView like the one below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8468.ListViewBold1_5F00_436207C5.png"&gt;&lt;img height="238" width="594" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0647.ListViewBold1_5F00_thumb_5F00_5C4DB23D.png" alt="ListViewBold1" border="0" title="ListViewBold1" style="display:inline;border-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;To bold all the text in the first column is particularly easy. There are only two steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Set the UseItemStyleForSubItems property to False so that only the first column is affected. &lt;/li&gt;
&lt;li&gt;Enumerate through the ListView items and apply Bold font to the ListViewItem by its name (lvi): &lt;/li&gt;
&lt;/ol&gt;
&lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; &lt;span style="color:blue;"&gt;Each&lt;/span&gt; lvi &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListViewItem&lt;/span&gt; &lt;span style="color:blue;"&gt;In&lt;/span&gt; ListView1.Items&lt;/p&gt;
&lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lvi.UseItemStyleForSubItems = &lt;span style="color:blue;"&gt;False&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; lvi.SubItems(0).Font = New Font(ListView1.Font, FontStyle.Bold)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lvi.Font = &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Font&lt;/span&gt;(ListView1.Font, &lt;span style="color:#2b91af;"&gt;FontStyle&lt;/span&gt;.Bold)&lt;/p&gt;
&lt;p style="margin:0px;font-family:consolas;background:white;color:black;font-size:10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7737.ListViewBold2_5F00_0377D5A6.png"&gt;&lt;img height="235" width="595" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/5584.ListViewBold2_5F00_thumb_5F00_6A6BDF93.png" alt="ListViewBold2" border="0" title="ListViewBold2" style="display:inline;border-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Using &amp;lsquo;lvi.Font&amp;rsquo; in this example is effectively the same as using lvi.SubItems(0).Font. So, this would work just as well:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; &lt;span style="color:blue;"&gt;Each&lt;/span&gt; lvi &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListViewItem&lt;/span&gt; &lt;span style="color:blue;"&gt;In&lt;/span&gt; ListView1.Items&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lvi.UseItemStyleForSubItems = &lt;span style="color:blue;"&gt;False&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lvi.SubItems(0).Font = &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Font&lt;/span&gt;(ListView1.Font, &lt;span style="color:#2b91af;"&gt;FontStyle&lt;/span&gt;.Bold)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39;lvi.Font = New Font(ListView1.Font, FontStyle.Bold)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;Bold all items in a selected column&lt;/span&gt; &lt;br /&gt;The second example above should make it clear what we need to do to bold one of the other columns. You simply change the index of the target column and use the same code:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; &lt;span style="color:blue;"&gt;Each&lt;/span&gt; lvi &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListViewItem&lt;/span&gt; &lt;span style="color:blue;"&gt;In&lt;/span&gt; ListView1.Items&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lvi.SubItems(2).Font = &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Font&lt;/span&gt;(ListView1.Font, &lt;span style="color:#2b91af;"&gt;FontStyle&lt;/span&gt;.Bold)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The result will be:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8322.listviewbold3_5F00_354F2A96.png"&gt;&lt;img height="234" width="605" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7180.listviewbold3_5F00_thumb_5F00_353F04C9.png" alt="listviewbold3" border="0" title="listviewbold3" style="display:inline;border-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Bold only items that contain a search term &lt;br /&gt;&lt;/span&gt;Let&amp;rsquo;s take a scenario where you want to highlight all items in the second column that contain the string &amp;lsquo;web&amp;rsquo; as part of its text.&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; &lt;span style="color:blue;"&gt;Each&lt;/span&gt; lvi &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListViewItem&lt;/span&gt; &lt;span style="color:blue;"&gt;In&lt;/span&gt; ListView1.Items&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; lvi.SubItems(1).Text.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;web&amp;quot;&lt;/span&gt;) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lvi.UseItemStyleForSubItems = &lt;span style="color:blue;"&gt;False&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lvi.SubItems(1).Font = &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Font&lt;/span&gt;(ListView1.Font, &lt;span style="color:#2b91af;"&gt;FontStyle&lt;/span&gt;.Bold)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;In my example, the result will be:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0412.Listviewbold4_5F00_0DF495C7.png"&gt;&lt;img height="234" width="605" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/5633.Listviewbold4_5F00_thumb_5F00_46FB4CFC.png" alt="Listviewbold4" border="0" title="Listviewbold4" style="display:inline;border:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Of course, you could use this kind of code to search for an exact entry, not just a substring. In that case, you&amp;rsquo;d use something like &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If lvi.SubItems(1).Text.Contains(&amp;quot; (&lt;a href="mailto:fredi@gerts.ch"&gt;fredi@gerts.ch&lt;/a&gt; )&amp;rdquo;) Then&lt;/p&gt;
&lt;p&gt;But bear in mind that spaces might mean that sometimes you don&amp;rsquo;t get a match you think exists.&amp;nbsp; You can use the Trim function to strip out unimportant space characters.&lt;/p&gt;
&lt;p&gt;And you can search through the whole ListView for a partial or exact match if you tweak the above code a little.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704161" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/jarVNObfCn0" height="1" width="1"/&gt;</description><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/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/ListView/default.aspx">ListView</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/Search/default.aspx">Search</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/11/07/how-to-bold-text-in-a-windows-forms-listview.aspx</feedburner:origLink></item><item><title>How To Search for Multiple Occurrences of a String in a ListView</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/9C3qPZCD3ig/a-search-for-multiple-occurrences-of-a-string-in-a-windows-forms-listview.aspx</link><pubDate>Mon, 31 Oct 2011 18:11:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:703993</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Introduction &lt;br /&gt;&lt;/span&gt;In an earlier blog I showed how to search for a word or phrase in any column of a list view, and then select and highlight any rows that contained the search word or phrase.&amp;nbsp; In that first example, I restricted the search in two ways.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Firstly, I stopped as soon as the search string had been found once (and so ignoring the situation where the same term might occur several times).&amp;nbsp; Secondly, and perhaps more importantly, that example required the search term to be matched &lt;em&gt;exactly &lt;/em&gt;with what appeared in any of the columns.&amp;nbsp; In other words, if the search term was &amp;lsquo;Jeff&amp;rsquo;, then it wouldn&amp;rsquo;t be found as a match in a column that contained the phrase &amp;lsquo;Jeff Martin&amp;rsquo;.&amp;nbsp; In many real world scenarios, this simply won&amp;rsquo;t cut it, so let&amp;rsquo;s move on to the next stage and build in these features.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Contains &amp;ndash; not Equals &lt;br /&gt;&lt;/span&gt;The key to the partial match search is to use the &lt;strong&gt;Contains&lt;/strong&gt; string function in place of the &amp;ldquo;=&amp;rdquo; symbol approach we first used.&amp;nbsp; As you&amp;rsquo;d expect from the name, Contains will return a value of True if the string contains the search term within any part of its whole length.&amp;nbsp; So, it will return True for &amp;lsquo;Jeff&amp;rsquo; in Jeff Martin, or True for &amp;lsquo;Mar&amp;rsquo; in Jeff Martin, or even True for &amp;lsquo;ff M&amp;rsquo; in Jeff Martin.&amp;nbsp; You get the picture.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s part of the code for the function that carries out the search:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt; FindPartialItems(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; TextToFind &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;) &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color:blue;"&gt;Of&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Loop through LV&amp;rsquo;s ListViewItems.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&lt;span style="color:#ff0000;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).Text).Contains(Trim(TextToFind)) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the Contains function is used to check if the first column contains the search term.&amp;nbsp; Trim is used to remove spaces either side of the search term and the text in the column, simply to avoid any matches that might fail just because one side has a space and the other doesn&amp;rsquo;t. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Storing the row numbers where a match is found&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s look at a bit more of the procedure to see what happens if a match is found:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt; FindPartialItems(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; TextToFind &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;) &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color:blue;"&gt;Of&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&lt;span style="color:#ff0000;"&gt;&lt;span style="color:blue;"&gt;Dim&lt;/span&gt; rowsFound &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color:blue;"&gt;Of&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Loop through LV&amp;rsquo;s ListViewItems.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).Text).Contains(Trim(TextToFind)) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; If found, add to collection&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&amp;nbsp; &lt;span style="color:#ff0000;"&gt;rowsFound.Add(i)&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;At this stage, a List(Of Integer) has been added, and this will be used to hold the row numbers where matches are found.&amp;nbsp; List(Of T) is probably a bit of a sledgehammer for this task, but I find it much easier to work with than other options, such as an array of ints.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;So, if the text in the first column of the current row is found to contain the search term, the row number is added to the list.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;Checking the remaining columns &lt;br /&gt;&lt;/span&gt;That&amp;rsquo;s the first column dealt with, so next the code will check the remaining columns.&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt; FindPartialItems(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; TextToFind &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;) &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color:blue;"&gt;Of&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; rowsFound &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color:blue;"&gt;Of&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Loop through LV&amp;rsquo;s ListViewItems.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).Text).Contains(Trim(TextToFind)) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; If found, add to collection&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rowsFound.Add(i)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&lt;span style="color:#ff0000;"&gt;&lt;span style="color:blue;"&gt;For&lt;/span&gt; subitem &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items(i).SubItems.Count - 1&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;strong&gt;&lt;span style="color:#ff0000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).SubItems(subitem).Text).Contains(Trim(TextToFind)) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; If found, note the row number unless the same search term was found in an earlier column of this row&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#ff0000;"&gt;&lt;strong&gt;&lt;span style="color:#ff0000;"&gt;&lt;span style="color:blue;"&gt;If&lt;/span&gt; &lt;span style="color:blue;"&gt;Not&lt;/span&gt;&lt;/span&gt; rowsFound.Contains(i) &lt;span style="color:blue;"&gt;Then&lt;/span&gt; rowsFound.Add(i)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The nested loop For subitem As Integer &amp;hellip; checks through each of the remaining columns of the current row.&amp;nbsp; Again, it uses the Contains method to see if there&amp;rsquo;s a match to be found.&lt;/p&gt;
&lt;p&gt;Then, we want to deal with the case where the search phrase occurs more than once in the same row.&amp;nbsp; To avoid duplicate numbers in the list of rows, you&amp;rsquo;ll see that the&amp;nbsp; Contains method is used again for a different purpose:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="color:#ff0000;"&gt;&lt;strong&gt;&lt;span style="color:#ff0000;"&gt;&lt;span style="color:blue;"&gt;If&lt;/span&gt; &lt;span style="color:blue;"&gt;Not&lt;/span&gt;&lt;/span&gt; rowsFound.Contains(i) &lt;span style="color:blue;"&gt;Then&lt;/span&gt; rowsFound.Add(i)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This time it makes sure that a row number is only added to the list once.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Returning the result &lt;br /&gt;&lt;/span&gt;The final task, once the whole list view has been processed is to return the list of integers:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="color:blue;"&gt;Return&lt;/span&gt; rowsFound&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So the complete FindPartialItems method looks like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt; FindPartialItems(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; TextToFind &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;) &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color:blue;"&gt;Of&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; rowsFound &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color:blue;"&gt;Of&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Loop through LV&amp;rsquo;s ListViewItems.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).Text).Contains(Trim(TextToFind)) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; If found, add to collection&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rowsFound.Add(i)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; subitem &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items(i).SubItems.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).SubItems(subitem).Text).Contains(Trim(TextToFind)) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; If found, note the row number unless the same search term was found in an earlier column of this row&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; &lt;span style="color:blue;"&gt;Not&lt;/span&gt; rowsFound.Contains(i) &lt;span style="color:blue;"&gt;Then&lt;/span&gt; rowsFound.Add(i)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; &lt;strong&gt;Return the resulting collection of rows&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&lt;span style="color:blue;"&gt;Return&lt;/span&gt; rowsFound&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;The Calling Code &lt;br /&gt;&lt;/span&gt;All that remains is to write some code that uses this search method:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button_Click(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; btnPartialSearch.Click&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.FullRowSelect = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;ListView1.MultiSelect = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; foundRows &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color:blue;"&gt;Of&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foundRows = FindPartialItems(ListView1, TextBox1.Text)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; foundRows.Count = 0 &lt;span style="color:blue;"&gt;Then&lt;/span&gt; Label1.Text = &lt;span style="color:#a31515;"&gt;&amp;quot;Search string not found&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; foundRows.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Items(foundRows(i)).Selected = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Focus()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.SelectedItems(0).EnsureVisible()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;This is similar to the calling code used in the earlier blog.&amp;nbsp; The main changes are that MultiSelect is now set to true.&amp;nbsp; Obviously, if we&amp;rsquo;re returning a list of potentially multiple hits, we want to able to highlight them all.&lt;/p&gt;
&lt;p&gt;Next, a temporary list of integers is created and the search function called.&amp;nbsp; Once a result has been returned, it first tests that there has been at least one match.&amp;nbsp; If not, a message is displayed.&lt;/p&gt;
&lt;p&gt;Then, if there are one or more matches, the row numbers can be pulled from the list of integers and each of those rows is selected.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Finally, as in the earlier blog, the focus is handed off to the list view to bring the highlights to life, and if necessary the list is scrolled to reveal the first matching row.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/1727.listViewSearch2_5F00_47F24A57.png"&gt;&lt;img height="455" width="633" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/7144.listViewSearch2_5F00_thumb_5F00_529F7BDF.png" alt="listViewSearch2" border="0" title="listViewSearch2" style="display:inline;border:0px;" /&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=703993" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/9C3qPZCD3ig" height="1" width="1"/&gt;</description><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/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/ListView/default.aspx">ListView</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/Search/default.aspx">Search</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Contains/default.aspx">Contains</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/10/31/a-search-for-multiple-occurrences-of-a-string-in-a-windows-forms-listview.aspx</feedburner:origLink></item><item><title>Three ListView Tips and Tricks</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/Z_cx5aCZkPg/three-listview-tips-and-tricks.aspx</link><pubDate>Mon, 24 Oct 2011 09:35:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704149</guid><dc:creator>Ged Mead</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction      &lt;br /&gt;&lt;/u&gt;The ListView is a really useful control for display data, but it’s not always easy to see how to get it to look just the way you want.&amp;#160; Here are a few tips and tricks.&lt;/p&gt;  &lt;p&gt;First, there’s how to change the color of alternative lines, then there’s how to insert a new line of data at runtime. Finally, how to find an item in a ListView and change it.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Change the color of alternate lines      &lt;br /&gt;&lt;/u&gt;To enhance the look of the otherwise plain white background, you can inject color into alternate lines:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6433.ListViewAlternateLines_5F00_5DAEBFEF.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ListViewAlternateLines" border="0" alt="ListViewAlternateLines" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4214.ListViewAlternateLines_5F00_thumb_5F00_6FD73B17.png" width="661" height="428" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The code is quite simple:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; ChangeAlternateColors(LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; color1 &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Color&lt;/span&gt; = System.Drawing.&lt;span style="color:#2b91af;"&gt;Color&lt;/span&gt;.LightBlue&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; color2 &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Color&lt;/span&gt; = System.Drawing.&lt;span style="color:#2b91af;"&gt;Color&lt;/span&gt;.Beige&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; alternator &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; lvi &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListViewItem&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;For&lt;/span&gt; &lt;span style="color:blue;"&gt;Each&lt;/span&gt; lvi &lt;span style="color:blue;"&gt;In&lt;/span&gt; LV.Items&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; alternator &lt;span style="color:blue;"&gt;Mod&lt;/span&gt; 2 = 0 &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lvi.BackColor = color1&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Else&lt;/span&gt; : lvi.BackColor = color2&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; alternator += 1&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The key that makes this work is Integer variable named alternator. As the code enumerates through each line of the ListView, this variable increments by 1. Then, a check is made to see if the value of alternator is odd or even. That’s what the &lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&lt;span style="color:blue;"&gt;If&lt;/span&gt; alternator &lt;span style="color:blue;"&gt;Mod&lt;/span&gt; 2 = 0 &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;line does. If the line number is even, then color1 is used as the background; otherwise color2 is used.&amp;#160; Simple, but effective.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Insert a new item into a specific row of a ListView      &lt;br /&gt;&lt;/u&gt;If you want to programmatically add a new item, there are a couple of options. if it’s just a question of adding text to the first column, then you can use this approach:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;ListView1.Items.Insert(3, &lt;span style="color:#a31515;"&gt;&amp;quot;James May&amp;quot;&lt;/span&gt;)&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;In this example, the name is inserted as a new row (which is of course the fourth row down, because the ListView Items are zero indexed).&lt;/p&gt;  &lt;p&gt;In reality, you’ll usually want to insert data in other columns too.&amp;#160; This takes a slightly more verbose approach, but it’s still easy:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;With&lt;/span&gt; ListView1&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Items.Insert(3, &lt;span style="color:#a31515;"&gt;&amp;quot;James May&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Items.Item(3).SubItems.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;www.bbc.co.uk/topgear&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Items.Item(3).SubItems.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;Technology, cars, engineering, aeroplanes, flying&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;With&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;You can see the result here:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8322.ListViewInsertItem_5F00_04F51AF3.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ListViewInsertItem" border="0" alt="ListViewInsertItem" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8877.ListViewInsertItem_5F00_thumb_5F00_2FBD5938.png" width="771" height="499" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;James May has been added to the fourth line.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Find an item and change it      &lt;br /&gt;&lt;/u&gt;If you need to programmatically find a particular item in the ListView, and then edit the content, you can use code like this:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; FindAndChangeAnItem(LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, SearchText &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;, ReplacementText &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).Text) = SearchText &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LV.Items(i).Text = ReplacementText&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button2_Click(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button2.Click&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; FindAndChangeAnItem(ListView1, &lt;span style="color:#a31515;"&gt;&amp;quot;James May&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;James A. May&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This example only looks in the first column.&lt;/p&gt;  &lt;p&gt;And, as before, in the real world things are seldom as easy as just needing to change something in column 1. So if you need to dig deeper into the other columns, then you can search the subitems too, find the search term,and make the change:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; FindAndChangeASubItem(LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, SearchText &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;, ReplacementText &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).Text) = SearchText &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LV.Items(i).Text = ReplacementText&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Exit Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Else&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;For&lt;/span&gt; subitems &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items(i).SubItems.Count - 1&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).SubItems(subitems).Text) = SearchText &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LV.Items(i).SubItems(subitems).Text = ReplacementText&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Exit Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160; Calling code:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt; FindAndChangeASubItem(ListView1, &lt;span style="color:#a31515;"&gt;&amp;quot;www.bbc.co.uk/topgear&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;http:www.jamesmay.com&amp;quot;&lt;/span&gt;)&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;u&gt;Summary      &lt;br /&gt;&lt;/u&gt;These little tips and tricks can change your ListView experience from one that’s quite limited and frustrating, to something that makes you realize that it’s a really useful control that you’ll be happy to use.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704149" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/Z_cx5aCZkPg" height="1" width="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/ListView/default.aspx">ListView</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/Colors/default.aspx">Colors</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/Search/default.aspx">Search</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/10/24/three-listview-tips-and-tricks.aspx</feedburner:origLink></item><item><title>How to Create a Numerals Only TextBox</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/p4Q98D0-8cY/how-to-create-a-numerals-only-textbox.aspx</link><pubDate>Thu, 20 Oct 2011 07:45:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704216</guid><dc:creator>Ged Mead</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction      &lt;br /&gt;&lt;/u&gt;If you want to restrict the user input into a text box, you can check what the current key press is and then decide if it’s an allowed key or not. At the other end of the scale, you can create a standalone user control that can be added to the Toolbox, and have instances of it dragged on to forms across several projects.&amp;#160; This article looks at an approach half way between the two.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;u&gt;The TextBox code      &lt;br /&gt;&lt;/u&gt;In a Windows Forms application, create a new class and name it NumericTextBox.&amp;#160; Add an Inherits statement as shown below that will make this class inherit from the standard TextBox class:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;Public&lt;/span&gt; &lt;span style="color:blue;"&gt;Class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;NumericTextBox&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Inherits&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TextBox&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The next step is to identify the keys that we will allow the user to use in this text box. One way of doing this is to create an array that holds the Ascii key codes for allowed keys.&lt;/p&gt;  &lt;p&gt;In our case, this will obviously be the numerals 0 to 9, but we probably also want to allow things like the period, comma, delete and backspace key.&amp;#160; So, these are all added to the array.&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Allowed keys&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; allowedKeys &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Short&lt;/span&gt;() = {8, 44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 127}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;With that in place, the reminder of the code overrides the KeyPress event of the text box and checks if the current key press is allowed or not.&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;Protected&lt;/span&gt; &lt;span style="color:blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; OnKeyPress(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;KeyPressEventArgs&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; KeyAscii &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Short&lt;/span&gt; = &lt;span style="color:blue;"&gt;CShort&lt;/span&gt;(Asc(e.KeyChar))&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;MyBase&lt;/span&gt;.OnKeyPress(e)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Check if this key is allowed&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; allowedKeys.Contains(KeyAscii) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Exit Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Else&lt;/span&gt;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Ignore input&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.Handled = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Beep()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;When the user presses a key, the value of that key is stored as a KeyChar property. So, firstly, this code casts the key char to a Short value named KeyAscii before it then allows the text box standard key press code to continue.&lt;/p&gt;  &lt;p&gt;Then it checks to see if the current key press Ascii code is one of the ones contained in that array of allowed values. if it is, then the code lets the key press go through. if it’s not an allowed key, then the Handled property of the KeyPressEventArgs is set to True.&amp;#160; This effectively tells the key press handler that this key press has been dealt with and instructs it not to consider it any further. In other words, that key press is to be ignored.&amp;#160; Optionally, you can warn the user by playing the Beep sound.&amp;#160; &lt;/p&gt;  &lt;p&gt;At its most basic, that’s all that’s needed.&amp;#160; Here’s a summary of the code so far:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;Public&lt;/span&gt; &lt;span style="color:blue;"&gt;Class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;NumericTextBox&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Inherits&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TextBox&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Allowed keys&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; allowedKeys &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Short&lt;/span&gt;() = {8, 44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 127}&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Protected&lt;/span&gt; &lt;span style="color:blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; OnKeyPress(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;KeyPressEventArgs&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; KeyAscii &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Short&lt;/span&gt; = &lt;span style="color:blue;"&gt;CShort&lt;/span&gt;(Asc(e.KeyChar))&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;MyBase&lt;/span&gt;.OnKeyPress(e)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Check if this key is allowed&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; allowedKeys.Contains(KeyAscii) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Exit Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Else&lt;/span&gt;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Ignore input&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.Handled = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Beep()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Class&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160; The next step is to use the NumericTextBox in a form.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Using the text box in a form      &lt;br /&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;After you Build the project, go to the Windows Form code page and add the following code:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Form1_Load(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; &lt;span style="color:blue;"&gt;Me&lt;/span&gt;.Load&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; numericTextBox &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;NumericTextBox&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;With&lt;/span&gt; numericTextBox&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Left = 20&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Top = 50&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Name = &lt;span style="color:#a31515;"&gt;&amp;quot;ntxtPrincipal&amp;quot;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Me&lt;/span&gt;.Controls.Add(numericTextBox)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;With&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Now, when you run the application, the new NumericTextBox will have been added, and the user will only be able to add those allowed characters into it.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;A couple of options      &lt;br /&gt;&lt;/u&gt;As all experienced developers know, if it’s possible for a user to foul up the input, they probably will at some time.&amp;#160; So one basic safety check you may want to include is to stop them entering more than one period character. &lt;/p&gt;  &lt;p&gt;This is easy to implement. Change the code for the NumericTextBox class as shown:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Allowed keys&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; allowedKeys &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Short&lt;/span&gt;() = {8, 44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 127}&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;strong&gt;&lt;em&gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; periodUsed &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Boolean&lt;/span&gt; = &lt;span style="color:blue;"&gt;False&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Protected&lt;/span&gt; &lt;span style="color:blue;"&gt;Overrides&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; OnKeyPress(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;KeyPressEventArgs&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; KeyAscii &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Short&lt;/span&gt; = &lt;span style="color:blue;"&gt;CShort&lt;/span&gt;(Asc(e.KeyChar))&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;MyBase&lt;/span&gt;.OnKeyPress(e)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;strong&gt;&lt;em&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; KeyAscii = 46 &lt;span style="color:blue;"&gt;AndAlso&lt;/span&gt; periodUsed = &lt;span style="color:blue;"&gt;True&lt;/span&gt; &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;strong&gt;&lt;em&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.Handled = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;strong&gt;&lt;em&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Check if this key is allowed&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; allowedKeys.Contains(KeyAscii) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;strong&gt;&lt;em&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; KeyAscii = 46 &lt;span style="color:blue;"&gt;Then&lt;/span&gt; periodUsed = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Exit Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Else&lt;/span&gt;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39; Ignore input&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.Handled = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Beep()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt; I’ve highlighted the changes. First, I added a Boolean that keeps track of whether a period has already been entered.&amp;#160; Then before the code even checks if the current key press is an allowed one, it specifically checks to see if this key press is a period and&amp;#160; the text box already contains a period.&amp;#160; If this is the situation, then this attempt to enter another period is blocked by the use of the e.Handled = True line.&amp;#160; The final action is to switch that Boolean to True the first time the user enters a period.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Summary     &lt;br /&gt;&lt;/u&gt;That’s a very basic numerals only text box.&amp;#160; You can of course expand the options to control what the user is allowed to enter, if you want a text box that, for instance, only allows certain letters.&amp;#160; Or certain letters and numbers, and so on.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704216" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/p4Q98D0-8cY" height="1" width="1"/&gt;</description><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/.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/TextBox/default.aspx">TextBox</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/OnKeyPress/default.aspx">OnKeyPress</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/KeyPress/default.aspx">KeyPress</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Inheritance/default.aspx">Inheritance</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/10/20/how-to-create-a-numerals-only-textbox.aspx</feedburner:origLink></item><item><title>SharePoint 2010 And Excel Services when switching to Claims Based Authentication.</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/-klevpOf8aU/sharepoint-2010-and-excel-services-when-switching-to-claims-based-authentication.aspx</link><pubDate>Wed, 19 Oct 2011 19:31:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704472</guid><dc:creator>Andy Bonner</dc:creator><slash:comments>0</slash:comments><description>&lt;link type="text/css" rel="stylesheet" href="/dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css"&gt;&lt;/link&gt;
&lt;script language="javascript" src="/dp.SyntaxHighlighter/Scripts/shCore.js"&gt;&lt;/script&gt;
&lt;script language="javascript" src="/dp.SyntaxHighlighter/Scripts/shBrushCSharp.js"&gt;&lt;/script&gt;
&lt;p&gt;Doing some work for a customer on SharePoint 2010 where I hadn&amp;#39;t setup the initial environment &amp;amp; came across an interesting situation which had me pulling what little hair I have out, so thought I&amp;#39;d create a quick blog post in case anyone else stumbles upon this situation in the future.&lt;/p&gt;
&lt;p&gt;The web app had initially been setup using Classic Mode Authentication but due to access being required&amp;nbsp;with both Windows &amp;amp; Forms Based Authentication using the SQL provider mechanism I needed to change it to Claims Based Authentication.&amp;nbsp; This all went relatively smoothly apart from one tiny problem....&lt;/p&gt;
&lt;p&gt;Previously to the switch over spreadsheets had been opening nicely in the browser with Excel Services however after the change over, they were getting the error that the user did not have permissions to open the file in the browser.&amp;nbsp; This was easily remedied by jumping into Global Settings for excel services&amp;nbsp;changing the File Access Method&amp;nbsp;from Impersonation&amp;nbsp;over to using the&amp;nbsp;Process account instead, and I&amp;nbsp;also setup the Unattended Service Account which for some reason they hadn&amp;#39;t done in the initial install.&lt;/p&gt;
&lt;p&gt;Then tried opening the file in the browser again only to be confronted with a new error &amp;quot;The workbook cannot be opened.&amp;quot;&amp;nbsp; This had me scratching my head for a little while until I looked in the event log where I found that the Service Account being used for Excel Services hadn&amp;#39;t been given any rights on the content database.&amp;nbsp; After which the fix was easy to implement with the following PowerShell commands&lt;/p&gt;
&lt;p&gt;&lt;pre name="3981b24785e24a71b3edb34ade5b5b78" class="C#"&gt;
$webApp = Get-SPWebApplication &amp;quot;http://YourSharePointSite&amp;quot; 

$webApp.GrantAccessToProcessIdentity(&amp;quot;YourDomain\ExcelServicesAccountName&amp;quot;)
&lt;/pre&gt; &lt;/p&gt;
&lt;p&gt;Hopefully this will save someone else a bit of time.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;script language="javascript"&gt;
dp.SyntaxHighlighter.ClipboardSwf = '/dp.SyntaxHighlighter/Scripts/clipboard.swf'
dp.SyntaxHighlighter.HighlightAll('3981b24785e24a71b3edb34ade5b5b78')
&lt;/script&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704472" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/-klevpOf8aU" height="1" width="1"/&gt;</description><category domain="http://vbcity.com/blogs/skullcrusher/archive/tags/SharePoint+2010/default.aspx">SharePoint 2010</category><category domain="http://vbcity.com/blogs/skullcrusher/archive/tags/CBA/default.aspx">CBA</category><category domain="http://vbcity.com/blogs/skullcrusher/archive/tags/Excel+Services/default.aspx">Excel Services</category><category domain="http://vbcity.com/blogs/skullcrusher/archive/tags/Claims+Based+Authentication/default.aspx">Claims Based Authentication</category><feedburner:origLink>http://vbcity.com/blogs/skullcrusher/archive/2011/10/20/sharepoint-2010-and-excel-services-when-switching-to-claims-based-authentication.aspx</feedburner:origLink></item><item><title>How to Serialize and Deserialize ListView data</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/xPQHOgoMsmY/how-to-serialize-and-deserialize-listview-data.aspx</link><pubDate>Mon, 17 Oct 2011 05:26:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704151</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction      &lt;br /&gt;&lt;/u&gt;Although you’ll most often use serialization to store objects in a file, you can use it to store the data from a ListView.&amp;#160; Here’s how.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Serialize the data      &lt;br /&gt;&lt;/u&gt;The following code serializes a ListView’s data to a file:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; SerializeListViewData(LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; filename &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Create a FileStream and get the file to write to. &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; FS &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FileStream&lt;/span&gt; = &lt;span style="color:#2b91af;"&gt;File&lt;/span&gt;.Create(filename)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Create a Binary Formatter for Serialization process&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; BinFmtr &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BinaryFormatter&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Create an arraylist as temp storage for listview items data&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; alSavedLV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ArrayList&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Iterate through the ListView&amp;#39;s listitem collection and add&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; each to the temporary ArrayList&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;For&lt;/span&gt; item &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; lv.Items.Count - 1&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Add next item/subitem to the arraylist&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; alSavedLV.Add(LV.Items(item))&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Serialize the complete arraylist to the file&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; The arraylist contains all the listview data&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; BinFmtr.Serialize(FS, alSavedLV)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Close the FileStream&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; FS.Close()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The first step is to create a FileStream that will create the pipeline from the data to the file it’s going to be stored in. This FileStream will be passed the path and name of the target file&lt;/p&gt;  &lt;p&gt;Next, you create a BinaryFormatter object that will do the serialization for you. Note that you need to have an Imports statement pointing to&amp;#160; System.Runtime.Serialization.Formatters.Binary to access this.&lt;/p&gt;  &lt;p&gt;You can’t directly serialize the columns and rows from the ListView to the file, so what you do is create a collection and then serialize this. One collection type that works well is the ArrayList. The next line of code creates this.&lt;/p&gt;  &lt;p&gt;Then, you read each row of data from the ListView into the ArrayList.&lt;/p&gt;  &lt;p&gt;Finally, you take that BinaryFormatter, use its Serialize method, passing in the FileStream (to connect to the file) and the ArrayList(to give access to the ListView data). &lt;/p&gt;  &lt;p&gt;&lt;u&gt;Deserialize the data to a ListView      &lt;br /&gt;&lt;/u&gt;The serialized data is fine where it is, but presumably at some point you may want to pipe it back into the ListView. To do that, you use a similar approach. You create a FileStream, a BinaryFormatter, and an ArrayList again. &lt;/p&gt;  &lt;p&gt;Then, you use the Deserialize method of the BinaryFormatter object and read the data from the file into the ArrayList.&lt;/p&gt;  &lt;p&gt;Once the ArrayList holds the data, you can iterate through it, read the content, cast each element to ListViewItem, and then add the ListViewItem to the ListView. &lt;/p&gt;  &lt;p&gt;Here’s the code that does this:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; DeserializeToListView(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; filename &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Create a FileStream to access the storage file&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; FS &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FileStream&lt;/span&gt; = &lt;span style="color:#2b91af;"&gt;File&lt;/span&gt;.Open(filename, &lt;span style="color:#2b91af;"&gt;FileMode&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Create a Binary Formatter for Serialization process&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; BinFmtr &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BinaryFormatter&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Create an arraylist as temp storage for listview items data&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; alSavedLV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ArrayList&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Deserialize the data from the file and put it in the arraylist&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; alSavedLV = &lt;span style="color:blue;"&gt;CType&lt;/span&gt;(BinFmtr.Deserialize(FS), &lt;span style="color:#2b91af;"&gt;ArrayList&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Read the arraylist contents into the listview&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; lvi &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListViewItem&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;For&lt;/span&gt; item &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; alSavedLV.Count - 1&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lvi = &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListViewItem&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lvi = &lt;span style="color:blue;"&gt;CType&lt;/span&gt;(alSavedLV(item), &lt;span style="color:#2b91af;"&gt;ListViewItem&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LV.Items.Add(lvi)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;&amp;#160; Done with the FileStream&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; FS.Close()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;u&gt;Summary      &lt;br /&gt;&lt;/u&gt;Once you understand the tools you can use – FileStream, BinaryFormatter, and ArrayList – serializing and deserializing is a trivial task. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704151" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/xPQHOgoMsmY" height="1" width="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/ListView/default.aspx">ListView</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/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><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/10/17/how-to-serialize-and-deserialize-listview-data.aspx</feedburner:origLink></item><item><title>How To Print a Windows Forms ListBox to a selected printer</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/f3DHqNQV-fk/how-to-print-a-windows-forms-listbox-to-a-selected-printer.aspx</link><pubDate>Thu, 13 Oct 2011 20:57:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704212</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction      &lt;br /&gt;&lt;/u&gt;Here’s an easy way you If you have a list of items in a list box and print them out to a printer.&amp;#160; This version lets the user choose a printer from the PrintDialog first.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The Windows Form      &lt;br /&gt;&lt;/u&gt;It’s a very simple form – just a list box and a button:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6521.PrintListBox_5F00_00B16F8C.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="PrintListBox" border="0" alt="PrintListBox" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2045.PrintListBox_5F00_thumb_5F00_36D2E7DB.png" width="498" height="228" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The code that does the printing      &lt;br /&gt;&lt;/u&gt;You start by adding an Imports statement for the System.Drawing.Printing class. &lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;Imports&lt;/span&gt; System.Drawing.Printing&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Next, you create a PrintDocument.&amp;#160; Essentially this object will hold whatever it is we decide we want to print.&amp;#160; In this case, we know it will be the contents of the list box. Note that it’s declared WithEvents.&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;WithEvents&lt;/span&gt; docToPrint &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;PrintDocument&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;And the reason it’s declared with events is because next we’re going to create the PrintPage event:&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; document_PrintPage(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Drawing.Printing.&lt;span style="color:#2b91af;"&gt;PrintPageEventArgs&lt;/span&gt;) _&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; docToPrint.PrintPage&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This event handler really does all the important work.&amp;#160; First, it creates a Font object that describes which font to use, what font size it will be, and whether any font attributes will be set, such as bold or italic. or this example, I’m staying with fairly plain text, but you can change these settings to whatever you want.&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&lt;span style="color:blue;"&gt;Dim&lt;/span&gt; printFont &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Font&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Arial&amp;quot;&lt;/span&gt;, 15, System.Drawing.&lt;span style="color:#2b91af;"&gt;FontStyle&lt;/span&gt;.Regular)&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The next line creates an integer that will be used to move the print head down the printed page. You’ll see how this works in a moment.&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;Dim&lt;/span&gt; YPosition &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 40&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;I picked the value of 40 to create the amount of space between the lines that looked right to me.&amp;#160; You can of course change this – and you would need to if, for instance, you used a larger font.&lt;/p&gt;  &lt;p&gt;The final four lines of the PrintPage event print the content. &lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;For&lt;/span&gt; &lt;span style="color:blue;"&gt;Each&lt;/span&gt; hotel &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt; &lt;span style="color:blue;"&gt;In&lt;/span&gt; lstHotels.Items&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.Graphics.DrawString(hotel, printFont, System.Drawing.&lt;span style="color:#2b91af;"&gt;Brushes&lt;/span&gt;.Black, 25, YPosition)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; YPosition += 40&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;As you can see, each item in the list box is dealt with separately. The DrawString method is passed the following parameters:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;the next string in the list box, &lt;/li&gt;    &lt;li&gt;the font that we created earlier &lt;/li&gt;    &lt;li&gt;A Brush that determines the color of the text, and &lt;/li&gt;    &lt;li&gt;the distance down the printer page to start printing this item. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Then, to set the print head up for the next line, the YPosition value is increased, and this moves the start position down the page.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The calling code      &lt;br /&gt;&lt;/u&gt;If you want to give the user the opportunity to select a printer, you can create and display a PrintDialog. Actually, the PrintDialog has a whole bunch of properties that you can configure, if you want to give the user even more options. But for this example, I’m just going to keep those choices to the minimum.&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button1_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button1.Click&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; PrintDialog1 &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;PrintDialog&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; result &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DialogResult&lt;/span&gt; = PrintDialog1.ShowDialog()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; result = DialogResult.OK &lt;span style="color:blue;"&gt;Then&lt;/span&gt; docToPrint.Print()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt; Essentially, what happens here is that when the user clicks the OK button of the dialog, the PrintDocument’s Print method is called. The page will then be printed, with each item of the list box in turn on a new line. &lt;/p&gt;  &lt;p&gt;&lt;u&gt;Summary     &lt;br /&gt;&lt;/u&gt;So, there it is – an easy way to print out the items in a list box to the printer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704212" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/f3DHqNQV-fk" height="1" width="1"/&gt;</description><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/.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/ListBox/default.aspx">ListBox</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Print/default.aspx">Print</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/PrintDocument/default.aspx">PrintDocument</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/10/13/how-to-print-a-windows-forms-listbox-to-a-selected-printer.aspx</feedburner:origLink></item><item><title>How to save ListView content to a text file</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/j_9Ua8oZ-68/how-to-save-listview-content-to-a-text-file.aspx</link><pubDate>Mon, 10 Oct 2011 05:26:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704148</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Introduction &lt;br /&gt;&lt;/span&gt;Sometimes you might need to take a copy of what&amp;rsquo;s displayed in a ListView and save it.&amp;nbsp; There are several options and one of these is simply to save it to a plain text file.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Saving to a known file name &lt;br /&gt;&lt;/span&gt;If you know the file name, then you can use the following code:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; SaveToKnownFile(LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, FilePathAndName &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; sw &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;StreamWriter&lt;/span&gt;(FilePathAndName)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Write(LV.Items(i).Text)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; subItemIndex &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 1 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items(i).SubItems.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Write(&lt;span style="color:#a31515;"&gt;&amp;quot;:::&amp;quot;&lt;/span&gt; &amp;amp; LV.Items(i).SubItems(subItemIndex).Text)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Write(&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.NewLine)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Close()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You&amp;rsquo;ll see that I&amp;rsquo;ve hard-coded the separator characters as &amp;ldquo;:::&amp;rdquo;.&amp;nbsp; These are the characters that any code that wants to read the data back from the file will need to use in order to split the lines back into the separate entries for the columns in the list view. I&amp;rsquo;ve found that it&amp;rsquo;s usually better to use some combination of characters that won&amp;rsquo;t come up in standard text writing.&amp;nbsp; For instance, if you just used a comma as the delimiter then you&amp;rsquo;ll probably find that your data won&amp;rsquo;t be split correctly when you read it back, because commas are so common in everyday text.&lt;/p&gt;
&lt;p&gt;So, you might want to expand the method to allow for a delimiter to be included as a parameter:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; SaveToKnownFileWithDelimiter(LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, FilePathAndName &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;, &lt;strong&gt;Delimiter &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;&lt;/strong&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; sw &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;StreamWriter&lt;/span&gt;(FilePathAndName)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Write(LV.Items(i).Text)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; subItemIndex &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 1 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items(i).SubItems.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Write(&lt;strong&gt;Delimiter&lt;/strong&gt; &amp;amp; LV.Items(i).SubItems(subItemIndex).Text)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Write(&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.NewLine)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Close()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;And you could call it with something like this:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;/div&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; btnSave_Click(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; btnSave.Click&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SaveToKnownFileWithDelimiter(ListView1, &lt;span style="color:#a31515;"&gt;&amp;quot;C:\Temp\Contributors3.txt&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;@@--@@&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Letting the user select a file &lt;br /&gt;&lt;/span&gt;You won&amp;rsquo;t always want to hard code the file path and name, so you can build in a save dialog and take out FilePathAndName :&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; UseSaveFileDialogToSave(LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, Delimiter &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; sw &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;StreamWriter&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&lt;span style="color:blue;"&gt;Dim&lt;/span&gt; sfDialog &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SaveFileDialog&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;sfDialog.&lt;/strong&gt;Filter = &lt;span style="color:#a31515;"&gt;&amp;quot;Text Files (*.txt)|*.txt&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; &lt;strong&gt;sfDialog.ShowDialog&lt;/strong&gt; = DialogResult.OK &lt;span style="color:blue;"&gt;And&lt;/span&gt; sfDialog.FileName &amp;lt;&amp;gt; &lt;span style="color:#a31515;"&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;File&lt;/span&gt;.Exists(sfDialog.FileName) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw = &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;StreamWriter&lt;/span&gt;(sfDialog.FileName)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;Error with file selection&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Write(LV.Items(i).Text)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; subItemIndex &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 1 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items(i).SubItems.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Write(Delimiter &amp;amp; LV.Items(i).SubItems(subItemIndex).Text)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Write(&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.NewLine)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Close()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Summary &lt;br /&gt;&lt;/span&gt;You can use these simple methods to save ListView data to a text file. You can see how to write the data back from the text file to the ListView in &lt;a target="_blank" href="http://vbcity.com/controlpanel/blogs/postlist.aspx?tab=postlist&amp;amp;sectionid=8"&gt;this earlier blog item&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704148" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/j_9Ua8oZ-68" height="1" width="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/ListView/default.aspx">ListView</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><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/10/10/how-to-save-listview-content-to-a-text-file.aspx</feedburner:origLink></item><item><title>How to display a countdown timer</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/yZEjA5UUllk/how-to-display-a-countdown-timer.aspx</link><pubDate>Thu, 06 Oct 2011 11:39:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704211</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction      &lt;br /&gt;&lt;/u&gt;Going through some of my old posts this morning, I saw that this question seems to have been asked a lot.&amp;#160; So I thought it was worth posting up an easy way of doing it.&lt;/p&gt;  &lt;p&gt;Here’s the form at design time:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3414.Countdown1_5F00_432B3C6F.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Countdown1" border="0" alt="Countdown1" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4087.Countdown1_5F00_thumb_5F00_4EE083A1.png" width="480" height="274" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The label with the text of “Countdown finished” has its Visibility property set to False.&amp;#160; So when the application first runs, the user sees:&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/4035.Countdown2_5F00_4AA6860F.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Countdown 2" border="0" alt="Countdown 2" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/1033.Countdown2_5F00_thumb_5F00_020C973E.png" width="476" height="269" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;u&gt;The controls      &lt;br /&gt;&lt;/u&gt;Here’s a quick rundown of the controls used in this example:&lt;/p&gt;  &lt;table border="1" cellspacing="2" cellpadding="2" width="489"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="100"&gt;Control Type&lt;/td&gt;        &lt;td valign="top" width="114"&gt;Name&lt;/td&gt;        &lt;td valign="top" width="150"&gt;Purpose&lt;/td&gt;        &lt;td valign="top" width="113"&gt;Notes&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;Label&lt;/td&gt;        &lt;td valign="top" width="114"&gt;lblElapsed&lt;/td&gt;        &lt;td valign="top" width="150"&gt;Displays the countdown in minutes and seconds&lt;/td&gt;        &lt;td valign="top" width="113"&gt;Font set to Bold and Font Size 18&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;Label&lt;/td&gt;        &lt;td valign="top" width="114"&gt;Label1&lt;/td&gt;        &lt;td valign="top" width="150"&gt;Displays the instructions to the user&lt;/td&gt;        &lt;td valign="top" width="113"&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;Label&lt;/td&gt;        &lt;td valign="top" width="114"&gt;lblFinished&lt;/td&gt;        &lt;td valign="top" width="150"&gt;Displays a message when the countdown in complete&lt;/td&gt;        &lt;td valign="top" width="113"&gt;Visible property set to False. Foreground Red.          &lt;br /&gt;FontSize = 22&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;TextBox&lt;/td&gt;        &lt;td valign="top" width="114"&gt;txtMinutes&lt;/td&gt;        &lt;td valign="top" width="150"&gt;Allows the user to set the countdown time&lt;/td&gt;        &lt;td valign="top" width="113"&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;Button&lt;/td&gt;        &lt;td valign="top" width="114"&gt;btnStart&lt;/td&gt;        &lt;td valign="top" width="150"&gt;Start the countdown&lt;/td&gt;        &lt;td valign="top" width="113"&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;Timer&lt;/td&gt;        &lt;td valign="top" width="117"&gt;Timer1&lt;/td&gt;        &lt;td valign="top" width="160"&gt;Counts down the elapsed time&lt;/td&gt;        &lt;td valign="top" width="144"&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;u&gt;The code      &lt;br /&gt;&lt;/u&gt;&lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;Here’s how it works:&lt;/p&gt;    &lt;p&gt;&lt;/p&gt; &lt;/div&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; tspn &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TimeSpan&lt;/span&gt;()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; btnStart_Click(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; btnStart.Click&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; IsNumeric(txtMinutes.Text) &lt;span style="color:blue;"&gt;AndAlso&lt;/span&gt; &lt;span style="color:blue;"&gt;CInt&lt;/span&gt;(txtMinutes.Text) &amp;gt; 0 &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tspn = &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TimeSpan&lt;/span&gt;(0, &lt;span style="color:blue;"&gt;CInt&lt;/span&gt;(txtMinutes.Text), 0)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Timer1.Enabled = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Else&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#2b91af;"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;Please enter a numeric value in the text box&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Error&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;MessageBoxButtons&lt;/span&gt;.OK, &lt;span style="color:#2b91af;"&gt;MessageBoxIcon&lt;/span&gt;.Error)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 13&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 14&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 15&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 16&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 17&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Timer1_Tick(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Timer1.Tick&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 18&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tspn = tspn.Subtract(&lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TimeSpan&lt;/span&gt;(0, 0, 1))&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 19&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 20&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblElapsed.Text = &lt;span style="color:blue;"&gt;String&lt;/span&gt;.Format(&lt;span style="color:#a31515;"&gt;&amp;quot; {0} Mins : {1} Secs&amp;quot;&lt;/span&gt;, tspn.Minutes, tspn.Seconds)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 21&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; tspn.Minutes = 0 &lt;span style="color:blue;"&gt;AndAlso&lt;/span&gt; tspn.Seconds = 0 &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 22&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Timer1.Stop()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 23&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblFinished.Visible = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 24&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 25&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 26&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Line 4 creates a new TimeSpan object. As it’s name suggests, this object represents a given period of time.&lt;/p&gt;  &lt;p&gt;When the user clicks the button, Line 8 checks that a valid number has been entered.&amp;#160; If it has, then the TimeSpan object is given the number of minutes the user entered. Note that the TimeSpan takes three values there – they represent, hour, minute and second values.&amp;#160; Hour and second have been set to zero for this example, but you can change the code if you need to.&lt;/p&gt;  &lt;p&gt;Line 10 then kickstarts the timer.&lt;/p&gt;  &lt;p&gt;If the user had failed to enter a valid number in the text box, then Line 12, points out their error and the timer isn’t started.&lt;/p&gt;  &lt;p&gt;The Timer Tick event occurs every second, so Line 18 ensures that as each second passes, one second is subtracted from the initial valur of the TimeSpan.&lt;/p&gt;  &lt;p&gt;Then, the display is updated to show how many minutes and seconds still remain in the TimeSpan.&lt;/p&gt;  &lt;p&gt;Next, in Line 21 a check is made to see if there is zero time left in the TimeSpan.&amp;#160; If so, the timer is stopped.&amp;#160; (If you didn’t do this,the timer would continue deducting seconds and the display would show negative times).&amp;#160; As the countdown is complete, as an optional extra, the user is shown a message. This is done simply by making the previously hidden lblFinished label visible.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Summary     &lt;br /&gt;&lt;/u&gt;And that’s all there is to it.&amp;#160; TimeSpans do seem to cause a lot of confusion, so if you’ve found yourself stuck on what seems like a fairly trivial task, it’ll probably be because you didn’t get the TimeSpan syntax right.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704211" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/yZEjA5UUllk" height="1" width="1"/&gt;</description><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/.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/Timer/default.aspx">Timer</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Countdown/default.aspx">Countdown</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/10/06/how-to-display-a-countdown-timer.aspx</feedburner:origLink></item><item><title>How to search for an item in a ListView</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/08krvucIFFI/how-to-search-for-an-item-in-a-listview.aspx</link><pubDate>Mon, 03 Oct 2011 06:30:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:703990</guid><dc:creator>Ged Mead</dc:creator><slash:comments>8</slash:comments><description>&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Introduction &lt;br /&gt;&lt;/span&gt;Sometimes the list of entries in a ListView is so large that you need to give the user the means of finding a particular item.&amp;nbsp; Here&amp;rsquo;s one way of doing that.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;The user interface &lt;br /&gt;&lt;/span&gt;Here&amp;rsquo;s the Windows Form I&amp;rsquo;m using for the demonstration:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/2705.ListViewSearch1_5F00_62C077BE.png"&gt;&lt;img height="407" width="566" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3326.ListViewSearch1_5F00_thumb_5F00_097E6832.png" alt="ListViewSearch1" border="0" title="ListViewSearch1" style="display:inline;border-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The user enters the search term, hits the Find button and if a match is found that row will be selected.&amp;nbsp; If no match is found, then a message will appear in the label (that currently just has a dash as its content).&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;The Code for the Search&amp;nbsp; &lt;br /&gt;&lt;/span&gt;Starting with the function that carries out the search:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 53&lt;/span&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt; FindItem(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; LV &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListView&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; TextToFind &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;) &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 54&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 55&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Loop through LV&amp;rsquo;s ListViewItems.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 56&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; i As Integer = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 57&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).Text) = Trim(TextToFind) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 58&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; If found, return the row number&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 59&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Return&lt;/span&gt; (i)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 60&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 61&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;For&lt;/span&gt; subitem As Integer = 0 &lt;span style="color:blue;"&gt;To&lt;/span&gt; LV.Items(i).SubItems.Count - 1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 62&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; Trim(LV.Items(i).SubItems(subitem).Text) = Trim(TextToFind) &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 63&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; If found, return the row number&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 64&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Return&lt;/span&gt; (i)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 65&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 66&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 67&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 68&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; If not found, then return -1.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 69&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Return&lt;/span&gt; -1&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 70&lt;/span&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;you can see that the name of the list view and the search term are passed in as parameters. The first part of the For-Next loop checks if the search term exists in the first column of the list view.&amp;nbsp; If it does, then the index number of that row is returned.&lt;/p&gt;
&lt;p&gt;If it&amp;rsquo;s not found in the first column, then the remaining columns of that row are checked.&amp;nbsp; Again, if a match is found, the index number of the row is returned.&lt;/p&gt;
&lt;p&gt;The loop continues until either a match is found or the end of the list view is reached.&amp;nbsp; If the end is reached, then a value of &amp;ndash;1 is returned.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;The Calling Code &lt;br /&gt;&lt;/span&gt;Here&amp;rsquo;s the code in the click event of the Find button:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 37&lt;/span&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; btnFind_Click(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; btnFind.Click&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 38&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.MultiSelect = &lt;span style="color:#0000ff;"&gt;False&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 39&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.FullRowSelect = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 40&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 41&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 42&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; checkInt &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = FindItem(ListView1, TextBox1.Text)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 43&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; checkInt &amp;lt;&amp;gt; -1 &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 44&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Items(checkInt).Selected = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 45&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Focus()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 46&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 47&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 48&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Label1.Text = &lt;span style="color:#a31515;"&gt;&amp;quot;Search string not found&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 49&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 50&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 51&lt;/span&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;I&amp;rsquo;ve set MultiSelect to False so that only the first occurrence is returned.&amp;nbsp; The setting of FullRowSelect in line 39 is optional, but I prefer to use it.&lt;/p&gt;
&lt;p&gt;The FindItem function is called and then the If-Then-Else block deals with the returned result.&amp;nbsp; If the result isn&amp;rsquo;t &amp;ndash;1 then a match has been found.&amp;nbsp; That row number is then selected.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Because the last user action was the clicking of the button, the button will still have focus, so the selection in the list view won&amp;rsquo;t be highlighted (and you&amp;rsquo;d be tempted to think that the function had failed).&amp;nbsp; Setting the Focus on to the list view solves this issue.&lt;/p&gt;
&lt;p&gt;If the returned value is &amp;ndash;1, then a message is shown in the label to let the user know.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;How to deal with hidden rows &lt;br /&gt;&lt;/span&gt;In most cases, the row being searched for won&amp;rsquo;t be one of the rows that is currently visible.&amp;nbsp; So, if a match is found and that row is selected, once again the user probably won&amp;rsquo;t be aware of the successful match.&amp;nbsp; The easiest way to deal with this is to add:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;ListView1.SelectedItems(0).EnsureVisible()&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;to the code immediately after the ListView1.Focus() statement.&amp;nbsp; If the SelectedItems(0) has you confused, the way it works is that SelectedItems is a collection of rows that are currently selected.&amp;nbsp; Because in this example we&amp;rsquo;re just looking for the first occurrence, we know that it will be element 0 of that collection.&amp;nbsp; (I plan to deal with multiple results in a follow-up blog).&amp;nbsp; So the relevant lines of code will now be:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 43&lt;/span&gt;&amp;nbsp;&lt;span style="color:blue;"&gt;If&lt;/span&gt; checkInt &amp;lt;&amp;gt; -1 &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 44&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Items(checkInt).Selected = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 45&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Focus()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 46&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.SelectedItems(0).EnsureVisible()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 47&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 48&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Label1.Text = &lt;span style="color:#a31515;"&gt;&amp;quot;Search string not found&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 49&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;This will cause the list view to scroll to the selected and highlighted row.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=703990" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/08krvucIFFI" height="1" width="1"/&gt;</description><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/.NET/default.aspx">.NET</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/ListView/default.aspx">ListView</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/Search/default.aspx">Search</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/10/03/how-to-search-for-an-item-in-a-listview.aspx</feedburner:origLink></item><item><title>How to copy and paste a section of a PictureBox image</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/MTSSSYEwNKQ/copy-and-paste-a-section-of-a-picturebox-image.aspx</link><pubDate>Thu, 29 Sep 2011 08:33:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704163</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Introduction      &lt;br /&gt;&lt;/span&gt;This is just a quick and dirty way to copy a section of image from one picture box and paste the selected area into a second picture box.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the basic Windows Form with two PictureBox controls at design time:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/4237.CopyPartImage1_5F00_35DF3BA6.png"&gt;&lt;img height="299" width="496" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0724.CopyPartImage1_5F00_thumb_5F00_32D9B126.png" alt="CopyPartImage1" border="0" title="CopyPartImage1" style="display:inline;border-width:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;The code      &lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; btnCopyPart_Click(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; btnCopyPart.Click&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39;Grab the content of the first PictureBox and save it as a bitmap&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; sourcebmp &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bitmap&lt;/span&gt;(picImage.Image)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39;&amp;nbsp; Create an empty bitmap the size of the second PictureBox&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; destinationbmp &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bitmap&lt;/span&gt;(picDestination.Width, picDestination.Height)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Create a Graphics object for the destination &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; gr &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt; = &lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt;.FromImage(destinationbmp)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Set the size of the area you want to copy &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; selectionrectangle &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Rectangle&lt;/span&gt;(140, 20, 270, 400)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Set the size of the destination rectangle to match the size of the second PictureBox&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; destinationrectangle &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Rectangle&lt;/span&gt;(0, 0, picDestination.Width, picDestination.Height)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Draw selected area on to the destination bitmap.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gr.DrawImage(sourcebmp, destinationrectangle, selectionrectangle, &lt;span style="color:#2b91af;"&gt;GraphicsUnit&lt;/span&gt;.Pixel)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Set the drawn destination bitmap as the image of the second PictureBox&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; picDestination.Image = destinationbmp&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;I hope that the detailed commenting makes it clear how this works. You can change the start points&amp;nbsp; and the size of the selection rectangle to grab the required area. Of course, you may want to expand this to let the user select an area and I plan to cover that in a later blog.&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;The result of the above will be:&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6428.copyPartImage2_5F00_5DA1EF6B.png"&gt;&lt;img height="300" width="500" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/3858.copyPartImage2_5F00_thumb_5F00_41FCA1CE.png" alt="copyPartImage2" border="0" title="copyPartImage2" style="display:inline;border:0px;" /&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=704163" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/MTSSSYEwNKQ" height="1" width="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/PictureBox/default.aspx">PictureBox</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/DrawImage/default.aspx">DrawImage</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/Graphics/default.aspx">Graphics</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/09/29/copy-and-paste-a-section-of-a-picturebox-image.aspx</feedburner:origLink></item><item><title>How to populate a ListView from a text file</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/JM80Bq-ZQpM/how-to-populate-a-listview-from-a-text-file.aspx</link><pubDate>Mon, 26 Sep 2011 10:01:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:703986</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Introduction &lt;br /&gt;&lt;/span&gt;The source of your data for a ListView can come from various sources and, if your requirements, a text file can sometimes be all you need.&amp;nbsp; Because of the way the ListView is structured, there are a couple of things you need to look out for.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;The ListView &lt;br /&gt;&lt;/span&gt;Here&amp;rsquo;s a screenshot of the ListView I&amp;rsquo;m using as the example:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/6557.ListViewfromFile2_5F00_06DC00F0.png"&gt;&lt;img height="228" width="965" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/8875.ListViewfromFile2_5F00_thumb_5F00_426BE9E3.png" alt="ListViewfromFile2" border="0" title="ListViewfromFile2" style="display:inline;border:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;The text file &lt;br /&gt;&lt;/span&gt;The only reason for showing you the text file is to point out that it uses the &amp;ldquo;-&amp;ldquo; symbol to break up the data into the required columns:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Brian Bilbro - ( bbilbro@hotmail.com )- Module Communicator code and architecture &lt;br /&gt;Fredi Gertsch- ( fredi@gerts.ch ) - Tab Administrator &lt;br /&gt;John Dyer -&amp;nbsp; (&lt;a href="http://www.revjon.com)"&gt;http://www.revjon.com)&lt;/a&gt; - Revjon Studios &lt;br /&gt;Robin Lilly, Vanessa Monzon - N/A &lt;br /&gt;Tom Trefz &amp;amp; Scott Willhite -NK - PopupCalendar.js (original by Tom, localization &amp;amp; Netscape by Scott) &lt;br /&gt;Richard Cox - ( &lt;a href="http://www.bydesignwebsights.com"&gt;http://www.bydesignwebsights.com&lt;/a&gt; )- Search engine optimization at the Tab level ( Description, KeyWords, Title ) &lt;br /&gt;Dino Esposito - ( &lt;a href="http://weblogs.asp.net/despos"&gt;http://weblogs.asp.net/despos&lt;/a&gt; )- Personalization architecture from MSDN magazine ( Cutting Edge - March 2004 ) &lt;br /&gt;Jeff Martin- ( &lt;a href="http://www.jeffmartin.com"&gt;http://www.jeffmartin.com&lt;/a&gt; )- move ViewState to bottom of page for better search engine spidering &lt;br /&gt;Jonathan de Halleux - ( &lt;a href="http://www.dotnetwiki.org"&gt;http://www.dotnetwiki.org&lt;/a&gt; )- PA Installer &lt;br /&gt;David Haggard -( &lt;a href="http://www.newcovenant.com"&gt;http://www.newcovenant.com&lt;/a&gt; )- function to obfuscate sensitive data to prevent collection by robots and spiders and crawlers &lt;br /&gt;Vicenc Masanas - ( &lt;a href="http://lacolla.europe.webmatrixhosting.net"&gt;http://lacolla.europe.webmatrixhosting.net&lt;/a&gt; ) - SMTP authentication &lt;br /&gt;Joe Brinkman - ( &lt;a href="http://www.tag-software.net"&gt;http://www.tag-software.net&lt;/a&gt; )- Module Action Menu concept &lt;br /&gt;Patrick Santry - ( &lt;a href="http://www.wwwcoder.com"&gt;http://www.wwwcoder.com&lt;/a&gt; ) - PayPal price checking and account checking &lt;br /&gt;M. Murali Dharan - ( &lt;a href="http://www.phpbuilder.com/columns/dhar20040217.php3"&gt;http://www.phpbuilder.com/columns/dhar20040217.php3&lt;/a&gt; )- Search Engine Concepts &lt;br /&gt;Craig Dunn - ( &lt;a href="http://users.bigpond.com/conceptdevelopment/"&gt;http://users.bigpond.com/conceptdevelopment/&lt;/a&gt; ) - Search Engine Concepts ( Searcharoo ) &lt;br /&gt;Stan Schultes - ( &lt;a href="http://www.vbnetexpert.com"&gt;http://www.vbnetexpert.com&lt;/a&gt; ) -&amp;nbsp; RSS Generator ( Visual Studio Magazine - April 2004 ) &lt;br /&gt;James Clarkson&amp;nbsp; - ( &lt;a href="http://www.blogolith.com"&gt;http://www.blogolith.com&lt;/a&gt; ) - Favicon per portal &lt;br /&gt;Scott Mitchell - ( &lt;a href="http://4guysfromrolla.com"&gt;http://4guysfromrolla.com&lt;/a&gt; ) - URL Rewriting &lt;br /&gt;Scott Stokes- ( &lt;a href="http://www.adverageous.com"&gt;http://www.adverageous.com&lt;/a&gt; )- File Manager UI ( based on DNNExplorer ) &lt;br /&gt;Michael Beller - ( &lt;a href="http://www.lightshippartners.com"&gt;http://www.lightshippartners.com&lt;/a&gt; ) - Database model for abstraction and refactoring of the core database &lt;br /&gt;Sebastian Leupold - ( &lt;a href="http://www.gamma-concept.de"&gt;http://www.gamma-concept.de&lt;/a&gt; ) - German resource files&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Using that symbol was my choice, and I could have opted for another character, symbol, or even a combination such as &amp;ldquo;-//-&amp;ldquo; .&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;The code that populates the ListView &lt;br /&gt;&lt;/span&gt;Here&amp;rsquo;s the code that will read the file and allocate the sections of text to the columns of the ListView:&lt;/p&gt;
&lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/span&gt;&amp;nbsp;&lt;span style="color:blue;"&gt;Imports&lt;/span&gt; System.IO&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/span&gt;&amp;nbsp;&lt;span style="color:blue;"&gt;Public&lt;/span&gt; &lt;span style="color:blue;"&gt;Class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Form2&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&lt;/span&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Form2_Load(sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;, e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; &lt;span style="color:blue;"&gt;Me&lt;/span&gt;.Load&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.View = &lt;span style="color:#2b91af;"&gt;View&lt;/span&gt;.Details&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Columns.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, 176)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Columns.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;Contact Information&amp;quot;&lt;/span&gt;, 266)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Columns.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;Expertise&amp;quot;&lt;/span&gt;, 276)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 10&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 11&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Try&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 12&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Declare StreamReader and pass the Path of the ini file to be read as a Parameter &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 13&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; MyStream &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;StreamReader&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;C:\Temp\Contributors.txt&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 14&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; A string array to hold each line as it is read&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 15&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; strTemp() &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 16&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39;&amp;nbsp; Code that reads the file line by line&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 17&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Do&lt;/span&gt; &lt;span style="color:blue;"&gt;While&lt;/span&gt; MyStream.Peek &amp;lt;&amp;gt; -1 &lt;span style="color:green;"&gt;&amp;#39; Use Peek to read the file until there are no more lines &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 18&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; LVItem &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ListViewItem&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 19&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Split the line using the -&amp;nbsp; delimiter&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 20&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; strTemp = MyStream.ReadLine.Split(&lt;span style="color:#a31515;"&gt;&amp;quot;-&amp;quot;c&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 21&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Assign the content of the first element of the array to the first column&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 22&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LVItem.Text = strTemp(0).ToString&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 23&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Then add the item to the ListView&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 24&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ListView1.Items.Add(LVItem)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 25&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39; Assign the content of the second element to the next column &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 26&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LVItem.SubItems.Add(strTemp(1).ToString)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 27&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;&amp;#39;&amp;nbsp; Check if there is a third section.&amp;nbsp; If so, assign it to the next column&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 28&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;If&lt;/span&gt; strTemp.Length &amp;gt; 2 &lt;span style="color:blue;"&gt;Then&lt;/span&gt; LVItem.SubItems.Add(strTemp(2).ToString)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 29&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Loop&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 30&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 31&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MyStream.Close() &lt;span style="color:green;"&gt;&amp;#39; Close the StreamReader &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 32&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 33&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MessageBox.Show(&lt;span style="color:#a31515;"&gt;&amp;quot;Error reading file.&amp;quot;&lt;/span&gt; &amp;amp; ex.Message)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 34&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Try&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 35&lt;/span&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;nbsp;&amp;nbsp; 36&lt;/span&gt;&amp;nbsp;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think that the commenting fairly well explains exactly how it works.&lt;/p&gt;
&lt;p&gt;I chose to set the properties in lines 6 to 9 here in the code.&amp;nbsp; I could of course have set them using the properties window instead.&lt;/p&gt;
&lt;p&gt;The test in line 28 is important, because if strTemp(2) is empty then you&amp;rsquo;ll get an error at runtime that will cause the display to stop rendering fully.&lt;/p&gt;
&lt;p&gt;The only other point I wanted to make is that, if you look at the Dino Esposito entry, you&amp;rsquo;ll see that not all the information is shown.&amp;nbsp; That&amp;rsquo;s because there is an additional &amp;ldquo;-&amp;ldquo; in that piece of text, and as a result the words &amp;ldquo;March 2004&amp;rdquo; at the end of his entry are not shown in the ListView.&amp;nbsp; In fact, they have been saved, but they will be in element (3) of strTemp and the code doesn&amp;rsquo;t do anything with that element.&amp;nbsp; To avoid this kind of problem, it&amp;rsquo;s usually better to use a delimiter that won&amp;rsquo;t occur naturally, such as the &amp;ldquo;-//-&amp;ldquo; example I gave earlier.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Summary &lt;br /&gt;&lt;/span&gt;Populating a ListView from a text file is relatively easy, as long as you ensure that the delimiter will work properly and that you&amp;rsquo;ve accounted for all elements of the string array you use with the Split function.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=703986" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/JM80Bq-ZQpM" height="1" width="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/ListView/default.aspx">ListView</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/StreamReader/default.aspx">StreamReader</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/09/26/how-to-populate-a-listview-from-a-text-file.aspx</feedburner:origLink></item><item><title>SharePoint 2010 Picture Library Slideshow Web Part Image Size</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/6fVZOjxRblM/sharepoint-2010-picture-library-slideshow-web-part-image-size.aspx</link><pubDate>Sun, 25 Sep 2011 19:43:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:704221</guid><dc:creator>Andy Bonner</dc:creator><slash:comments>0</slash:comments><description>&lt;link type="text/css" rel="stylesheet" href="/dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css"&gt;&lt;/link&gt;
&lt;script language="javascript" src="/dp.SyntaxHighlighter/Scripts/shCore.js"&gt;&lt;/script&gt;
&lt;script language="javascript" src="/dp.SyntaxHighlighter/Scripts/shBrushJScript.js"&gt;&lt;/script&gt;
&lt;p&gt;Well although the Picture Library Slideshow Web Part is a great OOTB feature, one thing that bugged me when I first started playing with it was the fact that it shrinks the size of the pictures that it displays with no means of overriding the size it decides to make them.&lt;/p&gt;
&lt;p&gt;But with some simple&amp;nbsp;jQuery in a Content Editor&amp;nbsp;WebPart&amp;nbsp;we can&amp;nbsp;easily overcome this.&lt;/p&gt;
&lt;p&gt;&lt;pre name="a9cc576ce297410ab0726acbf8144526" class="javascript"&gt;
&amp;lt;script&amp;gt; 
$(document).ready(function() { 
var divList = $(&amp;quot;div[id^=&amp;#39;MSOPictureLibrarySlideshowWebPart&amp;#39;]&amp;quot;); 
var currImage= $(&amp;quot;img[id$=&amp;#39;curr&amp;#39;]&amp;quot;); 
divList.height(&amp;#39;100%&amp;#39;); 
divList.width(&amp;#39;100%&amp;#39;); 
currImage.height(&amp;#39;125%&amp;#39;); 
currImage.width(&amp;#39;125%&amp;#39;); 
});&amp;lt;/script&amp;gt;
&lt;/pre&gt; &lt;/p&gt;&lt;script language="javascript"&gt;
dp.SyntaxHighlighter.ClipboardSwf = '/dp.SyntaxHighlighter/Scripts/clipboard.swf'
dp.SyntaxHighlighter.HighlightAll('a9cc576ce297410ab0726acbf8144526')
&lt;/script&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=704221" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/6fVZOjxRblM" height="1" width="1"/&gt;</description><category domain="http://vbcity.com/blogs/skullcrusher/archive/tags/Picture+Library+Slideshow/default.aspx">Picture Library Slideshow</category><category domain="http://vbcity.com/blogs/skullcrusher/archive/tags/SharePoint+2010/default.aspx">SharePoint 2010</category><feedburner:origLink>http://vbcity.com/blogs/skullcrusher/archive/2011/09/26/sharepoint-2010-picture-library-slideshow-web-part-image-size.aspx</feedburner:origLink></item><item><title>How To Print the Content of a Windows Forms ListView</title><link>http://feedproxy.google.com/~r/vbCityBlogs/~3/aRfMvRdOxvs/how-to-print-the-content-of-a-windows-forms-listview.aspx</link><pubDate>Mon, 19 Sep 2011 12:28:00 GMT</pubDate><guid isPermaLink="false">921a0936-6df9-4a08-8006-68f3b1747f32:703954</guid><dc:creator>Ged Mead</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;u&gt;Introduction&lt;/u&gt;     &lt;br /&gt;I tweaked this code a while back when I just wanted to take a screenshot of the contents of a ListView.&amp;#160; Essentially, it just uses BitBlt to grab all the required content, stores it as a temporary image, and then uses the standard PrintPage method to print it out.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The ListView      &lt;br /&gt;&lt;/u&gt;Here’s a screenshot of the list view I’ve used:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/0383.ListViewToPrint1_5F00_243BAB96.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ListViewToPrint1" border="0" alt="ListViewToPrint1" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/1805.ListViewToPrint1_5F00_thumb_5F00_7D8D1F48.png" width="564" height="340" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;While I was experimenting with the printing, I found that I got a better printed result if I increased the font size on the list view.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/5875.ListViewToPrint2_5F00_7DC9A27D.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ListViewToPrint2" border="0" alt="ListViewToPrint2" src="http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/5611.ListViewToPrint2_5F00_thumb_5F00_304CFFF0.png" width="350" height="287" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;u&gt;The Code      &lt;br /&gt;&lt;/u&gt;Here’s the code for the Windows Form: &lt;/p&gt;  &lt;div style="font-family:consolas;background:white;color:black;font-size:10pt;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&lt;span style="color:blue;"&gt;Public&lt;/span&gt; &lt;span style="color:blue;"&gt;Class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Form1&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Friend&lt;/span&gt; &lt;span style="color:blue;"&gt;WithEvents&lt;/span&gt; prntDoc &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; System.Drawing.Printing.&lt;span style="color:#2b91af;"&gt;PrintDocument&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Friend&lt;/span&gt; Print_Image &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Image&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Declare&lt;/span&gt; &lt;span style="color:blue;"&gt;Auto&lt;/span&gt; &lt;span style="color:blue;"&gt;Function&lt;/span&gt; BitBlt &lt;span style="color:blue;"&gt;Lib&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;GDI32.DLL&amp;quot;&lt;/span&gt; (&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; hdcDest &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IntPtr&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; nXDest &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; nYDest &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; nWidth &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; nHeight &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; hdcSrc &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IntPtr&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; nXSrc &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; nYSrc &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 13&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; dwRop &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt;) &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 14&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 15&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button1_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;Object&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.&lt;span style="color:#2b91af;"&gt;EventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button1.Click&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 16&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MakeImage()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 17&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 18&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 19&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; MakeImage()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 20&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; prnDialog &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;PrintDialog&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 21&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#2b91af;"&gt;Application&lt;/span&gt;.DoEvents()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 22&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Me&lt;/span&gt;.Refresh()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 23&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#2b91af;"&gt;Application&lt;/span&gt;.DoEvents()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 24&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;Get a Graphics Object from the form &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 25&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; FormG &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt; = ListView1.CreateGraphics&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 26&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;Create a bitmap from that graphics &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 27&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bitmap&lt;/span&gt;(ListView1.Width, ListView1.Height, FormG)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 28&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;Create a Graphics object in memory from that bitmap &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 29&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; memG &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt; = &lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt;.FromImage(i)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 30&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;get the IntPtr&amp;#39;s of the graphics &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 31&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; HDC1 &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IntPtr&lt;/span&gt; = FormG.GetHdc&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 32&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; HDC2 &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IntPtr&lt;/span&gt; = memG.GetHdc&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 33&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;get the picture &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 34&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BitBlt(HDC2, 0, 0, ListView1.ClientRectangle.Width, ListView1.ClientRectangle.Height, HDC1, 0, 0, 13369376)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 35&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;Clone the bitmap so we can dispose this one &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 36&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Me&lt;/span&gt;.Print_Image = &lt;span style="color:blue;"&gt;CType&lt;/span&gt;(i.Clone(), &lt;span style="color:#2b91af;"&gt;Image&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 37&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;&amp;#39;Clean Up &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 38&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FormG.ReleaseHdc(HDC1)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 39&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; memG.ReleaseHdc(HDC2)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 40&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FormG.Dispose()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 41&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; memG.Dispose()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 42&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i.Dispose()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 43&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; prnDialog.Document = prntDoc&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 44&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; r &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DialogResult&lt;/span&gt; = prnDialog.ShowDialog&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 45&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;If&lt;/span&gt; r = DialogResult.OK &lt;span style="color:blue;"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 46&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; prntDoc.Print()&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 47&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 48&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 49&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 50&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; prntDoc_PrintPage(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Drawing.Printing.&lt;span style="color:#2b91af;"&gt;PrintPageEventArgs&lt;/span&gt;) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; prntDoc.PrintPage&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 51&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.Graphics.DrawImage(Print_Image, 20, 90)&lt;span style="color:green;"&gt; &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 52&lt;/span&gt;&amp;#160;&amp;#160; &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 53&lt;/span&gt;&amp;#160;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Class&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&amp;#160;&amp;#160; 54&lt;/span&gt;&amp;#160;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Firstly, it creates a couple of variables, one for the PrintDocument that’s going to be created, and one for the image that will be built using BitBlt.&amp;#160; &lt;/p&gt;  &lt;p&gt;As you can see, BitBlt is at the heart of this task, and the next block of code comprises the BitBlt API .&amp;#160; Jumping to the core method in line 19 that creates the image for printing, you’ll see how BitBlt is used to make a copy of the area of the Windows Form that’s occupied by the list view. &lt;/p&gt;  &lt;p&gt;Once a copy of the list view has been taken and assigned to the Image named Print_Image, the user is shown a standard print dialog.&amp;#160; Once they hit the OK button in that dialog, the PrintPage method of the print document is called and this uses the standard DrawImage method to print the image to the printer page.&amp;#160;&amp;#160;&amp;#160; You can change the values for the left and top points (currently 20 and 90) if you want to reposition the printed list view on the printer page. &lt;/p&gt;  &lt;p&gt;&lt;u&gt;Summary     &lt;br /&gt;&lt;/u&gt;You’ll probably realise that although I’m capturing a list view here, this same procedure can be used for any area of the form, or any individual control, that you want to print out.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://vbcity.com/aggbug.aspx?PostID=703954" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/vbCityBlogs/~4/aRfMvRdOxvs" height="1" width="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/ListView/default.aspx">ListView</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/Print/default.aspx">Print</category><category domain="http://vbcity.com/blogs/xtab/archive/tags/PrintDocument/default.aspx">PrintDocument</category><feedburner:origLink>http://vbcity.com/blogs/xtab/archive/2011/09/19/how-to-print-the-content-of-a-windows-forms-listview.aspx</feedburner:origLink></item></channel></rss>

