<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>VB.NET and VB 2005</title><link>http://blogs.vbcity.com/xtab/category/155.aspx</link><description>  Hints and Tips on VB.NET developing.</description><managingEditor>Ged Mead</managingEditor><dc:language>et</dc:language><generator>.Text Version 0.95.2004.102</generator><item><dc:creator>Ged Mead</dc:creator><title>Populating a WinForms ListView from a Text File</title><link>http://blogs.vbcity.com/xtab/archive/2009/10/26/9428.aspx</link><pubDate>Mon, 26 Oct 2009 12:41:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2009/10/26/9428.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9428.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2009/10/26/9428.aspx#Feedback</comments><slash:comments>287</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9428.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9428.aspx</trackback:ping><description>&lt;p&gt;&lt;span style="font-family: Calibri;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;The Windows Forms ListView is designed to display data. You can use a simple text file as the data source of a Windows Forms ListView. Let's start with a scenario where you have a txt file that contains the data. Each line of the text file represents one row of data to be displayed in the ListView. The content for each column item (or cell) is delimited by the use of a TAB character. &lt;/p&gt;
&lt;p&gt;A simple text file along these lines might contain the following entries: &lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Courier New;"&gt;Ged Mead UK &lt;br /&gt;Serge Baranovsky&amp;nbsp;&amp;nbsp; &amp;nbsp;USA &lt;br /&gt;Larry Blake USA &lt;br /&gt;Scott Waletzko&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; USA &lt;br /&gt;Mark Dryden UK &lt;br /&gt;Dave Jeavons UK &lt;br /&gt;Chris Manning USA &lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;(The uneven layout is caused by the use of the TAB as the delimiter)&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;Reading data from a file&lt;/span&gt;&lt;br /&gt;An easy way of transferring this data from the file to the ListView is to: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use a StreamReader which accesses the text file and then reads it line by line. &lt;br /&gt;&lt;/li&gt;
&lt;li&gt;As each new line is read, it is split by means of the TAB character and temporarily stored in a String array. &lt;br /&gt;&lt;/li&gt;
&lt;li&gt;Next, the first element is pulled out of the String array and used as the item for the first column of the ListView (the ListViewItem).&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;Then the remaining two elements are pulled out of the String array in turn and assigned as the SubItems of the ListViewItem. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here's the code that carries out those steps: &lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: #008000;"&gt;' Variable for file to hold data&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Dim&lt;/span&gt; TextFile &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;String&lt;/span&gt; = &lt;span style="color: #a31515;"&gt;"F:\MVPs.txt"&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: #0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;Sub&lt;/span&gt; btnGet_Click(&lt;span style="color: #0000ff;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color: #0000ff;"&gt;ByVal&lt;/span&gt; e &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color: #0000ff;"&gt;Handles&lt;/span&gt; btnGet.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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Try&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: #008000;"&gt;' Declare StreamReader and pass the Path of the text file to be read as a Parameter &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: #0000ff;"&gt;Dim&lt;/span&gt; SR &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;New&lt;/span&gt; StreamReader(TextFile)&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: #008000;"&gt;'&amp;nbsp; Variable to hold data as it is read line by line&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: #0000ff;"&gt;Dim&lt;/span&gt; strTemp() &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff;"&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Do&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;While&lt;/span&gt; SR.Peek &amp;lt;&amp;gt; -1 &lt;span style="color: #008000;"&gt;' Use Peek to read the file until there are no more lines &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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #008000;"&gt;'&amp;nbsp; Create a variable for the ListViewItems&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Dim&lt;/span&gt; LVItem &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;New&lt;/span&gt; ListViewItem&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #008000;"&gt;'&amp;nbsp; Read the next line in file and Split it using the TAB. &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; &amp;nbsp;&amp;nbsp;&amp;nbsp; strTemp = SR.ReadLine.Split(Chr(9))&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #008000;"&gt;'&amp;nbsp; Pull out the first element in the line and assign it as&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #008000;"&gt;'&amp;nbsp; the data for the first column.&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; LVItem.Text = strTemp(0).ToString&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #008000;"&gt;'&amp;nbsp; Add the item to the ListView&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; LVMVPs.Items.Add(LVItem)&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #008000;"&gt;'&amp;nbsp; Assign elements 2 &amp;amp; 3 as the subitems.&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; LVItem.SubItems.Add(strTemp(1).ToString)&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; LVItem.SubItems.Add(strTemp(2).ToString)&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: #0000ff;"&gt;Loop&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;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SR.Close() &lt;span style="color: #008000;"&gt;' Close the StreamReader &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;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; Exception&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;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; MsgBox(&lt;span style="color: #a31515;"&gt;"Error reading file."&lt;/span&gt; &amp;amp; ex.Message)&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;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;Try&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: #0000ff;"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You'll see that I've used a variable to hold the path to the file that contains the data. You will also need to include an Imports statement for System.IO at the top of the Class file.&lt;/p&gt;
&lt;p&gt;Although the above code will successfully access the file, read and split the data and then populate the ListView, the result you will see may not be what you expect or want: &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;img src="http://www.xtabvbcity.plus.com/Blogs/Blog_LV_File01.png" /&gt;&lt;/p&gt;
&lt;p&gt;The first problem is that the View Property of the ListView needs to be set to Details. By default it is set to LargeIcon, with the result you see in the screenshot. You can make this change via the Properties Window or in code.&amp;nbsp;&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; LVMVPs.View = View.Details&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Make that change, run the project, hit the 'Get From File' button and you will see:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;img src="http://www.xtabvbcity.plus.com/Blogs/Blog_LV_File02.png" /&gt;&lt;/p&gt;
&lt;p&gt;Again, definitely &lt;i&gt;not&lt;/i&gt; what you want! The problem here is that the ListView won't automatically create columns for you, even though you might expect that it would based on the data you are feeding in. As it is traditional to have some header text at the top of each column, you can create this at the same time as you create the column itself .&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; LVMVPs.Columns.Add(&lt;span style="color: #a31515;"&gt;"First Name"&lt;/span&gt;, 76)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; LVMVPs.Columns.Add(&lt;span style="color: #a31515;"&gt;"Surname"&lt;/span&gt;, 96)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; LVMVPs.Columns.Add(&lt;span style="color: #a31515;"&gt;"Location"&lt;/span&gt;, 56)&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now, when you run the project again you will see the data neatly tabulated in the ListView.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;img src="http://www.xtabvbcity.plus.com/Blogs/Blog_LV_File03.png" /&gt;&lt;/p&gt;
&lt;p&gt;I have also set the width property of each column in the code above, because the default width would result in some of the longer strings being truncated otherwise.&lt;/p&gt;
&lt;p&gt;If you don't want to hard code those column headers, you can change the text file structure and have the column headers written to the first line of the file. You would then pull these out first and assign them to the columns, before using the Do While loop to read and display the rest of the file. &lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;Writing data to a file&lt;/span&gt;&lt;br /&gt;Although a ListView's key purpose is to display existing data, there may be times when you have edited the content and want to save it back to a text file. The steps involved in doing this are as follows: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use a FileStream to set up the file you are going to write to. &lt;/li&gt;
&lt;li&gt;Use a StreamWriter to access the chosen file. &lt;/li&gt;
&lt;li&gt;Loop through each row of the ListView, pull the text data from each column in turn and write it to the text file, adding a TAB character after each item. &lt;/li&gt;
&lt;li&gt;Add a new line at the end of each loop.&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The code for this process is:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Private&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;Sub&lt;/span&gt; btnSave_Click(&lt;span style="color: #0000ff;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color: #0000ff;"&gt;ByVal&lt;/span&gt; e &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color: #0000ff;"&gt;Handles&lt;/span&gt; btnSave.Click&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #008000;"&gt;'&amp;nbsp; Create FileStream and StreamWriter to access and write to file&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Dim&lt;/span&gt; FS &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;New&lt;/span&gt; FileStream(TextFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Dim&lt;/span&gt; SW &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;New&lt;/span&gt; StreamWriter(FS)&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;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Try&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: #008000;"&gt;'&amp;nbsp; Loop through all rows&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: #0000ff;"&gt;For&lt;/span&gt; Index &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color: #0000ff;"&gt;To&lt;/span&gt; LVMVPs.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;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #008000;"&gt;'&amp;nbsp; Internally loop through all columns, gathering items&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;For&lt;/span&gt; SubIndex &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color: #0000ff;"&gt;To&lt;/span&gt; LVMVPs.Items(Index).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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SW.Write(LVMVPs.Items(Index).SubItems(SubIndex).Text &amp;amp; Chr(9))&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&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;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #008000;"&gt;'&amp;nbsp; Create new line ready for next 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; &amp;nbsp;&amp;nbsp;&amp;nbsp; SW.Write(Environment.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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #0000ff;"&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; &lt;span style="color: #0000ff;"&gt;Catch&lt;/span&gt; ex &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; Exception&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;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; MsgBox(&lt;span style="color: #a31515;"&gt;"Error saving to file."&lt;/span&gt; &amp;amp; ex.Message)&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;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;Finally&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;&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FS.Close()&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;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;Try&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: #0000ff;"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;For many simple scenarios where you want to display tabulated text in a ListView (and optionally save it back to a file), the above approaches will work just fine. There will of course be times when you need something more sophisticated and I will look at some of those in later blogs.&lt;/p&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9428.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Date and Time: How To Create a 'Time' element</title><link>http://blogs.vbcity.com/xtab/archive/2009/09/05/9390.aspx</link><pubDate>Sat, 05 Sep 2009 11:18:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2009/09/05/9390.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9390.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2009/09/05/9390.aspx#Feedback</comments><slash:comments>191</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9390.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9390.aspx</trackback:ping><description>&lt;FONT face=Calibri&gt;
&lt;P&gt;I've often thought that dealing with dates and times can sometimes be much more difficult than it should be. There is a bewildering range of choices, many of which seem to do pretty much the same thing and you can sometimes wonder which is the 'right' way. One good example of the multiple choices is identifying a time. I don't mean a TimeSpan - which is a period of time - but a single specific moment in time, such as 8 o'clock, that you want to isolate and maybe compare, manipulate or filter in some way. &lt;/P&gt;
&lt;P&gt;You can of course take any DateTime instance and display only the time element by using ToShortTimeString or ToLongTimeString. &lt;/P&gt;
&lt;P&gt;
&lt;DIV style="FONT-FAMILY: Courier New; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; TimeCheck = &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; DateTime&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; TimeCheck = Now&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"TimeCheck =&amp;nbsp; "&lt;/SPAN&gt; &amp;amp; TimeCheck.ToShortTimeString)&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;You can also drill down and pull out the hour, minute, second or any combination of those.&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="FONT-FAMILY: Courier New; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; TimeCheck = &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; DateTime&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; TimeCheck = Now&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"TimeCheck Hour and Minute = {0}:{1}&amp;nbsp; "&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; TimeCheck.Hour, TimeCheck.Minute)&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Also available is the TimeOfDay property of the DateTime class, which - as you would expect - will return the time element. However this is a TimeSpan which in some cases can make it very fiddly to manipulate. It is also a ReadOnly property, so you can't use it to set a new time. &lt;/P&gt;
&lt;P&gt;And by coincidence, only today I learned of yet another one - the TimeString property, which returns the current time in HH:mm:ss format.&amp;nbsp; But that is fixed to the current time, not some time point of your choice.&lt;/P&gt;
&lt;P&gt;The TimeValue function offers a different approach. Although it gives the appearance of being just a time - e.g. 14:22:30 or 3:15 PM and so on - it is basically a full standard DateTime, but the date is always constrained to be 1st January in the year 0001. For most purposes when you use TimeValue it conveniently ignores that date, anyway. &lt;/P&gt;
&lt;P&gt;You can extract the time value of any particular date and time by passing in a DateTime instance to the TimeValue function: &lt;/P&gt;
&lt;P&gt;
&lt;DIV style="FONT-FAMILY: Courier New; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; DT &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; DateTime = TimeValue(Now)&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;TimeValue returns a String, so there is no need to cast to any of the String formats to read it as text:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="FONT-FAMILY: Courier New; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt"&gt;
&lt;P style="MARGIN: 0px"&gt;Console.WriteLine(DT)&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;If you run those last two lines of code, you may be surprised/disappointed to see that it still includes a date. However, remember that the original DateTime we passed in was the DateTime value &lt;B&gt;Now&lt;/B&gt;. As you will see in the Console printout, the real value of today's date has been dropped and is replaced by the 01/01/0001 date.&lt;/P&gt;
&lt;P&gt;TimeValue also allows you to manufacture a time. In the next example, a time of 1400 hours or 2pm is created:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="FONT-FAMILY: Courier New; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; TVTime &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; DateTime&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; TVTime = TimeValue(&lt;SPAN style="COLOR: #a31515"&gt;"14:00"&lt;/SPAN&gt;)&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Note that unlike the overloaded constructors of the DateTime class, you don't have to include year, month and day - for the obvious reason that they would be ignored anyway. &lt;/P&gt;
&lt;P&gt;Although I haven't done so, you can also include a value for Seconds to the time you create. &lt;BR&gt;&lt;BR&gt;You can add or subtract elements of time from the currently stored time by using AddHours, AddMinutes, AddSeconds, etc&lt;/P&gt;
&lt;P&gt;Finally, you can compare times. If you wanted, for example, to check if a particular deadline had passed you can compare the times extracted with TimeValue:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="FONT-FAMILY: Courier New; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; TVTime &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; DateTime&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; TVTime = TimeValue(&lt;SPAN style="COLOR: #a31515"&gt;"14:00"&lt;/SPAN&gt;)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; TimeValue(Now) &amp;gt; TVTime &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; Console.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Passed deadline"&lt;/SPAN&gt;)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Not yet reached"&lt;/SPAN&gt;)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&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;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;There is really nothing in TimeValue that can't be achieved by manipulating other properties and methods of the DateTime class, but in many situations it's a neat function that will make for easier, cleaner code.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9390.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>How To Format a String to Currency</title><link>http://blogs.vbcity.com/xtab/archive/2009/07/31/9368.aspx</link><pubDate>Fri, 31 Jul 2009 20:47:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2009/07/31/9368.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9368.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2009/07/31/9368.aspx#Feedback</comments><slash:comments>123</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9368.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9368.aspx</trackback:ping><description>&lt;FONT face=Calibri&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Here's a useful little tip for if you have a numeric value and you need to convert it into your local currency.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Let's assume you take in a value as a Double and you want the output to be in the format of currency, with two decimal places (in the case of the UK to represent Pence).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Here's the Double:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="BACKGROUND-COLOR: #e0e0e0; FONT-FAMILY: 'Courier New'; FONT-SIZE: 15px; OVERFLOW: auto"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; TotalCost&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Double&lt;/FONT&gt; = 123.4567&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;To show this formatted, you can use the String.Format method, a placeholder for the variable and the 'c' attribute to ensure the currency format is used:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="BACKGROUND-COLOR: #e0e0e0; FONT-FAMILY: 'Courier New'; FONT-SIZE: 15px; OVERFLOW: auto"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Label1.Text =&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;.Format(&lt;FONT color=#a31515&gt;"Total Cost = {0:c}"&lt;/FONT&gt;, TotalCost)&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The result would look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/Currency.png"&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Notice that it automatically rounds up for you, changing '4567' to '46'. &lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9368.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Copying An Item Between ListViews</title><link>http://blogs.vbcity.com/xtab/archive/2009/06/09/9319.aspx</link><pubDate>Tue, 09 Jun 2009 09:44:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2009/06/09/9319.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9319.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2009/06/09/9319.aspx#Feedback</comments><slash:comments>122</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9319.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9319.aspx</trackback:ping><description>&lt;P&gt;&lt;SPAN style="FONT-SIZE: x-small; FONT-FAMILY: Verdana"&gt;&lt;/SPAN&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I've always found ListViews quite fascinating. Slightly confusing sometimes, but fascinating nevertheless. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; As I have often been heard to say,&amp;nbsp;it's the little Gotchas that'll get ya.&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;Take, for instance, the subject of this blog - copying an item between ListViews. The scenario is that you let the user click on an item in ListView1 and if they want this item copied to ListView2, they hit a button. &lt;BR&gt;&lt;BR&gt;Now, you would probably think that all you need to do is identify the currently selected item and add it straight to the second ListView. Something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e0e0e0"&gt;ListView2.Items.Add(ListView1.SelectedItems(0))&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;But if you do try this, you will get an error. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;IMG height=249 src="http://www.xtabvbcity.plus.com/Blogs/ListViewItemCopy1.jpg" width=453&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The text of the error message pretty much says it all. What you have to do (assuming that you aren't prepared to remove the item from the original ListView) is to clone it. You will then be allowed to add the clone to the second ListView.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Although the cloning isn't difficult, you do have to be aware of the need to cast the selected item to ListViewItem if you have Option Strict On. To be honest, I found this a bit strange at first. If I lift a ListViewItem from a ListView, I didn't expect to have to cast it to what it is - i.e a ListViewItem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I'm not entirely sure why this occurs and wonder if the underlying reason for this is that the ListViewItem is stored in the SelectedItems collection of the ListView as a generic object. Anyway, Casting it back to a ListViewItem at the point where the cloning takes place, fixes this&amp;nbsp;without any problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;This code works well:&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e0e0e0"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;If&lt;/SPAN&gt; ListView1.SelectedItems.Count &amp;gt; 0&lt;SPAN style="COLOR: #0000ff"&gt; Then&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/SPAN&gt; lvi&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; New&lt;/SPAN&gt; ListViewItem&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lvi = ListView1.SelectedItems(0)&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/SPAN&gt; lvi2&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; New&lt;/SPAN&gt; ListViewItem&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lvi2 =&lt;SPAN style="COLOR: #0000ff"&gt; CType&lt;/SPAN&gt;(lvi.Clone, ListViewItem)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ListView2.Items.Add(lvi2)&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;End&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; If&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;You'll have noticed that I built in a test to ensure that an item is currently selected. It's an easy thing to forget and is sure to bring your app to a grinding halt before long if you don't build this in.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;You can see where I have cast the selected item (aka lvi) to ListViewItem in Line 5. Intriguingly, casting to ListViewItem in the third line of code, e.g. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e0e0e0"&gt;lvi =&lt;SPAN style="COLOR: #0000ff"&gt; CType&lt;/SPAN&gt;(ListView1.SelectedItems(0), ListViewItem)&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;doesn't cut it. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9319.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Removing Double Quotes from a Text File</title><link>http://blogs.vbcity.com/xtab/archive/2009/06/09/9318.aspx</link><pubDate>Tue, 09 Jun 2009 09:35:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2009/06/09/9318.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9318.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2009/06/09/9318.aspx#Feedback</comments><slash:comments>142</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9318.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9318.aspx</trackback:ping><description>&lt;P&gt;&lt;SPAN style="FONT-SIZE: x-small; FONT-FAMILY: Verdana"&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Sometimes you don't have control over how the data is saved to a text file. For instance, some items might be saved with quotation marks around words or phrases. If you want to read the file but not show these marks then you'll need a way to remove them. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Like a lot of things, it's actually very easy when you know how. You can use the built-in Replace function of the String class, replacing the marks as you find them. The trick though (and to my mind, the less than totally intuitive bit) is knowing how many quotation marks to use in the first argument of the Replace method's parameters. This is the 'OldChar' parameter, i.e the one you want to replace. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;You would think, wouldn't you, that you could put a Quotation Mark inside a pair of Quotation Marks like this:-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #aaaaaa 1px solid; BORDER-TOP: #aaaaaa 1px solid; MARGIN-LEFT: 10pt; BORDER-LEFT: #aaaaaa 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #aaaaaa 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #aaaaaa 1px solid"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Lucida Console'; BACKGROUND-COLOR: #dbdbdb"&gt;MyString.Replace(&lt;SPAN style="COLOR: #a31515"&gt;"""&lt;/SPAN&gt;,&lt;SPAN style="COLOR: #a31515"&gt; ""&lt;/SPAN&gt;)&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;But if you try that, you will find that it doesn't work. What you actually have to do is include a second Quotation Mark inside the outside ones. In other words, you need four Quotation Marks in a row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #aaaaaa 1px solid; BORDER-TOP: #aaaaaa 1px solid; MARGIN-LEFT: 10pt; BORDER-LEFT: #aaaaaa 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #aaaaaa 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #aaaaaa 1px solid"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Lucida Console'; BACKGROUND-COLOR: #dbdbdb"&gt;MyString.Replace(&lt;SPAN style="COLOR: #a31515"&gt;""""&lt;/SPAN&gt;,&lt;SPAN style="COLOR: #a31515"&gt; ""&lt;/SPAN&gt;)&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;It's only a tiny change, but it will move your mental state from annoyed confusion to enlightened contentment. Or something like that, anyway.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;So putting this together with code that reads from a file and displays the result (minus Quotation Marks) in a ListBox, you have:&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #aaaaaa 1px solid; BORDER-TOP: #aaaaaa 1px solid; MARGIN-LEFT: 10pt; BORDER-LEFT: #aaaaaa 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #aaaaaa 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #aaaaaa 1px solid"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Lucida Console'; BACKGROUND-COLOR: #dbdbdb"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;Private&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; Sub&lt;/SPAN&gt; RemoveQuotes(&lt;SPAN style="COLOR: #0000ff"&gt;ByVal&lt;/SPAN&gt; filename&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; String&lt;/SPAN&gt;,&lt;SPAN style="COLOR: #0000ff"&gt; ByVal&lt;/SPAN&gt; target&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt; ListBox)&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;A StreamReader to fetch the data&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/SPAN&gt; sr&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; New&lt;/SPAN&gt; IO.StreamReader(filename)&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;A string to hold each line as it is read&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/SPAN&gt; line&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; String&lt;/SPAN&gt; =&lt;SPAN style="COLOR: #0000ff"&gt; String&lt;/SPAN&gt;.Empty&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Read from the file&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' As long as there is something left to read&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Do&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; While&lt;/SPAN&gt; sr.Peek &amp;lt;&amp;gt; -1&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Replace the Quotation Marks with Nothing&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;line = sr.ReadLine.Replace(&lt;SPAN style="COLOR: #a31515"&gt;""""&lt;/SPAN&gt;,&lt;SPAN style="COLOR: #a31515"&gt; ""&lt;/SPAN&gt;)&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Add edited text to a ListBox&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;target.Items.Add(line)&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Loop&lt;BR&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Tidy up when finished&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sr.Close()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sr =&lt;SPAN style="COLOR: #0000ff"&gt; Nothing&lt;BR&gt;End&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; Sub&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;If you prefer your code to be broken down into clearer steps, you could do this instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #aaaaaa 1px solid; BORDER-TOP: #aaaaaa 1px solid; MARGIN-LEFT: 10pt; BORDER-LEFT: #aaaaaa 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #aaaaaa 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #aaaaaa 1px solid"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Lucida Console'; BACKGROUND-COLOR: #dbdbdb"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; Sub&lt;/SPAN&gt; RemoveQuotes(&lt;SPAN style="COLOR: #0000ff"&gt;ByVal&lt;/SPAN&gt; filename&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; String&lt;/SPAN&gt;,&lt;SPAN style="COLOR: #0000ff"&gt; ByVal&lt;/SPAN&gt; target&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt; ListBox)&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;A StreamReader to fetch the data&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/SPAN&gt; sr&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; New&lt;/SPAN&gt; IO.StreamReader(filename)&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;A string to hold each line as it is read&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/SPAN&gt; line&lt;SPAN style="COLOR: #0000ff"&gt; As&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; String&lt;/SPAN&gt; =&lt;SPAN style="COLOR: #0000ff"&gt; String&lt;/SPAN&gt;.Empty&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Read from the file&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' As long as there is something left to read&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Do&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; While&lt;/SPAN&gt; sr.Peek &amp;lt;&amp;gt; -1&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Read the next line&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;line = sr.ReadLine&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Replace the Quotation Marks with Nothing&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;line = line.Replace(&lt;SPAN style="COLOR: #a31515"&gt;""""&lt;/SPAN&gt;,&lt;SPAN style="COLOR: #a31515"&gt; ""&lt;/SPAN&gt;)&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Add edited text to a ListBox&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;target.Items.Add(line)&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Loop&lt;BR&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Tidy up when finished&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sr.Close()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sr =&lt;SPAN style="COLOR: #0000ff"&gt; Nothing&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; Sub&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Either way, your quotation marks will be history.&lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9318.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Restricting TextBox Input</title><link>http://blogs.vbcity.com/xtab/archive/2009/06/09/9316.aspx</link><pubDate>Tue, 09 Jun 2009 09:28:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2009/06/09/9316.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9316.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2009/06/09/9316.aspx#Feedback</comments><slash:comments>99</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9316.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9316.aspx</trackback:ping><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;This is a question that seems to come up a lot in the forums:- How can I restrict the TextBox input to numerals, or only a single occurrence of a decimal point, or some other restriction?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;As ever, there are several approaches. If the restriction is something basic, such as numerals only then the easy approach is to use the KeyPress event. What you can do is stop the character from appearing in the TextBox, test to see if it is allowable and, if it is, then allow it to continue. &lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;B&gt;Letters Only&lt;/B&gt;&lt;/U&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp; To take an example which only allows letters of the alphabet, it would look like this:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; TextBox1_KeyPress(&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.Windows.Forms.KeyPressEventArgs) &lt;SPAN style="COLOR: blue"&gt;Handles&lt;/SPAN&gt; TextBox1.KeyPress&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled = &lt;SPAN style="COLOR: blue"&gt;True&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;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; e.KeyChar &lt;SPAN style="COLOR: blue"&gt;Like&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"[A-z]"&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Then&lt;/SPAN&gt; e.Handled = &lt;SPAN style="COLOR: blue"&gt;False&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;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;In this snippet, it is the e.Handled = True which blocks the input temporarily. The next line assesses whether the key press is a letter of the alphabet*, either lower or upper case, and if it is then the handled setting is reversed. This allows the key press to be passed to the TextBox display. If it fails the test, the block on this key press remains. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; * Depending on your locale and keyboard, some other keys are allowed. These include symbols that are used in combination with characters in some languages, such as accents. In most cases this is the behaviour you will want.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;&lt;U&gt;Specific Keys&lt;/U&gt;&lt;/B&gt; &lt;BR&gt;Sometimes you may want to allow certain keys. A common situation is where you will let the user use the Backspace to correct an error when inputting: &lt;/P&gt;
&lt;P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; TextBox2_KeyPress(&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.Windows.Forms.KeyPressEventArgs) &lt;SPAN style="COLOR: blue"&gt;Handles&lt;/SPAN&gt; TextBox2.KeyPress&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled = &lt;SPAN style="COLOR: blue"&gt;True&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;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; e.KeyChar &lt;SPAN style="COLOR: blue"&gt;Like&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"[A-z]"&lt;/SPAN&gt; _&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Or&lt;/SPAN&gt; e.KeyChar = Chr(&amp;amp;H8) &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; e.Handled = &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; &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; &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;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;In this case, it is the Chr(&amp;amp;H8) which identifies and allows the Backspace.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;&lt;U&gt;Numbers Only&lt;/U&gt;&lt;/B&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;Another common requirement is to restrict input to numerals. Of the several possible approaches, using IsNumeric is one of the most straightforward:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; TextBox3_KeyPress(&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.Windows.Forms.KeyPressEventArgs) &lt;SPAN style="COLOR: blue"&gt;Handles&lt;/SPAN&gt; TextBox3.KeyPress&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled = &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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; IsNumeric(e.KeyChar) &lt;SPAN style="COLOR: blue"&gt;Then&lt;/SPAN&gt; e.Handled = &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; &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;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Sometimes that is too restrictive though. What happens if you want to allow the user to enter decimal points or (depending on their locale) commas to break up large numbers? Allowing these individual characters is simple, but there is another potential catch as we will see in a moment:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; TextBox4_KeyPress(&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.Windows.Forms.KeyPressEventArgs) &lt;SPAN style="COLOR: blue"&gt;Handles&lt;/SPAN&gt; TextBox4.KeyPress&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled = &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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; IsNumeric(e.KeyChar) _&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Or&lt;/SPAN&gt; e.KeyChar = &lt;SPAN style="COLOR: #a31515"&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; &lt;SPAN style="COLOR: blue"&gt;Or&lt;/SPAN&gt; e.KeyChar = &lt;SPAN style="COLOR: #a31515"&gt;","&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;&amp;nbsp;&amp;nbsp; e.Handled = &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; &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; &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;B&gt;&lt;U&gt;Only One Decimal Point&lt;/U&gt;&lt;/B&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;In most cases where users are inputting numeric values you will want to restrict them to a single decimal point. The code above will allow multiple entries. Again, there are several solutions, but the following one will usually do the job:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; TextBox5_KeyPress(&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.Windows.Forms.KeyPressEventArgs) &lt;SPAN style="COLOR: blue"&gt;Handles&lt;/SPAN&gt; TextBox5.KeyPress&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled = &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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; e.KeyChar &amp;lt;&amp;gt; &lt;SPAN style="COLOR: #a31515"&gt;"."&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;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; IsNumeric(e.KeyChar) &lt;SPAN style="COLOR: blue"&gt;Then&lt;/SPAN&gt; e.Handled = &lt;SPAN style="COLOR: blue"&gt;False&lt;/SPAN&gt; &lt;SPAN style="COLOR: green"&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; &lt;SPAN style="COLOR: blue"&gt;ElseIf&lt;/SPAN&gt; TextBox5.Text.Contains(&lt;SPAN style="COLOR: #a31515"&gt;"."&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;&amp;nbsp;&amp;nbsp; MessageBox.Show(&lt;SPAN style="COLOR: #a31515"&gt;"Only one decimal point allowed"&lt;/SPAN&gt;)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp; e.Handled = &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; &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; &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;B&gt;&lt;U&gt;Command Keys&lt;/U&gt;&lt;/B&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; If you use any of the previous methods, you will be able to control the standard input keys. But there is another group of keys - Command keys - which won't be excluded by the use of the e.Handled approach. These include such keys as Home, End, Tab, and so on. You may risk alienating your users by excluding these, but there may be times when it is reasonable to do so, in which case you'll need to know how.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;A good way is to intercept the message at the window level and you can do this by overriding the ProcessCmdKey function. Here's how:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Create a new class which inherits from the basic TextBox. Override the ProcessCmdKey function and test for the currently pressed key in a similar way to that used in the earlier examples. If the key is one you want to suppress then you return True and the Windows message pump will ignore it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The following code will be all you need:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&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; CustomTextBox&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;Inherits&lt;/SPAN&gt; System.Windows.Forms.TextBox&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;Sub&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt;()&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Me&lt;/SPAN&gt;.BackColor = Color.Azure&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;Sub&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;Protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Overrides&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt; ProcessCmdKey(&lt;SPAN style="COLOR: blue"&gt;ByRef&lt;/SPAN&gt; msg &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; System.Windows.Forms.Message, &lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; keyData &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; System.Windows.Forms.Keys) &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;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;' Declare a variable of type Keys enumeration &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;' named keyPressed. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;' Cast the msg's WParam as a KeyEnum value &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;' and assign it to the keyPressed variable. &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;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; keyPressed &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; Keys = &lt;SPAN style="COLOR: blue"&gt;CType&lt;/SPAN&gt;(msg.WParam.ToInt32(), Keys)&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;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;' Process the key that is pressed. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;'&amp;nbsp; If keyPressed = Keys.Home Or keyPressed = Keys.End Then Return True&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; keyPressed = Keys.Tab &lt;SPAN style="COLOR: blue"&gt;Then&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;True&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;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;' Return the Command key message&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;MyBase&lt;/SPAN&gt;.ProcessCmdKey(msg, keyData)&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;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Function&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&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;nbsp;&amp;nbsp;The light blue back color is simply to make this sub-classed TextBox look slightly different from the default one for demo purposes, but of course is not a key part of the key checking functionality. As you can see, my example blocks the Tab key. You can add or replace other keys, such as Home and End. &lt;/P&gt;
&lt;P&gt;&lt;B&gt;&lt;U&gt;Multiple Options&lt;/U&gt;&lt;/B&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;Handling the KeyPress is fine if you only have a few TextBoxes for which you are controlling input. If there are going to be a lot of them throughout your application, or if you have different input rules for several TextBoxes, then again it may be worth your while to create your own inherited version.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The following example deals with some of the previous scenarios, but allows the input rule to be selected from an enumeration of choices. The choices used here remain basic, but of course you can expand this idea much further. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Here is the code:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&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; RestrictedTextBox&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Inherits&lt;/SPAN&gt; System.Windows.Forms.TextBox&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;Enum&lt;/SPAN&gt; RestrictionCategory&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;&amp;nbsp; NoRestriction&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; NumeralsOnly&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; LettersOnly&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; AlphanumericOnly&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;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Enum&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;Private&lt;/SPAN&gt; _allowedKeys &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; RestrictionCategory&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;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Property&lt;/SPAN&gt; AllowedKeys() &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; RestrictionCategory&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;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Get&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;Return&lt;/SPAN&gt; _allowedKeys&lt;/P&gt;
&lt;P style="MARGIN: 0px"&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;Get&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;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Set&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; Value &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; RestrictionCategory)&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;Select&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Case&lt;/SPAN&gt; Value&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Case&lt;/SPAN&gt; 1 &lt;SPAN style="COLOR: blue"&gt;To&lt;/SPAN&gt; 3 &lt;SPAN style="COLOR: green"&gt;' One of the enum choices &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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _allowedKeys = Value&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Case&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Else&lt;/SPAN&gt; &lt;SPAN style="COLOR: green"&gt;' No restriction&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _allowedKeys = 0&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;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Select&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;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Set&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;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Property&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;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &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; KeyPressEventArgs)&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;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;MyBase&lt;/SPAN&gt;.OnKeyPress(e)&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;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;'&amp;nbsp; Test whether key is allowed, based on the current choice&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;'&amp;nbsp; from the enum&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;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Select&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Case&lt;/SPAN&gt; _allowedKeys&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;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Case&lt;/SPAN&gt; 1 &lt;SPAN style="COLOR: green"&gt;'Numerals only &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;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; IsNumeric(e.KeyChar) &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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Exit&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&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; &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;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled = &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; &amp;nbsp;&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;&amp;nbsp;&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;Case&lt;/SPAN&gt; 2&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;' Letters Only&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;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; e.KeyChar &lt;SPAN style="COLOR: blue"&gt;Like&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"[A-z]"&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;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Exit&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&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; &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;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled = &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; &amp;nbsp;&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;&amp;nbsp;&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;Case&lt;/SPAN&gt; 3&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;' Alphanumeric&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;If&lt;/SPAN&gt; e.KeyChar &lt;SPAN style="COLOR: blue"&gt;Like&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"[A-z]"&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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Or&lt;/SPAN&gt; IsNumeric(e.KeyChar) &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; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Exit&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&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; &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;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled = &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; &amp;nbsp;&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;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&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;Select&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;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&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;nbsp;&amp;nbsp;The key areas are the enumeration which is called RestrictionCategory. These are automatically assigned values from 0 to 3. The Property AllowedKeys and its backing Field carry out the standard roles of a Property, the user being able to set the AllowedKeys property in code. (You could improve this by having the property appear in the Properties Window). &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The core of this class is the overridden OnKeyPress method. This checks for the chosen enumeration and then either allows or applies the blocking filter to the currently pressed key. This works in a very similar way to the individual KeyPress approach used in the earlier examples. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;By default, all keys will be allowed and to set the enumeration of your choice, you simply include code similar to the following somewhere appropriate in your form (I've used the Form Load event for my example):&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&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; Form1_Load(&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.EventArgs) &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;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;Me&lt;/SPAN&gt;.RestrictedTextBox1.AllowedKeys = RestrictedTextBox.RestrictionCategory.NumeralsOnly&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;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;B&gt;&lt;U&gt;Summary&lt;/U&gt;&lt;/B&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;I think that a combination or extension of any of the above approaches will enable you to control exactly what you will allow the user to input into a TextBox. &lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9316.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>My Blogs Are Moving to New Site</title><link>http://blogs.vbcity.com/xtab/archive/2008/12/08/9212.aspx</link><pubDate>Mon, 08 Dec 2008 16:13:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/12/08/9212.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9212.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/12/08/9212.aspx#Feedback</comments><slash:comments>62</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9212.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9212.aspx</trackback:ping><description>&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp; VBCity will be looking very different&amp;nbsp;soon, as we change to the new Community Server based model.&amp;nbsp;&amp;nbsp; In advance of that change, our blogs are moving to a new area.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;From now on, you can view my latest blogs both here and&amp;nbsp;at the new site &lt;/FONT&gt;&lt;A href="http://cs.vbcity.com/blogs/xtab/default.aspx"&gt;&lt;FONT face=Verdana&gt;here&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana&gt;.&amp;nbsp; Once the new site is fully up and running, all new blog items will be posted on the new area only.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;All previous blogs from VBCity Bloggers will still be available and hopefully will continue to rank highly in search engine results.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt; &lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9212.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>How To Get the Icon from an Application</title><link>http://blogs.vbcity.com/xtab/archive/2008/11/27/9208.aspx</link><pubDate>Thu, 27 Nov 2008 18:00:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/11/27/9208.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9208.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/11/27/9208.aspx#Feedback</comments><slash:comments>165</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9208.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9208.aspx</trackback:ping><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;This question comes up every so often in the forums and it's one of those things that you'd think should be easy, but actually isn't. Well, it isn't until you know how - like most things in developer land. The question is usually something along the lines of &lt;I&gt;"How can I grab the icon from an MS Word file?"&lt;/I&gt; The application may not necessarily be MS Word, but the that's the general thrust of it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The only approach I have successfully used in this kind of scenario is the SHGetFileInfo API*. This API will grab the icon from an application. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Here's the way I do it: First, Import Runtime.InteropServices into your Windows Form. &lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e0e0e0"&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt; System.Runtime.InteropServices&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Next, the API code, which goes at the top of the form, outside of any procedures:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e0e0e0"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Structure&lt;/FONT&gt; SHFILEINFO&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Public&lt;/FONT&gt; hIcon&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; IntPtr&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Public&lt;/FONT&gt; iIcon&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Public&lt;/FONT&gt; dwAttributes&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)&amp;gt; _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Public&lt;/FONT&gt; szDisplayName&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)&amp;gt; _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Public&lt;/FONT&gt; szTypeName&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Structure&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Declare&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Auto&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Function&lt;/FONT&gt; SHGetFileInfo&lt;FONT color=#0000ff&gt; Lib&lt;/FONT&gt;&lt;FONT color=#a31515&gt; "shell32.dll"&lt;/FONT&gt; _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; pszPath&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;, _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ByVal&lt;/FONT&gt; dwFileAttributes&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt;, _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ByRef&lt;/FONT&gt; psfi&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; SHFILEINFO, _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ByVal&lt;/FONT&gt; cbFileInfo&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt;, _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ByVal&lt;/FONT&gt; uFlags&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt;)&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; IntPtr&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; hImgSmall&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; IntPtr&lt;FONT color=#008000&gt; 'The handle to the system image list.&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Const&lt;/FONT&gt; SHGFI_ICON&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Long&lt;/FONT&gt; = &amp;amp;H100&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Const&lt;/FONT&gt; SHGFI_SMALLICON&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Long&lt;/FONT&gt; = &amp;amp;H1&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Note the two Constants at the end there being declared as Long. You'll usually find you have to tweak old API code to fix these kind of lines if you have Option Strict On.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;To demonstrate this API in action, here is how I've structured it: 
&lt;UL&gt;
&lt;LI&gt;I have added a button (btnSelectFile) to the Form.&lt;BR&gt;
&lt;LI&gt;When this button is clicked, an OpenFileDialog is created to allow the user to select a file.&lt;BR&gt;
&lt;LI&gt;Once a file has been selected, the file name is passed to a function that will dig out the icon for that file type and pass it back. &lt;BR&gt;
&lt;LI&gt;For demo purposes, the Form's default icon will be replaced with this selected one. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Here is the code in the function that grabs the icon:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e0e0e0"&gt;&lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt; GetIcon(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; filename&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;)&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Icon&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; shinfo&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; SHFILEINFO&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;shinfo =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; SHFILEINFO&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'Get the small icon.&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hImgSmall = SHGetFileInfo(filename, 0, shinfo, _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Marshal.SizeOf(shinfo), _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SHGFI_ICON&lt;FONT color=#0000ff&gt; Or&lt;/FONT&gt; SHGFI_SMALLICON)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Return&lt;/FONT&gt; System.Drawing.Icon.FromHandle(shinfo.hIcon)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Function&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;And here is that function being called from a Button Click, the icon then being shown as the Form's icon:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e0e0e0"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; btnSelectFile_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Object,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; btnSelectFile.Click&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; ofd&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; OpenFileDialog&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt; ofd.ShowDialog = Windows.Forms.DialogResult.OK&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Me&lt;/FONT&gt;.Icon = GetIcon(ofd.FileName)&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;That's pretty much all there is to it, if you are happy to plonk the icon on the form like that. But in most cases you'll probably want something a bit more reusable. One easy way to store the grabbed icon is to put it in an ImageList.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Assuming that you are only going to deal with one icon at a time, you can do something like this:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e0e0e0"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; myIcons&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; ImageList&lt;BR&gt;myIcons.Images.Add(System.Drawing.Icon.FromHandle(shinfo.hIcon))&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Now that you have that icon safely stored in the ImageList, you can access the ImageList in the usual way if you want to place the icon image elsewhere. And of course you can change the above code slightly to allow for more than one icon to be added to the ImageList and used as required.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Finally, if you need to switch the icon into Bitmap format you can use the handy ToBitmap method:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e0e0e0"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; bmp&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Bitmap = System.Drawing.Icon.FromHandle(shinfo.hIcon).ToBitmap&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;It is possible then to save the Bitmap by using the Bitmap's Save method. However, I have found this to be a bit hit-and-miss, with it sometimes throwing a generic GDI+ error when I tried to recall it for use by means of the Image.FromFile method. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;I hope that if you need to grab an image from an application that you find this article useful.&lt;/P&gt;---------------------------------------------------------------------- 
&lt;P&gt;&amp;nbsp;&amp;nbsp;* There is the much more hopeful sounding ExtractIcon API which I have used to extract the icon of a currently running VB application, but haven't managed to port the logic over to the kind of file selection scenario described here.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9208.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>WPF: How To Hide the Minimize and Maximize Buttons</title><link>http://blogs.vbcity.com/xtab/archive/2008/10/22/9189.aspx</link><pubDate>Wed, 22 Oct 2008 09:17:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/10/22/9189.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9189.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/10/22/9189.aspx#Feedback</comments><slash:comments>108</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9189.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9189.aspx</trackback:ping><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;For some time now, I've been promising myself that one day I will create a&amp;nbsp;&amp;nbsp;list of what I like&amp;nbsp;to&amp;nbsp;think of as "WPF MIA" - those basic elements&amp;nbsp;that I am totally familiar with in WindowsForms, but which somehow seem to have disappeared into thin air in WPF.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;In many cases of course the tools are still there; it's just that the name has changed.&amp;nbsp;&amp;nbsp; Here's an example:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;In Windows Forms, you can hide the Minimize and Maximize buttons by setting the MinimizeBox or MaximizeBox to False&amp;nbsp; I guess that this is most people's favourite way of doing this.&amp;nbsp;If you dig deeper into the Properties Window though, there is also the &amp;nbsp;FormBorderStyle property.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; I used to think that this property was only good for things like changing&amp;nbsp;the border style, say, from 3D to single line.&amp;nbsp; But actually if you choose FixedToolWindow or SizableToolWindow then you automatically get a Form that only has the close button - no Min or Max.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; So what about WPF then? Does it have the same properties available?&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp;If you've spent any time on WPF, you'll know that that's really a rhetorical question because you can almost guarantee that things will have changed.&amp;nbsp;Now though&amp;nbsp;you have a WindowStyle property which you can use to set the Window's border style, including showing or hiding the Max and Min buttons.&amp;nbsp; To have those buttons hidden, you need to choose the &lt;STRONG&gt;ToolWindow&lt;/STRONG&gt; option.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; As far as&amp;nbsp;I know, there isn't a property setting that will hide the individual buttons&amp;nbsp;in in WPF.&amp;nbsp; At first I found this a bit surprising.&amp;nbsp;&amp;nbsp; However,&amp;nbsp; as I've become more familiar with WPF and its ability to morph its Windows into almost any conceivable style and shape, I can see that creating your own Window style is so easy that maybe those properties would have been almost redundant anyway.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9189.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>How To Navigate Around Your Code in Visual Studio</title><link>http://blogs.vbcity.com/xtab/archive/2008/07/08/9126.aspx</link><pubDate>Tue, 08 Jul 2008 15:48:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/07/08/9126.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9126.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/07/08/9126.aspx#Feedback</comments><slash:comments>82</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9126.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9126.aspx</trackback:ping><description>&lt;FONT face=Verdana&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;One of the things that I've always found a bit frustrating as a project grows larger and larger is that it gets more difficult to navigate backwards and forwards between chunks of code that you've recently accessed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; Now I know that Visual Studio has some built in navigation hot keys:&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;B&gt;Ctrl and Minus&lt;/B&gt; to go back to the last point, &lt;BR&gt;&lt;B&gt;Ctrl-Shift-Minus&lt;/B&gt; to go forward again are the most useful, &lt;/P&gt;
&lt;P&gt;&lt;BR&gt;but it can still be a bit laborious when you're trying to flit between several sets of code blocks and you don't particularly want to move in a linear or consecutive way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Step forward that very clever little Add-In (well, not so little really!) &lt;B&gt;CodeSMART&lt;/B&gt;. For some reason I'd stopped using it when I upgraded Visual Studios a while back, but when I was looking around for an answer to this problem I remembered that amongst its many little tools is the Code Explorer. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; One of the elements included in the Code Explorer window is a history of the last 20 procedures you've visited. So not only can you&amp;nbsp;move backwards and forwards one step at a time, you can of course jump to any of the saved locations just by double clicking on a choice in the list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/CodeExplorer.jpg" &lt;P &amp;gt;&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;It has some smart features (well, it is Code&lt;EM&gt;Smart &lt;/EM&gt;after all!), one of which is that it greys out any history items from other files - which helps you to very quickly identify those in your current file that you may be looking for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; And best of all? It persists your history list for each individual Visual Studio Solution, so that the next time you access a particular project Code Explorer will be repopulated with the relevant history for that project. How cool is that? &lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;The Code Explorer does have additional features, such as a fast and neat search tool to take you to procedures that match search criteria, together with what's effectively a Favorites store, the Workbench, where you can keep tabs on items you're particularly interested in. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Of course, CodeSMART has a ton of other features and tools, so if you want to check it out you can download a trial version from their &lt;A href="http://www.axtools.com"&gt;website here&lt;/A&gt;.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9126.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>