<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>VB 2005: How To Print With Word Wrapped Lines</title><link>http://blogs.vbcity.com/xtab/archive/2008/04/08/9021.aspx</link><pubDate>Tue, 08 Apr 2008 17:06:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/04/08/9021.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/9021.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2008/04/08/9021.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/9021.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/9021.aspx</trackback:ping><description>&lt;FONT face=verdana size=2&gt;
&lt;P&gt;&lt;U&gt;Introduction&lt;/U&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;Printing can often be a bit of a tricky task in .NET. Some things are more easily resolved than others though.&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;I saw a post recently that raised a problem that seems to occur fairly regularly: You want to print a large chunk of text to the printer, but VB seems to want to print everything all on one long line - even when this means that a lot of the text will be truncated or "printed" invisibly into thin air! &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Fortunately, this is one of the cases where the solution is relatively easy. Essentially, what you do is set a rectangle whose width represents the width of your printed page (or any lesser width you prefer). You then pass this rectangle to the DrawString method and it will obediently print within those boundaries.&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Solution&lt;/U&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; Here's the basic solution: &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f2f2f2"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Imports&lt;/FONT&gt; System.Drawing.Printing&lt;BR&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;Public&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Class&lt;/FONT&gt; frmPrint&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' The document you will use to print on&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; DocPrint&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; PrintDocument&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Set font for printing&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; fnt&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Font =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; Font(&lt;FONT color=#a31515&gt;"Verdana"&lt;/FONT&gt;, 12, FontStyle.Regular)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;'&amp;nbsp;&amp;nbsp;The printing is fired with a button click event&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button1_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Object,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; Button1.Click&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Print the textbox content&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DocPrint =&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; PrintDocument&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' A handler for it&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AddHandler&lt;/FONT&gt; DocPrint.PrintPage,&lt;FONT color=#0000ff&gt; AddressOf&lt;/FONT&gt; PrintTheTextBox1&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Print the document&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DocPrint.Print()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;The procedure that does the printing&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; PrintTheTextBox1(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; PrintPageEventArgs)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'Set a rectangle to bound the&amp;nbsp;&amp;nbsp;printing&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; PrintRect&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; RectangleF&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Width = 800&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Height = 1000&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Print the text from the TextBox:&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(TextBox1.Text, fnt, Brushes.Black, PrintRect)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;BR&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;/U&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Yes, But.....&lt;/U&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;That's OK as far as it goes, but it does suffer from one flaw. When you check your printed page you'll find that the text is stuffed right up at the top left of the paper and each line extends fully across to the very last few millimetres at the right hand side. Now, it may be that paper saving is high on your agenda, in which case you'll be happy. But generally you will probably want a layout that's a bit more generous with its whitespace.&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; The trouble is that there isn't an overload that allows you to pass in the Rectangle &lt;I&gt;&lt;B&gt;and&lt;/B&gt;&lt;/I&gt; the start position for the printing. Is that going to stump us? Nah, of course not! &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;If we go back to the Rectangle itself, there is a constructor overload that allows you to pass in the x and y values as well as the width and height. The x and y values will of course be used as the left and top points respectively of the rectangle as it is positioned on the printed page. So this variation will get the start position, the text line width and text block height all sorted:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f2f2f2"&gt;&lt;FONT color=#0000ff&gt;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; PrintTheTextBox2(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; PrintPageEventArgs)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'Set a rectangle to bound the&amp;nbsp;&amp;nbsp;printing&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; PrintRect&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; RectangleF(30, 100, 750, 1000)&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Assign its Left, Top, Width and Height values&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Print the text from the TextBox:&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(TextBox1.Text, fnt, Brushes.Black, PrintRect)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;While I was working on that, I remembered that the Rectangle has a useful little method named Offset (something you would normally use when drawing actual rectangles in a graphics based task). However, by using this method and adjusting the values that are passed so that the top left of the rectangle is shunted down and to the right, we can get nearer to the layout we desire. If you chose to use this alternative route, it would look like this: &lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f2f2f2"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; PrintRect&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; RectangleF&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Width = 800&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Height = 1000&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Offset(25, 60)&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Show Me The Width&lt;/U&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;It probably won't have escaped your notice that I very conveniently just happened to know what width and height I needed for the text to span across the page. Many times you will have this information to hand, but if you don't, what do you do then? Answer: You calculate it by querying the PrintDocument's PageSettings, find the width, height and margins and use those. Here's the code: &lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f2f2f2"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; PrintTheTextBox3(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; PrintPageEventArgs)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'Set a rectangle to bound the&amp;nbsp;&amp;nbsp;printing&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; PrintRect&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; RectangleF&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;With&lt;/FONT&gt; DocPrint.DefaultPageSettings&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Width = .PaperSize.Width - (.Margins.Left + .Margins.Right)&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Height = .PaperSize.Height&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PrintRect.Offset(.Margins.Left, .Margins.Top)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; With&lt;/FONT&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' Print the text from the TextBox:&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.Graphics.DrawString(TextBox1.Text, fnt, Brushes.Black, PrintRect)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Summary&lt;/U&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Although there are many more tweaks we could add to this approach, I think that the above will cover many common situations. If text trimming at the end of lines is something you need to refine to the nth degree then you should check out the StringFormat class in general and the StringTrimming enumeration in particular. There are several options available there. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Don't forget of course that you may not need to hand code your printing processes at all. There is a free Power Pack available which will print the contents of a form with minimum effort on your part. This will often be all that you need, but if not then at least you now know how to package up a bundle of text and send it to the printer to be laid out in a way that suits your requirements. &lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/9021.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>VB2005: How To Remove an Item from a ListBox</title><link>http://blogs.vbcity.com/xtab/archive/2008/02/19/8982.aspx</link><pubDate>Tue, 19 Feb 2008 17:26:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2008/02/19/8982.aspx</guid><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;This is something that seems to come up fairly often in the Forums. Someone wants to test for a particular value in a ListBox and if the list does (or doesn't) contain the value then that item is to be deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;One common way of doing this would be to use the Contains function to test if the target character or substring exists in the ListBox.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; This isn't a particular difficult task, but the thing that usually catches most beginners out here is that you have to traverse the ListBox items in reverse order. Well, you do if you actually end up deleting any items, anyway. The reason being that if you start from the top and work down, then as soon as you delete an item the ListBox's indices get out of sync and an ArgumentOutOfRange Exception will be thrown and you will get a message along the lines of the one shown below: &lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/ArgumentOutOfRange.jpg"&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; In a recent post, a slight variation on this theme was that the check was to be made in one ListBox and if the required search string wasn't found in that first ListBox then not only was the item in the first ListBox to be deleted but also the corresponding item number in a second ListBox was also to be deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Again this is straightforward and the following code will do the job :&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #eaeaea"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button1_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Object,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; Button1.Click&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/FONT&gt; i&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt; = ListBox1.Items.Count - 1&lt;FONT color=#0000ff&gt; To&lt;/FONT&gt; 0&lt;FONT color=#0000ff&gt; Step&lt;/FONT&gt; -1&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Not&lt;/FONT&gt; ListBox1.Items(i).Contains(TextBox1.Text)&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ListBox1.Items.RemoveAt(i)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ListBox2.Items.RemoveAt(i)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;As I say, it's not rocket science, but that traversing upwards trick&amp;nbsp;is something worth knowing for these kind of situations.&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8982.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>VB 2005: How To List Known Colors </title><link>http://blogs.vbcity.com/xtab/archive/2007/11/09/8861.aspx</link><pubDate>Fri, 09 Nov 2007 17:04:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2007/11/09/8861.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/8861.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2007/11/09/8861.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/8861.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/8861.aspx</trackback:ping><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; I saw a couple of unrelated questions&amp;nbsp;on vbCity recently - one was about PropertyInfo&amp;nbsp;&amp;nbsp;and&amp;nbsp; the other was in regard to Colors - and seeing them&amp;nbsp;reminded me about how easy it is to use PropertyInfo to get access to and list all&amp;nbsp;the 141 named Colors.&amp;nbsp; There are of course other ways of doing this, but I just thought it would be a fun approach to use.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; The following&amp;nbsp;example is based on a Windows Form that contains a Button and a ListBox for demo purposes.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; First create an array to hold the Colors once we've&amp;nbsp;dug them out of&amp;nbsp;the Colors structure:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #f2f2e6 1px solid; BORDER-TOP: #f2f2e6 1px solid; MARGIN-LEFT: 13pt; BORDER-LEFT: #f2f2e6 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #f2f2e6 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #f2f2e6 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: 'Courier New'; BACKGROUND-COLOR: #ffffb7"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; Colorprops()&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Reflection.PropertyInfo&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #ffffb7"&gt;&lt;FONT color=#008000&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp; &lt;FONT face=Verdana size=2&gt;Next, in the button click event:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #f2f2e6 1px solid; BORDER-TOP: #f2f2e6 1px solid; MARGIN-LEFT: 13pt; BORDER-LEFT: #f2f2e6 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #f2f2e6 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #f2f2e6 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: 'Courier New'; BACKGROUND-COLOR: #ffffb7"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button1_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Object,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; Button1.Click&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;To get hold of the&amp;nbsp;&amp;nbsp;Members of the Color Structure&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;the GetProperties method returns an array of properties in the Color structure&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;and we store them in the ColorProps array&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ColorProps =&lt;FONT color=#0000ff&gt; GetType&lt;/FONT&gt;(System.Drawing.Color).GetProperties&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;'&amp;nbsp;&amp;nbsp;For demo purposes, display the contents of the array in a listbox&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;We use the Name property to return the string assigned to each property in turn&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;For&lt;/FONT&gt; x&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt; = 0&lt;FONT color=#0000ff&gt; To&lt;/FONT&gt; ColorProps.Length - 1&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;ListBox1.Items.Add((ColorProps(x).Name))&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; As you can see from the code comments, thanks to the magic of Reflection it's very easy to get an array that contains all the Colors, which are stored as properties in the Color structure.&lt;/FONT&gt;&amp;nbsp;&lt;FONT face=Verdana size=2&gt; This is achieved in the first code line.&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; Then just for demo purposes I've displayed all the names of the Colors&amp;nbsp;in a listbox.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; Of course, if you've gone to the trouble of creating this list you have probably done so because you actually want to &lt;EM&gt;do&lt;/EM&gt; something with the colors.&amp;nbsp; For example, you might want to let the user change the BackColor of the current form.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; The potential problem here is that the listbox now contains a list of &lt;EM&gt;strings&lt;/EM&gt;.&amp;nbsp; I know we tend to look at a list like that and can visualise them as colors, but the compiler isn't quite that clever and needs a little human help.&amp;nbsp;&amp;nbsp; So what we can do is to instruct it to take the selected string from the listbox (which represents the desired color) and turn that back into an actual Color object - and this &lt;EM&gt;can &lt;/EM&gt;then be assigned as the Form's BackColor.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; You might consider using this code&amp;nbsp;to do the job :&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #f2f2e6 1px solid; BORDER-TOP: #f2f2e6 1px solid; MARGIN-LEFT: 13pt; BORDER-LEFT: #f2f2e6 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #f2f2e6 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #f2f2e6 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: 'Courier New'; BACKGROUND-COLOR: #ffffb7"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; ListBox1_SelectedIndexChanged(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; ListBox1.SelectedIndexChanged&lt;FONT color=#0000ff&gt;&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;BackColor = Color.FromName(ListBox1.SelectedItem.ToString)&lt;FONT color=#0000ff&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; and it will work ..... &amp;nbsp;up to a point.&amp;nbsp;&amp;nbsp; However, the first item in the list is the Transparent property, which is a valid Color in many situations but sadly not one that a Windows Form will accept as its BackColor.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; At the other end of the list you will see other Color properties, such as R, G, B, A, IsKnownColor and so on.&amp;nbsp;&amp;nbsp; Again these&amp;nbsp;more obviously are&amp;nbsp;not going to be valid BackColor properties for the Form.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; As always, alternative solutions are available.&amp;nbsp;&amp;nbsp; Perhaps the easiest one would simply be to wrap the code in an Exception Handler&amp;nbsp;which will then gracefully and silently ignore any&amp;nbsp;non-valid choice:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #f2f2e6 1px solid; BORDER-TOP: #f2f2e6 1px solid; MARGIN-LEFT: 13pt; BORDER-LEFT: #f2f2e6 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #f2f2e6 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #f2f2e6 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: 'Courier New'; BACKGROUND-COLOR: #ffffb7"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; ListBox1_SelectedIndexChanged(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; ListBox1.SelectedIndexChanged&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Try&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BackColor = Color.FromName(ListBox1.SelectedItem.ToString)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Catch&lt;/FONT&gt; ex&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; Exception&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Try&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT face=Verdana size=2&gt;Is this a good fix?&amp;nbsp;&amp;nbsp; Probably depends on the scenario you're using it in.&amp;nbsp; If you're planning on exposing this Form to a non-tech savvy user then I'd say it's a pretty poor workaround.&amp;nbsp; They might not understand or be happy that the choice they're making is unacceptable and nothing is happening.&amp;nbsp; Of course you could slip a message box into the Catch block, but personally I think it would be better not to show them unusable choices at all in the first place and so avoid the problem.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; So let's look at how we might do that.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; I suppose you could exclude the first Color (Transparent) and the final nine ("R" to "Name" inclusive) simply by changing the first line of the loop to:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&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; For&lt;/FONT&gt; x &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Integer&lt;/FONT&gt; = 1 &lt;FONT color=#0000ff&gt;To&lt;/FONT&gt; 140&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;but I'm not crazy about that approach, even though it would work just fine.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; My preference would be to try and filter in only those Colors that are system defined Public Shared Colors - that is, the named Colors themselves and effectively excluding the final nine entries in the list which are instance members.&amp;nbsp; Once again Reflection makes this a very easy task.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; The GetProperties method takes BindingFlags as parameters and you can use these to filter the search.&amp;nbsp; In our case we will use the Public and Static BindingFlags.&amp;nbsp; (You don't have to remember all the available Flags as the full enumeration of them will be displayed as soon as you type an opening bracket after "GetProperties"):&amp;nbsp;-&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #f2f2e6 1px solid; BORDER-TOP: #f2f2e6 1px solid; MARGIN-LEFT: 13pt; BORDER-LEFT: #f2f2e6 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #f2f2e6 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #f2f2e6 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: 'Courier New'; BACKGROUND-COLOR: #ffffb7"&gt;&amp;nbsp;&amp;nbsp;ColorProps =&lt;FONT color=#0000ff&gt; GetType&lt;/FONT&gt;(System.Drawing.Color).GetProperties(BindingFlags.Public&lt;FONT color=#0000ff&gt; Or&lt;/FONT&gt; BindingFlags.Static)&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;FONT face=Verdana size=2&gt;If you run the code again you will now only offer the 141 Colors to the user via the ListBox.&amp;nbsp;&amp;nbsp; "Transparent" is of course a valid Color so it's right that it appears in the list of Colors.&amp;nbsp; But because we have refined the task to assign the Colors to the Form's BackColor, we still have to resolve this.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; Again, a quick and acceptable fix would be to use a Try Catch Exception Handler.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; But what if&amp;nbsp;our scenario really was one&amp;nbsp;where we want to allow the&amp;nbsp;user to&amp;nbsp;change the Form's BackColor to Transparent?&amp;nbsp; Well we quite easily do this by intercepting the user's choice and if it turns out to be a choice of "Transparent" then we can manually make the whole form transparent by changing its Opacity property.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; Now, I know this isn't a&amp;nbsp;100% fix for making the BackColor transparent because the transparency effect is applied to the whole form, not just the client area.&amp;nbsp; Unfortunately the ClientRectangle doesn't&amp;nbsp;have its own Opacity property, so I think that this code will be the nearest we can get:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #f2f2e6 1px solid; BORDER-TOP: #f2f2e6 1px solid; MARGIN-LEFT: 13pt; BORDER-LEFT: #f2f2e6 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #f2f2e6 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #f2f2e6 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: 'Courier New'; BACKGROUND-COLOR: #ffffb7"&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; col&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; Color&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;col = Color.FromName(ListBox1.SelectedItem.ToString)&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; col.Name =&lt;FONT color=#a31515&gt; "Transparent"&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Then&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;Me&lt;/FONT&gt;.Opacity = 0.15&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Else&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BackColor = col&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The allowed values for Opacity range from 0 (invisible) to 1 (fully visible).&amp;nbsp; I purposely didn't set the value to 0, because of course this would mean that the user would lose the total form from view and wouldn't even be able to access the title bar to, for example, close the form.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; In fact, I'd be inclined to make the whole deal a bit more user friendly by showing the user a one-time message about how to remove the Transparent setting.&amp;nbsp; So the ListBox selection code would now be:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #f2f2e6 1px solid; BORDER-TOP: #f2f2e6 1px solid; MARGIN-LEFT: 13pt; BORDER-LEFT: #f2f2e6 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #f2f2e6 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #f2f2e6 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: 'Courier New'; BACKGROUND-COLOR: #ffffb7"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; msgshown&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Boolean&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; Sub&lt;/FONT&gt; ListBox1_SelectedIndexChanged(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; ListBox1.SelectedIndexChanged&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/FONT&gt; col&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; Color&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;col = Color.FromName(ListBox1.SelectedItem.ToString)&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; col.Name =&lt;FONT color=#a31515&gt; "Transparent"&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Then&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;Me&lt;/FONT&gt;.Opacity = 0.15&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Me&lt;/FONT&gt;.msgshown =&lt;FONT color=#0000ff&gt; False&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MessageBox.Show(&lt;FONT color=#a31515&gt;"Double Click the form to undo the Transparent setting"&lt;/FONT&gt;,&lt;FONT color=#a31515&gt; "Info"&lt;/FONT&gt;, MessageBoxButtons.OK)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;msgshown =&lt;FONT color=#0000ff&gt; True&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;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Else&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BackColor = col&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;FONT face=Verdana size=2&gt; and in order to implement that reinstatement of full Opacity, you would just need :-&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #f2f2e6 1px solid; BORDER-TOP: #f2f2e6 1px solid; MARGIN-LEFT: 13pt; BORDER-LEFT: #f2f2e6 1px solid; MARGIN-RIGHT: 2pt; BORDER-BOTTOM: #f2f2e6 1px solid"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BORDER-BOTTOM: #f2f2e6 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: 'Courier New'; BACKGROUND-COLOR: #ffffb7"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Form1_DoubleClick(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Me&lt;/FONT&gt;.DoubleClick&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Me&lt;/FONT&gt;.Opacity = 1&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;FONT size=2&gt;&amp;nbsp; So t&lt;/FONT&gt;&lt;FONT face=Verdana&gt;&lt;FONT size=2&gt;hat's it.&amp;nbsp;&amp;nbsp; Dealing with colors (as color objects and not as names that we visualise in our heads as actual colors) can be a bit tricky until you get used to&lt;/FONT&gt; how it works.&amp;nbsp; &lt;FONT size=2&gt;I hope that this relatively simple walkthrough has helped clarify things for you.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8861.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>VB 2005 : How To Move Items Up a ListBox</title><link>http://blogs.vbcity.com/xtab/archive/2007/10/08/8766.aspx</link><pubDate>Mon, 08 Oct 2007 16:41:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2007/10/08/8766.aspx</guid><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;  I had to revisit this problem recently and so had to recall how I did it.  Here's the scenario:  You have a ListBox and a Button.  User selects an item in the ListBox.  If they want to move that item up the list they click the button.  They can keep clicking the button until the item reaches the top of the list if they want to, or just stop when they've reached the desired position anywhere up the list.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;  I don't know of any inbuilt way of doing this, so this was my fix:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;1.  First set the Button's Enabled property to False in Design View (or on Form Load)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;2.  When the user first selects a new item, decide if it is possible for this item to go any higher.   If it's not already at the top of the list then enable the button.&lt;/FONT&gt;&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" onclick="var i=this.sel,j;for(j=0;j&lt;3;j++)this.childNodes[2+j*2].checked=j==i;if(i==this.selold)return;var sh=new Array('none',''),o=this.parentNode;j=i==0?0:1;this.selold=i;o.childNodes[1].style.display=sh[j];o.childNodes[2].style.display=sh[1-j];o=o.childNodes[1];var h =o.offsetHeight;o.style.height=i==1?'150pt':'';if(i==1 &amp;&amp; o.offsetHeight&gt;h)o.style.height=h;" sel="2" selold="2"&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;INPUT onclick=parentNode.sel=0 type=radio&gt;Hide&lt;INPUT onclick=parentNode.sel=1 type=radio&gt;Scroll&lt;INPUT onclick=parentNode.sel=2 type=radio CHECKED&gt;Full&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Lucida Console'; BACKGROUND-COLOR: #efefef"&gt;&lt;FONT color=#0000ff&gt;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; ListBox1_SelectedIndexChanged(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; ListBox1.SelectedIndexChanged&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;    If&lt;/FONT&gt; ListBox1.SelectedIndex &gt; 0&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt; UpButton.Enabled =&lt;FONT color=#0000ff&gt; True&lt;BR&gt;&lt;BR&gt;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="DISPLAY: none; BACKGROUND-COLOR: #efefef"&gt;&lt;B&gt;. . .&lt;/B&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;3.  &lt;FONT face=Verdana size=2&gt;Then make a note of the content to be moved and the element number of the next item up.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;4.  Make sure that the user isn't trying to move the top item up and if they are, then avoid the Exception by exiting the sub, after first disabling the button.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;5.  Temporarily store the text content in a variable.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;6.  Delete (Remove) the item you are about to move. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;7.   You then reinsert the deleted text at the next element higher up the list.   &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;8.  Finally, you reselect and automatically highlight the item the user is moving so that the whole action appears seamless to them.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Here's the Button's code:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt; &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" onclick="var i=this.sel,j;for(j=0;j&lt;3;j++)this.childNodes[2+j*2].checked=j==i;if(i==this.selold)return;var sh=new Array('none',''),o=this.parentNode;j=i==0?0:1;this.selold=i;o.childNodes[1].style.display=sh[j];o.childNodes[2].style.display=sh[1-j];o=o.childNodes[1];var h =o.offsetHeight;o.style.height=i==1?'150pt':'';if(i==1 &amp;&amp; o.offsetHeight&gt;h)o.style.height=h;" sel="2" selold="2"&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;INPUT onclick=parentNode.sel=0 type=radio&gt;Hide&lt;INPUT onclick=parentNode.sel=1 type=radio&gt;Scroll&lt;INPUT onclick=parentNode.sel=2 type=radio CHECKED&gt;Full&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Lucida Console'; BACKGROUND-COLOR: #efefef"&gt;&lt;FONT color=#0000ff&gt;    Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; UpButton_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; UpButton.Click&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;        ' Decide where to move item to : Note next position&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;        Dim&lt;/FONT&gt; NextPosition&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt; = ListBox1.SelectedIndex - 1&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;        ' If we have reached the top (or user selects first item manually)&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;        ' don't attempt the move and disable button&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;        If&lt;/FONT&gt; NextPosition = -1&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;            UpButton.Enabled =&lt;FONT color=#0000ff&gt; False&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;            Exit&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;        End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;        ' Copy the value temporarily&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;        Dim&lt;/FONT&gt; tempstr&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt; = ListBox1.SelectedItem.ToString&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;        ' remove the item and reinsert it one place higher&lt;/FONT&gt;&lt;BR&gt;        ListBox1.Items.Remove(tempstr)&lt;BR&gt;        ListBox1.Items.Insert(NextPosition, tempstr)&lt;BR&gt;&lt;FONT color=#008000&gt;        ' Reselect the newly inserted item to assist the user&lt;/FONT&gt;&lt;BR&gt;        ListBox1.SelectedIndex = NextPosition&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 style="DISPLAY: none; BACKGROUND-COLOR: #efefef"&gt;&lt;B&gt;. . .&lt;/B&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt;  &lt;FONT face=Verdana size=2&gt; It's one of those little things that you feel should be easy, but actually needs a bit of thought to make it work.&lt;/FONT&gt; &lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8766.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Windows Forms - Aligning Two Forms</title><link>http://blogs.vbcity.com/xtab/archive/2007/08/05/8573.aspx</link><pubDate>Sun, 05 Aug 2007 12:15:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2007/08/05/8573.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/8573.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2007/08/05/8573.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/8573.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/8573.aspx</trackback:ping><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; This tip barely qualifies as a Gotcha! but I have seen some people get stuck with it, so thought it worth a mention.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; If you want to control exactly where a second form is placed in relation to another form, you can use code that shifts the second form to either side or above or below the first one.&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; The key is to ensure that you set the StartPosition of the forms to "Manual".&amp;nbsp;&amp;nbsp; If you do this, each form in the project that i set to Manual will automatically be placed at position 0,0 - i.e top left of the screen.&amp;nbsp; (By default this property&amp;nbsp;is set to "WindowsDefaultLocation").&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; So, to take a scenario with two forms, you can leave Form1 as-is and set the location of Form2 relative to Form1.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; For example to align the two forms horizontally but with Form2 placed immediately to the Right of Form1 you could use:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 13pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #f7f7f0"&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; F2&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; Form2&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;F2.Left =&lt;FONT color=#0000ff&gt; Me&lt;/FONT&gt;.Width + 2&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;F2.Top =&lt;FONT color=#0000ff&gt; Me&lt;/FONT&gt;.Top&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;F2.Show()&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT face=Verdana size=2&gt; As you can see the calculation is based on the width of the first form, plus a small margin added for appearance's sake.&lt;/FONT&gt;&amp;nbsp; &lt;FONT face=Verdana size=2&gt;The result&amp;nbsp; is:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/WinFormsAlignment1.png"&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;FONT face=Verdana size=2&gt;It's simple to use the same approach to place the second form anywhere you wish in relation to the first.&amp;nbsp; Below it, for example:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 13pt; 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: #f7f7f0"&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; F2&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; Form2&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;F2.Left =&lt;FONT color=#0000ff&gt; Me&lt;/FONT&gt;.Left&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;F2.Top =&lt;FONT color=#0000ff&gt; Me&lt;/FONT&gt;.Height + 1&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;F2.Show()&lt;FONT color=#a31515&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT face=Verdana size=2&gt;So, just use whatever offset suits your needs, and don't forget to change that StartPosition property first.&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8573.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Try Now, Buy Later - Framework 3.0 Extensions in Visual Studio 2005 </title><link>http://blogs.vbcity.com/xtab/archive/2007/08/01/8565.aspx</link><pubDate>Wed, 01 Aug 2007 11:46:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2007/08/01/8565.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/8565.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2007/08/01/8565.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/8565.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/8565.aspx</trackback:ping><description>&lt;P&gt;&amp;nbsp;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; I was discussing Windows Presentation Foundation (WPF) recently with someone on VBCity.&amp;nbsp;&amp;nbsp; Although many people don't have the time or available capacity to download the huge Visual Studio 2008 (previously known as Orcas) Beta on a development machine, it is still possible to get a foretaste of what's to come.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; If you go to &lt;A href="http://www.microsoft.com/downloads/details.aspx?familyid=F54F5537-CC86-4BF5-AE44-F5A1E805680D&amp;amp;displaylang=en"&gt;this link &lt;/A&gt;you can download the Framework 3.0 Extensions for Visual Studio 2005.&amp;nbsp; Now, it's getting a bit dated and the VS 2008 Betas already have a&amp;nbsp;much better range of tools and support such as Intellisense, but I think &amp;nbsp;it is still worth&amp;nbsp;&amp;nbsp;a look if you are interested in what is coming down the road.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp; The .NET&amp;nbsp; Framework 3.0 SDK can be downloaded from&lt;A href="http://www.microsoft.com/downloads/details.aspx?familyid=7614FE22-8A64-4DFB-AA0C-DB53035F40A0&amp;amp;displaylang=en"&gt; here&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; You will also need to download and install&lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyId=10CC340B-F857-4A14-83F5-25634C3BF043&amp;amp;displaylang=en"&gt; Framework 3.0&lt;/A&gt; if you are&amp;nbsp; running XP on your system (Vista has it installed by default)&amp;nbsp;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; The extensions include WPF and WCF - Windows Communication Foundation.&amp;nbsp; Even if you don't do anything other than poke around with the new stuff, it's interesting to see how WPF, XAML and VB.NET all&amp;nbsp;work together to create some top class user interfaces.&amp;nbsp; (Or some terrible ones if you get carried away with animation, video and clashing colors!)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp; Projects available in VS 2005 are:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;WPF Windows Apps&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;XAML Browser Apps&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;WPF Custom Controls &lt;/FONT&gt;
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;WCF Service&lt;/FONT&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8565.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>VB 2005: How To Hide / Show Multiple TabPages in a TabControl</title><link>http://blogs.vbcity.com/xtab/archive/2007/07/18/8508.aspx</link><pubDate>Wed, 18 Jul 2007 11:19:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2007/07/18/8508.aspx</guid><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt; &lt;U&gt;Introduction&lt;/U&gt;&lt;BR&gt;In the previous parts we looked at ways of hiding and showing a single Tab Page in a TabControl. In some situations you may need to hide and show multiple pages.&lt;/P&gt;
&lt;P&gt; This is still fairly easy to do, but now you need a collection in which to hold the TabPages you have removed. (Remember from the previous descriptions that you are not really &lt;I&gt;hiding&lt;/I&gt; the TabPages, you are in fact completely removing them, but storing a copy in memory that you can later call on to reinstate the page) &lt;/P&gt;
&lt;P&gt;&lt;U&gt;A Collection of Saved TabPages&lt;/U&gt;&lt;BR&gt;In VB2005 the availability of generic collections makes our life very easy and we can create type safe collections automatically. In this case we will create a List and this list will only allow TabPage objects to be stored in it. Here is the code that does this:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e8e8e8"&gt;&lt;FONT color=#008000&gt;    '  A collection of removed TabPages&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;    Dim&lt;/FONT&gt; colRemovedTabs&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; New&lt;/FONT&gt; List(&lt;FONT color=#0000ff&gt;Of&lt;/FONT&gt; TabPage)&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt; &lt;U&gt;Allowing User to Select&lt;/U&gt;&lt;BR&gt;For demonstration purposes we need a way of listing all available TabPages. Whether this list is visible to the user and/or how it is displayed will depend on your particular application. For this article I will list all the TabPages in a standard ListBox control.. This list box will be populated when the form is first loaded: &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: #e8e8e8"&gt;&lt;FONT color=#0000ff&gt;    Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Form1_Load(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Object&lt;/FONT&gt;,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Me&lt;/FONT&gt;.Load&lt;BR&gt;&lt;FONT color=#0000ff&gt;        For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; tabpg&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; TabPage&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; TabControl1.TabPages&lt;BR&gt;&lt;FONT color=#0000ff&gt;            Me&lt;/FONT&gt;.ListBox1.Items.Add(tabpg.Text)&lt;BR&gt;&lt;FONT color=#0000ff&gt;        Next&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;        ListBox1.SelectionMode = SelectionMode.MultiSimple&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;  The final line of code inside that procedure is there simply to ensure that the user can select more than one TabPage at a time.&lt;/P&gt;
&lt;P&gt; Now the user can simply select multiple items (TabPages) from the listbox. &lt;/P&gt;
&lt;P&gt; &lt;U&gt;Removing and Saving&lt;/U&gt;&lt;BR&gt;Once they have made their selection, a button click is used to confirm that those pages should be hidden/removed. The click event code is as follows:&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: #e8e8e8"&gt;&lt;FONT color=#0000ff&gt;    Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button7_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; Button7.Click&lt;BR&gt;&lt;FONT color=#008000&gt;        ' Hide multiple pages&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;        For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; tabpg&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; TabPage&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; TabControl1.TabPages&lt;BR&gt;&lt;FONT color=#0000ff&gt;            For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; item&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; String&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; ListBox1.SelectedItems&lt;BR&gt;&lt;FONT color=#0000ff&gt;                If&lt;/FONT&gt; tabpg.Text = Trim(item)&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;                    colRemovedTabs.Add(tabpg)&lt;BR&gt;                    TabControl1.Controls.Remove(tabpg)&lt;BR&gt;&lt;FONT color=#0000ff&gt;                End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;            Next&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;        Next&lt;BR&gt;&lt;/FONT&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; All we have here is a nested loop. Firstly we loop through all of the TabPages in the TabControl. Inside that loop, we also loop through all the selected items in the listbox. &lt;BR&gt;If we find a match between the selected item and the text (the string used on the Tab of a TabPage) then we know this item needs to be hidden.. &lt;/P&gt;
&lt;P&gt; We store its information in the collection (colRemovedTabs) before removing it from the TabControl. The approach is very similar to that used in the earlier single TabPage examples. &lt;/P&gt;
&lt;P&gt; &lt;U&gt;Reinstating Removed TabPages&lt;/U&gt;&lt;BR&gt;Reinstating the TabPages is also an easy task. We have them all saved in the collection, so we can simply dip in and grab them all.&lt;/P&gt;
&lt;P&gt;Once again I've placed this in a Button Click event for demo purposes:&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: #e8e8e8"&gt;&lt;FONT color=#0000ff&gt;    Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button8_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; Button8.Click&lt;BR&gt;&lt;FONT color=#008000&gt;        '  Reinsert all removed pages&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;        For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; savedTab&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; TabPage&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt; colRemovedTabs&lt;BR&gt;            TabControl1.Controls.Add(savedTab)&lt;BR&gt;&lt;FONT color=#0000ff&gt;        Next&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;        ' Clear the collection as it has now done its work&lt;/FONT&gt;&lt;BR&gt;        colRemovedTabs.Clear()&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;  You will see that I have also cleared out the contents of the collection as the last code task. If you don't do this then further clicks of the button will result in duplicate TabPages being added back to the TabControl; probably not what you want in most situations.&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Summary&lt;/U&gt;&lt;BR&gt;
&lt;P&gt;It is also fairly easy to reinstate only certain hidden TabPages. The code for this is included in the demo solution which you can download from &lt;A href="http://www.xtabvbcity.plus.com/Blogs/TabPages%20Hide%20Demo.zip"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt; The completed project looks like this:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.xtabvbcity.plus.com/Blogs/TabPageDemoUI.png"&gt; &lt;/P&gt;
&lt;P&gt;  So now you have seen how to "hide" and "show" individual or multiple TabPages. As I said at the start of the first part of these articles, this is one of those simple looking tasks that takes a bit more effort than you first think, but really isn't very difficult once you realise that you can work round the limitations.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8508.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>Hiding a TabPage (2) - User's Selection</title><link>http://blogs.vbcity.com/xtab/archive/2007/06/29/8473.aspx</link><pubDate>Fri, 29 Jun 2007 08:33:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2007/06/29/8473.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/8473.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2007/06/29/8473.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/8473.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/8473.aspx</trackback:ping><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;In the previous part I looked at a very simple scenario where the developer knew in advance which TabPage should be hidden and re-shown. Next we'll look at doing the same thing, except that this time the user gets to choose which TabPage is hidden. &lt;/P&gt;
&lt;P&gt;As in the previous example, you're going to need some kind of trigger to kickstart the Hide or Show activity. I've used a button placed outside the TabControl on the form itself, but you could use any other trigger that suits your purpose - e.g. a button or checkbox inside a TabPage. &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="Locations TabPage has focus" src="http://www.xtabvbcity.plus.com/Blogs/FocussedTabPage.jpg"&gt;&lt;/P&gt;
&lt;P&gt;In fact of course it doesn't even have to be wired up to the clicking on a Windows Forms Control - you could hide the TabPage based on some kind of text input from the user.&lt;/P&gt;
&lt;P&gt;Anyway, in this example, the button click code needs to :&lt;BR&gt;
&lt;UL&gt;
&lt;LI&gt;Identify which TabPage currently has focus &lt;BR&gt;
&lt;LI&gt;Then store this TabPage's metadata in the TempTab variable &lt;BR&gt;
&lt;LI&gt;Before finally Removing the TabPage from the TabControl. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Here is the code I've 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="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e8e8e8"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button3_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; Button3.Click&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Identify the TabPage to be removed&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;For&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Each&lt;/FONT&gt; tp&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; TabPage&lt;FONT color=#0000ff&gt; In&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Me&lt;/FONT&gt;.TabControl1.TabPages&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt; tp.Focus =&lt;FONT color=#0000ff&gt; True&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TempTab = tp&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TabControl1.TabPages.Remove(tp)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Exit&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; For&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;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I think that code is pretty straightforward. To identify the single Tabpage that currently has focus you have to enumerate through all of them until you find the right one. &lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp; For Each tp As TabPage In Me.TabControl1.TabPages &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If tp.Focus = True Then&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Once you've located it, store it in TempTab;&lt;BR&gt;then remove it:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;TempTab = tp &lt;BR&gt;&amp;nbsp;&amp;nbsp; TabControl1.TabPages.Remove(tp)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Finally (and crucially) don't forget to jump out of the loop once you have found your TabPage and taken the appropriate action on it.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color=#0000ff size=2&gt;&amp;nbsp;&amp;nbsp; Exit For&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Why? Because what happens when you remove a Tabpage is that Focus automatically shifts to the first TabPage in the TabControl. Therefore if you don't jump out of the loop, it will keep finding TabPages that have focus, one after the other, and will keep removing them until all are gone. You won't get an Exception, but you won't have any TabPages either! (And only the most recently deleted one will be available for reshowing; you will have permanently lost all the others.)&lt;/P&gt;
&lt;P&gt;OK, the code to show the TabPage again is simple:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;TabControl1.TabPages.Add(TempTab) &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;As mentioned earlier, you will get unwanted results if you allow this button to be clicked more than once consecutively, so I advise that you add further code to avoid this happening. I will post up a downloadable sample project that includes this, but for conciseness won't deal with it in this blog. &lt;/P&gt;
&lt;P&gt;So that would seem to fix the "Remove user's choice of TabPage" problem easily enough. But what if for some reason it's important to reinstate the saved tabPage back in the TabControl in its original position?&lt;/P&gt;
&lt;P&gt;All sorts of ways of doing this spring to mind, but just because I want to keep this part really uncomplicated, I'm simply going to set up a second variable, call it TempTabIndex and make it an Integer Type. &lt;BR&gt;We will save the TabPage to TempTab as before and save the index value to TempTabIndex. &lt;/P&gt;
&lt;P&gt;Here's the code:&lt;/P&gt;
&lt;P&gt;
&lt;DIV style="MARGIN-LEFT: 10pt; MARGIN-RIGHT: 2pt"&gt;
&lt;DIV style="FONT-SIZE: 10pt"&gt;&lt;B&gt;Code &lt;/B&gt;&lt;A onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');" href="javascript:"&gt;Copy &lt;/A&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 15px; OVERFLOW: auto; FONT-FAMILY: 'Courier New'; BACKGROUND-COLOR: #e8e8e8"&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Identify the Index Number of the selected TabPage&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;With&lt;/FONT&gt; TabControl1&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/FONT&gt; i&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Integer&lt;/FONT&gt; = 0&lt;FONT color=#0000ff&gt; To&lt;/FONT&gt; .TabPages.Count - 1&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt; .TabPages(i).Focus =&lt;FONT color=#0000ff&gt; True&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Then&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Save the details of this TabPage&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TempTab = .TabPages(i)&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Save its index number&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TempTabIndex = i&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Remove it&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.TabPages.RemoveAt(i)&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;nbsp;&amp;nbsp;Stop looking for Tabpages with Focus&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Exit&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; For&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; If&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; With&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;As you can see, instead of enumerating through the TabPages what we do here is iterate through the TabControl's TabPages collection, an index at a time. &lt;BR&gt;Once we find the TabPage that has focus, we can also easily grab the current index number and store that, as well as storing the tabPage data in the same way as we did previously. &lt;/P&gt;
&lt;P&gt;And again as in the earlier example, it's important to ensure that you jump out of that loop as soon as you've completed the work on the one TabPage we're interested in.&lt;/P&gt;
&lt;P&gt;Reinserting the TabPage back into its correct original position simply requires us to pass in the Index number and the TabPage name to the TabControl's.TabPages Insert 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: #e8e8e8"&gt;&lt;FONT color=#0000ff&gt;Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button6_Click(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; sender&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.Object,&lt;FONT color=#0000ff&gt; ByVal&lt;/FONT&gt; e&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; System.EventArgs)&lt;FONT color=#0000ff&gt; Handles&lt;/FONT&gt; Button6.Click&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;'&amp;nbsp;&amp;nbsp;Reinsert TabPage in correct position&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TabControl1.TabPages.Insert(TempTabIndex, TempTab)&lt;FONT color=#0000ff&gt;&lt;BR&gt;&lt;BR&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;The project described in these first two parts now looks something like this:&lt;/P&gt;
&lt;P&gt;&lt;IMG alt="Single TabPages" src="http://www.xtabvbcity.plus.com/Blogs/SingleTabPages.jpg"&gt;&lt;/P&gt;
&lt;P&gt;In the next part I want to look at a more complex situation; one where more than one TabPage is hidden at a time.&lt;/P&gt;&lt;/ Font&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8473.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>VB 2005:  How To Hide and Show a TabPage in a TabControl</title><link>http://blogs.vbcity.com/xtab/archive/2007/06/25/8465.aspx</link><pubDate>Mon, 25 Jun 2007 11:10:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2007/06/25/8465.aspx</guid><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT face=Arial&gt;AfterNote:   There are now three blog items on this topic, each showing a different aspect  If you want to view them all as one article, follow &lt;A href="ftp://www.xtabvbcity.plus.com/htdocs/Articles/Hide%20TabPages%20Article.htm"&gt;this link&lt;/A&gt;)&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT face=Arial&gt; &lt;/FONT&gt;&lt;/EM&gt;Sometimes you may want to hide a TabPage from the user's view, but you don't want to permanently remove it.   Now, if you've checked out the TabPage's methods you will see that it does have a "Hide" method in VB 2005. That's great, but the trouble is if you try using it, it doesn't work!   You won't get an Exception, but you won't get a disappearing Tab Page either. (And if you're using an earlier version of VB.NET you have a choice of Hide or the Visible property, but neither will have the effect you want if you try using them.) &lt;/P&gt;
&lt;P&gt; Actually, the way to hide a TabPage is to access it through its parent container - the TabControl itself. We'll take a look at how to do this. (and possibly how to sidestep other problems along the way).&lt;/P&gt;
&lt;P&gt; Let's start with the situation where you know that you are only going to want to hide and show one specific TabPage in a TabControl which has a set number of TabPages. &lt;/P&gt;
&lt;P&gt;First, you need somewhere to store the info about the TabPage. In this scenario a simple variable will do:-&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: #e8e8e8"&gt;&lt;FONT color=#008000&gt;    '  A temp variable to hold a single hidden Page&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;    Dim&lt;/FONT&gt; TempTab&lt;FONT color=#0000ff&gt; As&lt;/FONT&gt; TabPage&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;The code goes at the top of the form, outside any procedures.&lt;/P&gt;
&lt;P&gt;Next, in the event you are using to trigger the hide action, (maybe a button click - see the "Hide TabPage 2" button in the screenshot below),&lt;/P&gt;
&lt;P&gt;&lt;IMG alt="Simple preselected TabPage" src="http://www.xtabvbcity.plus.com/Blogs/Preselected%20Tab.jpg"&gt;&lt;/P&gt;
&lt;P&gt;you store the chosen TabPage in that TempTab variable.&lt;BR&gt;And finally you use the Remove or RemoveAt method to physically delete the TabPage from within the TabControl.&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: #e8e8e8"&gt;TempTab = TabControl1.TabPages(1)&lt;BR&gt;TabControl1.TabPages.RemoveAt(1)&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Because we knew in advance that we would be hiding and removing the second TabPage (whose Index is 1, of course), we can use the RemoveAt method. This is the most straightforward approach to use in this situation. So the situation currently is that the TabPage no longer exists in the TabControl, but you have squirrelled away a copy in that TempTab variable for later use. &lt;/P&gt;
&lt;P&gt;What we need to look at next is reinstating the hidden TabPage when needed. Again this is quite simple. We insert the TabPage back into the TabControl, taking care to insert it in its proper original position. &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: #e8e8e8"&gt;&lt;FONT color=#0000ff&gt;  Private&lt;/FONT&gt;&lt;FONT color=#0000ff&gt; Sub&lt;/FONT&gt; Button2_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; Button2.Click&lt;BR&gt;        TabControl1.TabPages.Insert(1, TempTab)&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;The parameters of that Insert method are the Index to Insert at (Index 1, in this case, as that's where it came from) and the TabPage itself, as identified by its name. (Don't confuse its name with its Text. In this example they happen to be the same, but in a real world example this is unlikely to be the case).&lt;/P&gt;
&lt;P&gt;If you're wondering about the purpose of the TextBox in that screenshot, I put it in so you could really prove to yourself that this does work. &lt;/P&gt;
&lt;P&gt;If you create a Form as shown and enter some text into the TextBox at runtime and then Save/Remove the TabPage you want to be sure that any data input by the user really does get saved too. So, run the project, enter your name, hit the first button to hide the TabPage, then hit the second one to show it again. As you will see, even dynamically entered data, such as the text in the textBox, is saved and reinstated. &lt;/P&gt;
&lt;P&gt;To re-emphasise the point I made earlier about the &lt;I&gt;Name&lt;/I&gt; of the TextBox being used as the key parameter, not the &lt;I&gt;Text&lt;/I&gt; that the user sees on the Tab, take a look at the following screenshot:&lt;/P&gt;
&lt;P&gt;&lt;IMG alt=" TabPage Text Changed" src="http://www.xtabvbcity.plus.com/Blogs/TabPageTextChanged.jpg"&gt;&lt;/P&gt;
&lt;P&gt;The same code as used above will work perfectly with these TabPages, because I have only changed the Text and not the Name.&lt;/P&gt;
&lt;P&gt;By the way, if you don't need or want to reinsert the TabPage back in the position you took it out from, you can simply have it placed at the end of the current collection by using:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;    TabControl1.TabPages.Add(TempTab)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;instead. &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="Added at end" src="http://www.xtabvbcity.plus.com/Blogs/TabPageAdded.jpg"&gt;&lt;/P&gt;
&lt;P&gt;However, one little Gotcha to watch out for (it's obvious when you think about it, but ...) is that if you use the "Show" button more than once then you will get multiple TabPages added; each of which has the same Text and - more importantly - each of which has the same Name. &lt;BR&gt;However, not so obvious, and even worse, is that this multiplicity of Tabpages with the same name gives the compiler such a headache that in fact you will lose all the controls and formatting from all copies of that TabPage. Not something that you would normally want to happen, so you should include code to restrict the Hiding/Showing to single instances.&lt;/P&gt;
&lt;P&gt;So far, so good. In the next part we'll look at a slightly more complex scenario. &lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8465.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Ged Mead</dc:creator><title>VB 2005: The "Missing" Sub Main Problem</title><link>http://blogs.vbcity.com/xtab/archive/2007/06/19/8453.aspx</link><pubDate>Tue, 19 Jun 2007 13:02:00 GMT</pubDate><guid>http://blogs.vbcity.com/xtab/archive/2007/06/19/8453.aspx</guid><wfw:comment>http://blogs.vbcity.com/xtab/comments/8453.aspx</wfw:comment><comments>http://blogs.vbcity.com/xtab/archive/2007/06/19/8453.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.vbcity.com/xtab/comments/commentRss/8453.aspx</wfw:commentRss><trackback:ping>http://blogs.vbcity.com/xtab/services/trackbacks/8453.aspx</trackback:ping><description>&lt;FONT face=Verdana size=2&gt;
&lt;P&gt;As I've said on many occasions in the past, it's those sometimes tiny little Gotchas that will cause you the greatest amount of aggravation. Here's one that I've been meaning to blog for a while now. It's a question/problem that comes up fairly often and it usually goes something like this:- &lt;/P&gt;
&lt;P&gt;&lt;I&gt;"In a Windows Forms project, I add a module that has a Sub Main procedure in it. I want to use this Sub Main as the StartUp Object in the application. But when I look in the project properties, I don't see "Sub Main" as a choice. " &lt;/I&gt;&lt;BR&gt;&lt;BR&gt;&lt;IMG alt="No Sub Main Listed" src="http://www.xtabvbcity.plus.com/Blogs/NoSubMainListed.jpg"&gt; &lt;BR&gt;&lt;I&gt;"It used to be here in earlier editions - where has it gone to and how can I access it? "&lt;/I&gt; &lt;/P&gt;
&lt;P&gt;The solution to this particular Gotcha is very easy. &lt;/P&gt;
&lt;P&gt;Open up the Project Properties page. &lt;BR&gt;Home in on the "Enable Application Framework" CheckBox. &lt;BR&gt;&lt;BR&gt;&lt;IMG alt="Enable Application Framework" src="http://www.xtabvbcity.plus.com/Blogs/EnableApplicationFramework.jpg"&gt; &lt;BR&gt;Uncheck this option (i.e. disable it)&lt;/P&gt;
&lt;P&gt;Now if you view the "Startup Form" combo you'll see that it has changed so that it reads "Startup Object". &lt;BR&gt;Click on the combo and all the forms plus the module and the Sub Main inside that module will all be available as Startup objects.&lt;/P&gt;&lt;BR&gt;&lt;IMG alt="Sub Main Now Listed" src="http://www.xtabvbcity.plus.com/Blogs/SubMainListed.jpg"&gt; 
&lt;P&gt;It's one of those "simple when you know" things that'll drive you mad if you don't!&lt;/P&gt;&lt;/FONT&gt;&lt;img src ="http://blogs.vbcity.com/xtab/aggbug/8453.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>