<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WinSoft.se &#187; Microsoft .NET</title>
	<atom:link href="http://www.winsoft.se/category/dotnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.winsoft.se</link>
	<description>Development with focus on Visual Basic .NET</description>
	<lastBuildDate>Wed, 28 Jul 2010 08:49:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Writing a game using TurboSprite (2/2)</title>
		<link>http://www.winsoft.se/2010/07/writing-a-game-using-turbosprite-22/</link>
		<comments>http://www.winsoft.se/2010/07/writing-a-game-using-turbosprite-22/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 18:48:56 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 9]]></category>
		<category><![CDATA[2D game]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1306</guid>
		<description><![CDATA[Continued from here. Now I want to enable shooting. When the user presses the Control key, I create a bullet, sets its properties and adds it to the engine. This code is added to the KeyDown event. I have to modify the KeyUp event, because in the previous version, it unconditionally made the ship stop. [...]]]></description>
			<content:encoded><![CDATA[<p>Continued from <a href="http://www.winsoft.se/2010/07/writing-a-game-using-turbosprite-12/">here</a>.</p>
<p>Now I want to enable shooting. When the user presses the Control key, I create a bullet, sets its properties and adds it to the engine. This code is added to the <strong>KeyDown</strong> event. I have to modify the <strong>KeyUp</strong> event, because in the previous version, it unconditionally made the ship stop. Now when more keys are involved, I only want the ship to stop if it is the up or down buttons are released.</p>
<p>This is the new version of the <strong>KeyDown</strong> event:</p>
<pre class="csharpcode">
   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_KeyDown(<span class="kwrd">ByVal</span> _
sender <span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.Windows.Forms.KeyEventArgs) <span class="kwrd">Handles</span> <span class="kwrd">Me</span>.KeyDown
      <span class="kwrd">If</span> e.KeyData = Keys.A <span class="kwrd">Then</span>

         <span class="rem">'If user presses "a", move up!</span>
         <span class="kwrd">Me</span>.PlayerMover.DestY = 0
         <span class="kwrd">Me</span>.PlayerMover.SpeedY = -2

      <span class="kwrd">ElseIf</span> e.KeyData = Keys.Z <span class="kwrd">Then</span>

         <span class="rem">'If user presses "z", move down!</span>
         <span class="kwrd">Me</span>.PlayerMover.DestY = SpriteSurface1.Height
         <span class="kwrd">Me</span>.PlayerMover.SpeedY = 2

      <span class="kwrd">End</span> <span class="kwrd">If</span>

      <span class="kwrd">If</span> (e.KeyData <span class="kwrd">And</span> Keys.ControlKey) = Keys.ControlKey <span class="kwrd">Then</span>

         <span class="rem">'If user presses Control, fire!</span>
         <span class="kwrd">Dim</span> Bullet <span class="kwrd">As</span> <span class="kwrd">New</span> SCG.TurboSprite.PolygonSprite(2, 0, -2, 1, -2, -1)
         Bullet.Position = <span class="kwrd">New</span> Point(Player.Position.X + 15, Player.Position.Y + 9)
         Bullet.Color = Color.Cyan
         SpriteEngineDestination1.AddSprite(Bullet)
         <span class="kwrd">Dim</span> BulletMover <span class="kwrd">As</span> SCG.TurboSprite.DestinationMover = _
SpriteEngineDestination1.GetMover(Bullet)
         BulletMover.Destination = <span class="kwrd">New</span> Point(SpriteSurface1.Width + 5, _
Bullet.Height)
         BulletMover.SpeedX = 4

      <span class="kwrd">End</span> <span class="kwrd">If</span>
   <span class="kwrd">End</span> Sub</pre>
<p>This is the new version of the <strong>KeyUp</strong> event:</p>
<pre class="csharpcode">
   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_KeyUp(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> _
<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.Windows.Forms.KeyEventArgs) <span class="kwrd">Handles</span> <span class="kwrd">Me</span>.KeyUp

      <span class="rem">'Stop moving, but only if "a" or "z" is released.</span>
      <span class="kwrd">If</span> e.KeyData = Keys.A <span class="kwrd">Or</span> e.KeyCode = Keys.Z <span class="kwrd">Then</span>
         <span class="kwrd">Me</span>.PlayerMover.SpeedY = 0
      <span class="kwrd">End</span> <span class="kwrd">If</span>

   <span class="kwrd">End</span> Sub</pre>
<p>Finally, when a bullet reaches its destination at the right end of the screen, I want it deleted. Check the <strong>SpriteReachedDestination</strong> event in the final code listing below.</p>
<p>When this is all working, I want to add an enemy and use the collision detection to see if it was killed. I did not subclass the <strong>PolygonSprite</strong>, and this is really something you should do to be able to tag the sprites. The &#8220;ugly hack&#8221; of this post, must be that I use color to determine if the sprite is a bullet, or the enemy. This is the complete code:</p>
<pre class="csharpcode">
<span class="kwrd">Public</span> <span class="kwrd">Class</span> Form1

   <span class="rem">'A reference to the player (created in the Shown event) and the mover.</span>
   <span class="kwrd">Private</span> Player <span class="kwrd">As</span> SCG.TurboSprite.PolygonSprite
   <span class="kwrd">Private</span> PlayerMover <span class="kwrd">As</span> SCG.TurboSprite.DestinationMover

   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_Shown(<span class="kwrd">ByVal</span> sender _
<span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.EventArgs) <span class="kwrd">Handles</span> <span class="kwrd">Me</span>.Shown

      <span class="rem">'Set the desired number of frames per second and activate the surface.</span>
      SpriteSurface1.DesiredFPS = 40
      SpriteSurface1.Active = <span class="kwrd">True</span>

      <span class="rem">'Create the player and save the reference.</span>
      <span class="kwrd">Me</span>.Player = <span class="kwrd">New</span> SCG.TurboSprite.PolygonSprite(-20, -10, 20, 10, -20, 10)

      <span class="rem">'Set player original position.</span>
      <span class="kwrd">Me</span>.Player.Position = <span class="kwrd">New</span> Point(100, 100)

      <span class="rem">'Set player color.</span>
      <span class="kwrd">Me</span>.Player.Color = Color.Yellow

      <span class="rem">'Add the player to the sprite engine.</span>
      SpriteEngineDestination1.AddSprite(<span class="kwrd">Me</span>.Player)

      <span class="rem">'Save a reference to the mover.</span>
      <span class="kwrd">Me</span>.PlayerMover = SpriteEngineDestination1.GetMover(<span class="kwrd">Me</span>.Player)

      <span class="rem">'Now, add an ENEMY!</span>
      <span class="kwrd">Dim</span> Enemy <span class="kwrd">As</span> <span class="kwrd">New</span> SCG.TurboSprite.PolygonSprite(-30, 20, 0, -20, 30, 20)
      Enemy.Position = <span class="kwrd">New</span> Point(SpriteSurface1.Width, SpriteSurface1.Height)
      Enemy.Color = Color.Red
      SpriteEngineDestination1.AddSprite(Enemy)
      <span class="kwrd">Dim</span> EnenyMover <span class="kwrd">As</span> SCG.TurboSprite.DestinationMover = _
SpriteEngineDestination1.GetMover(Enemy)
      EnenyMover.Speed = 1
      EnenyMover.Destination = <span class="kwrd">New</span> Point(-50, 100)

   <span class="kwrd">End</span> <span class="kwrd">Sub</span>

   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_KeyDown(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> _
<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.Windows.Forms.KeyEventArgs) <span class="kwrd">Handles</span> <span class="kwrd">Me</span>.KeyDown
      <span class="kwrd">If</span> e.KeyData = Keys.A <span class="kwrd">Then</span>

         <span class="rem">'If user presses "a", move up!</span>
         <span class="kwrd">Me</span>.PlayerMover.DestY = 0
         <span class="kwrd">Me</span>.PlayerMover.SpeedY = -2

      <span class="kwrd">ElseIf</span> e.KeyData = Keys.Z <span class="kwrd">Then</span>

         <span class="rem">'If user presses "z", move down!</span>
         <span class="kwrd">Me</span>.PlayerMover.DestY = SpriteSurface1.Height
         <span class="kwrd">Me</span>.PlayerMover.SpeedY = 2

      <span class="kwrd">End</span> <span class="kwrd">If</span>

      <span class="kwrd">If</span> (e.KeyData <span class="kwrd">And</span> Keys.ControlKey) = Keys.ControlKey <span class="kwrd">Then</span>

         <span class="rem">'If user presses Control, fire!</span>
         <span class="kwrd">Dim</span> Bullet <span class="kwrd">As</span> <span class="kwrd">New</span> SCG.TurboSprite.PolygonSprite(2, 0, -2, 1, -2, -1)
         Bullet.Position = <span class="kwrd">New</span> Point(Player.Position.X + 15, Player.Position.Y + 9)
         Bullet.Color = Color.Cyan
         SpriteEngineDestination1.AddSprite(Bullet)
         <span class="kwrd">Dim</span> BulletMover <span class="kwrd">As</span> SCG.TurboSprite.DestinationMover = _
SpriteEngineDestination1.GetMover(Bullet)
         BulletMover.Destination = <span class="kwrd">New</span> Point(SpriteSurface1.Width + 5, _
Bullet.Height)
         BulletMover.SpeedX = 4

      <span class="kwrd">End</span> <span class="kwrd">If</span>
   <span class="kwrd">End</span> <span class="kwrd">Sub</span>

   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_KeyUp(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> _
e <span class="kwrd">As</span> System.Windows.Forms.KeyEventArgs) <span class="kwrd">Handles</span> <span class="kwrd">Me</span>.KeyUp

      <span class="rem">'Stop moving, but only if "a" or "z" is released.</span>
      <span class="kwrd">If</span> e.KeyData = Keys.A <span class="kwrd">Or</span> e.KeyCode = Keys.Z <span class="kwrd">Then</span>
         <span class="kwrd">Me</span>.PlayerMover.SpeedY = 0
      <span class="kwrd">End</span> <span class="kwrd">If</span>

   <span class="kwrd">End</span> <span class="kwrd">Sub</span>

   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> SpriteEngineDestination1_SpriteReachedDestination(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> _
<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> SCG.TurboSprite.SpriteEventArgs) <span class="kwrd">Handles</span> SpriteEngineDestination1.SpriteReachedDestination
      <span class="rem">'You really should subclass the bullet to be able to</span>
      <span class="rem">'tag it as a bullet. I use color. Hack!!!</span>
      <span class="kwrd">Dim</span> P <span class="kwrd">As</span> SCG.TurboSprite.PolygonSprite = <span class="kwrd">CType</span>(e.Sprite, SCG.TurboSprite.PolygonSprite)
      <span class="kwrd">If</span> P.Color = Color.Cyan <span class="kwrd">Then</span>
         P.Kill()
         SpriteEngineDestination1.RemoveSprite(P)
      <span class="kwrd">End</span> <span class="kwrd">If</span>
   <span class="kwrd">End</span> <span class="kwrd">Sub</span>

   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> SpriteSurface1_SpriteCollision(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> _
<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> SCG.TurboSprite.SpriteCollisionEventArgs) <span class="kwrd">Handles</span> SpriteSurface1.SpriteCollision
      <span class="rem">'Did the player shoot the enemy? Is one a bullet and one the enemy?</span>
      <span class="rem">'Again, I use color. Hack!!!</span>
      <span class="kwrd">Dim</span> FirstSprite <span class="kwrd">As</span> SCG.TurboSprite.PolygonSprite = <span class="kwrd">CType</span>(e.Sprite1, _
SCG.TurboSprite.PolygonSprite)
      <span class="kwrd">Dim</span> SecondSprite <span class="kwrd">As</span> SCG.TurboSprite.PolygonSprite = <span class="kwrd">CType</span>(e.Sprite2, _
SCG.TurboSprite.PolygonSprite)
      <span class="kwrd">If</span> FirstSprite.Color = Color.Cyan <span class="kwrd">Then</span>
         <span class="rem">'We have a bullet.</span>
         <span class="kwrd">If</span> SecondSprite.Color = Color.Red <span class="kwrd">Then</span>
            <span class="rem">'And the enemy.</span>
            SecondSprite.Kill()
            SpriteEngineDestination1.RemoveSprite(SecondSprite)
         <span class="kwrd">End</span> <span class="kwrd">If</span>
      <span class="kwrd">ElseIf</span> FirstSprite.Color = Color.Red <span class="kwrd">Then</span>
         <span class="rem">'We have the enemy.</span>
         <span class="kwrd">If</span> SecondSprite.Color = Color.Cyan <span class="kwrd">Then</span>
            <span class="rem">'And a bullet.</span>
            FirstSprite.Kill()
            SpriteEngineDestination1.RemoveSprite(FirstSprite)
         <span class="kwrd">End</span> <span class="kwrd">If</span>
      <span class="kwrd">End</span> <span class="kwrd">If</span>
   <span class="kwrd">End</span> <span class="kwrd">Sub</span>

<span class="kwrd">End</span> <span class="kwrd">Class</span>
</pre>
<p>Again, subclass and add your own properties! Do not use color to determine what sprite you are dealing with. This is the result:</p>
<p><img alt="" src="http://imghost.winsoft.se/upload/142911279565002scramble.jpg" class="alignnone" width="307" height="151" /></p>
<p>Thank you, TurboSprite!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/07/writing-a-game-using-turbosprite-22/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing a game using TurboSprite (1/2)</title>
		<link>http://www.winsoft.se/2010/07/writing-a-game-using-turbosprite-12/</link>
		<comments>http://www.winsoft.se/2010/07/writing-a-game-using-turbosprite-12/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 13:13:47 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 9]]></category>
		<category><![CDATA[2D game]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1298</guid>
		<description><![CDATA[There are two things to take away from this post. The ease of TurboSprite and the power and speed of GDI+. Of course, you are still supposed to write games using hardware acceleration, but you can do decent desktop games without DirectX, XNA or OpenGL. In this first part, I will create a player (a [...]]]></description>
			<content:encoded><![CDATA[<p>There are two things to take away from this post. The ease of TurboSprite and the power and speed of GDI+. Of course, you are still supposed to write games using hardware acceleration, but you can do decent desktop games without DirectX, XNA or OpenGL.</p>
<p>In this first part, I will create a player (a spaceship) that can respond to keyboard control. In the second part, the player will be able to shoot and kill enemies.</p>
<p>To start off, I create a project, I add a sprite <strong>surface</strong> and an <strong>sprite engine destination</strong>. I connect them to each other by setting the <strong>Surface</strong> property of the engine. Also, I intend to use collision detection, so I set the <strong>DetectCollisionSelf</strong> property to True.</p>
<p>On the surface, I set the <strong>AutoBlank</strong> property to True and the <strong>AutoBlankColor</strong> property to Black. On the form, I set <strong>KeyPreview</strong> to True.</p>
<p>Note that so far, all the magic is happening in <strong>KeyDown</strong>, <strong>KeyUp</strong> (input feedback) and <strong>Shown</strong> (initialization).</p>
<pre class="csharpcode">
<span class="kwrd">Public</span> <span class="kwrd">Class</span> Form1

   <span class="rem">'A reference to the player (created in the Shown event) and the mover.</span>
   <span class="kwrd">Private</span> Player <span class="kwrd">As</span> SCG.TurboSprite.PolygonSprite
   <span class="kwrd">Private</span> PlayerMover <span class="kwrd">As</span> SCG.TurboSprite.DestinationMover

   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_Shown(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> _
System.EventArgs) <span class="kwrd">Handles</span> <span class="kwrd">Me</span>.Shown

      <span class="rem">'Set the desired number of frames per second and activate the surface.</span>
      SpriteSurface1.DesiredFPS = 40
      SpriteSurface1.Active = <span class="kwrd">True</span>

      <span class="rem">'Create the player and save the reference.</span>
      <span class="kwrd">Me</span>.Player = <span class="kwrd">New</span> SCG.TurboSprite.PolygonSprite(-20, -10, 20, 10, -20, 10)

      <span class="rem">'Set player original position.</span>
      <span class="kwrd">Me</span>.Player.Position = <span class="kwrd">New</span> Point(100, 100)

      <span class="rem">'Set player color.</span>
      <span class="kwrd">Me</span>.Player.Color = Color.Yellow

      <span class="rem">'Add the player to the sprite engine.</span>
      SpriteEngineDestination1.AddSprite(<span class="kwrd">Me</span>.Player)

      <span class="rem">'Save a reference to the mover.</span>
      <span class="kwrd">Me</span>.PlayerMover = SpriteEngineDestination1.GetMover(<span class="kwrd">Me</span>.Player)

   <span class="kwrd">End</span> <span class="kwrd">Sub</span>

   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_KeyDown(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> _
e <span class="kwrd">As</span> System.Windows.Forms.KeyEventArgs) <span class="kwrd">Handles</span> <span class="kwrd">Me</span>.KeyDown
      <span class="kwrd">If</span> e.KeyData = Keys.A <span class="kwrd">Then</span>

         <span class="rem">'If user presses "a", move up!</span>
         <span class="kwrd">Me</span>.PlayerMover.DestY = 0
         <span class="kwrd">Me</span>.PlayerMover.SpeedY = -2

      <span class="kwrd">ElseIf</span> e.KeyData = Keys.Z <span class="kwrd">Then</span>

         <span class="rem">'If user presses "z", move down!</span>
         <span class="kwrd">Me</span>.PlayerMover.DestY = SpriteSurface1.Height
         <span class="kwrd">Me</span>.PlayerMover.SpeedY = 2

      <span class="kwrd">End</span> <span class="kwrd">If</span>
   <span class="kwrd">End</span> <span class="kwrd">Sub</span>

   <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_KeyUp(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> _
e <span class="kwrd">As</span> System.Windows.Forms.KeyEventArgs) <span class="kwrd">Handles</span> <span class="kwrd">Me</span>.KeyUp

      <span class="rem">'Stop moving!</span>
      <span class="kwrd">Me</span>.PlayerMover.SpeedY = 0

   <span class="kwrd">End</span> <span class="kwrd">Sub</span>

<span class="kwrd">End</span> <span class="kwrd">Class</span>
</pre>
<p>Continued <a href="http://www.winsoft.se/2010/07/writing-a-game-using-turbosprite-22/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/07/writing-a-game-using-turbosprite-12/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>TurboSprite</title>
		<link>http://www.winsoft.se/2010/07/turbosprite/</link>
		<comments>http://www.winsoft.se/2010/07/turbosprite/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 13:23:01 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 9]]></category>
		<category><![CDATA[2D game]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1293</guid>
		<description><![CDATA[TurboSprite is a software driven (GDI+) sprite engine written by Dion Kurczek in C#. The reason that I&#8217;m picking this up, is that it shows (like I have mentioned before) the amazing capabilities and performance of the GDI+ library in .NET. TurboSprite provides a sprite container, bitmap sprites, vector sprites and collision detection. To be [...]]]></description>
			<content:encoded><![CDATA[<p>TurboSprite is a software driven (GDI+) sprite engine written by Dion Kurczek in C#. The reason that I&#8217;m picking this up, is that it shows (like I have mentioned before) the amazing capabilities and performance of the GDI+ library in .NET. TurboSprite provides a sprite container, bitmap sprites, vector sprites and collision detection.</p>
<p>To be able to use TurboSprite, download and compile it, add the components to your toolbox. Then, add a surface and an engine to a form. Note that you have two engines. One is called <strong>SpriteEngine</strong> and another is called SpriteEngineDestination. Use the <strong>SpriteEngineDestination</strong> object, because it extends the <strong>SpriteEngine</strong> class with the ability to move sprites. You might want to set a few properties to connect the engine to the surface, to tell the engine if you want collision detection and so on. Collision detection is a reasonable cause for using a sprite engine. I use the <strong>AutoBlank</strong> feature of the surface.</p>
<p>About the moving of the sprites, you can acquire the sprite mover using the <strong>GetMover</strong> function for the engine (if you use the <strong>SpriteEngineDestination</strong>), and set the X and Y speed of a sprite. Or you can do as I do in this example below. That is, just set a speed and a destination position. The destination position determines the sprite movement direction.</p>
<p>This code does two thinks (written in the <strong>Show</strong> event of a regular form). The first line activates the sprite engine so that sprites will move. The rest of the code initializes one vector sprite.</p>
<pre class="csharpcode">
<span class="rem">'When the form is loaded, activate the surface</span>
SpriteSurface1.Active = <span class="kwrd">True</span>

<span class="rem">'Create a space ship. These points makes a ship.</span>
<span class="kwrd">Dim</span> S <span class="kwrd">As</span> <span class="kwrd">New</span> SCG.TurboSprite.PolygonSprite(0, -10, 5, 10, 0, 5, -5, 10)

<span class="rem">'Tell the sprite to rotate.</span>
S.Spin = SCG.TurboSprite.SpinType.Clockwise
S.SpinSpeed = 3

<span class="rem">'Start position.</span>
S.Position = <span class="kwrd">New</span> Point(30, 30)

<span class="rem">'Add the sprite to the engine, and acquire the sprite mover from the engine.</span>
SpriteEngineDestination1.AddSprite(S)
<span class="kwrd">Dim</span> Mover <span class="kwrd">As</span> SCG.TurboSprite.DestinationMover = SpriteEngineDestination1.GetMover(S)

<span class="rem">'Tell the mover about speed and direction.</span>
Mover.Speed = 5
Mover.Destination = <span class="kwrd">New</span> Point(50, 40)

<span class="rem">'Tell the mover that the destination really isn't a destination,</span>
<span class="rem">'it's just used as a direction.</span>
Mover.StopAtDestination = <span class="kwrd">False</span>
</pre>
<p>This is the sprite:</p>
<p><img alt="" src="http://imghost.winsoft.se/upload/594291279199894sprite.jpg" class="alignnone" width="307" height="202" /></p>
<p>Download the engine <a href="http://www.codeproject.com/KB/game/TurboSprite.aspx" target="_blank">from the Code Project web site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/07/turbosprite/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tuples</title>
		<link>http://www.winsoft.se/2010/06/tuples/</link>
		<comments>http://www.winsoft.se/2010/06/tuples/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 18:17:17 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Microsoft .NET]]></category>
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1231</guid>
		<description><![CDATA[Tuples in F# is a list of objects with different types, and the tuple itself is strongly typed. You can imagine a simple class or a structure without having to declare the type. This code creates a tuple called myTuple that contains a string, and integer, another string and finally a boolean. let myTuple=("A", 10, [...]]]></description>
			<content:encoded><![CDATA[<p>Tuples in F# is a list of objects with different types, and the tuple itself is strongly typed. You can imagine a simple class or a structure without having to declare the type. This code creates a tuple called myTuple that contains a string, and integer, another string and finally a boolean.</p>
<p><code>let myTuple=("A", 10, "B", true)</code></p>
<p>The string representation of a tuple is a string representation of the individual values within parenthesis. This code&#8230;</p>
<p><code>let myTuple=("A", 10, "B", true)<br />
System.Console.WriteLine(myTuple)</code></p>
<p>&#8230;gives you&#8230;</p>
<p><code>(A, 10, B, True)</code></p>
<p>This code will extract the individual values from the tuple. The output from this code will be <strong>10</strong>.</p>
<p><code>let myTuple=("A", 10, "B", true)<br />
let v1, v2, v3, v4 = myTuple<br />
System.Console.WriteLine(v2)</code></p>
<p>The types of the values will be the .NET types from the original values in the tuple, v1 is a string, v2 is a 32 bit integer, v3 is also a string and v4 is a boolean. Tuples can be used in many ways, as allowing a single function to return multiple values.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/06/tuples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Decompressing text</title>
		<link>http://www.winsoft.se/2010/06/decompressing-text/</link>
		<comments>http://www.winsoft.se/2010/06/decompressing-text/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 12:50:44 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 9]]></category>
		<category><![CDATA[Streams]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1211</guid>
		<description><![CDATA[This post shows how to compress a String to reduce the amount memory it consumes, and this post shows how to use the CompressText function. To be able to read the content of the string, it must be decompressed (or inflated) again. The DecompressText function is one way to do this. Private Function DecompressText(ByVal B() [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.winsoft.se/2009/11/compressing-text/">This post</a> shows how to compress a String to reduce the amount memory it consumes, and <a href="http://www.winsoft.se/2009/11/compressing-genesis/">this post</a> shows how to use the <strong>CompressText</strong> function. To be able to read the content of the string, it must be decompressed (or inflated) again. The <strong>DecompressText</strong> function is one way to do this.</p>
<pre>Private Function DecompressText(ByVal B() As Byte) As String
   Dim Result As New System.Text.StringBuilder()
   Using MemStream As New System.IO.MemoryStream(B)
      Using GZStream As New System.IO.Compression.GZipStream(MemStream, _
         IO.Compression.CompressionMode.Decompress)
      Do
         'Note that this makes 1024 bytes in VB.
         Dim Buffer(1023) As Byte
         Dim BytesRead As Integer = GZStream.Read(Buffer, 0, 1024)
         If BytesRead > 0 Then
            Result.Append( _
               System.Text.Encoding.UTF8.GetString(Buffer, 0, BytesRead))
         End If
         If BytesRead < 1024 Then
            Exit Do
         End If
      Loop
      GZStream.Close()
      Return Result.ToString()
      End Using
   End Using
End Function</pre>
<p>Now, imagine that <strong>B</strong> is a byte array returned from the <strong>CompressText</strong> function. <strong>B</strong> holds the bytes of a compressed text string. <strong>B</strong> is passed to the <strong>DecompressText</strong> function and the function returns the inflated string again. Example:</p>
<pre>'Create some text.
Dim S As String = "This is some text that I want to compress. Preferably it's " &#038; _
"a long string loaded from a text file or some XML document."

'Assign the compressed version to the variable B.
Dim B() As Byte = CompressText(S.ToString())

'Decompress it, and display the result.
Dim Decompressed As String = DecompressText(B)
Console.WriteLine(Decompressed)</pre>
<p>Have you seen a more elegant way to handle strings in memory than what the .NET Framework offers?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/06/decompressing-text/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Inserting a value in an identity column</title>
		<link>http://www.winsoft.se/2010/05/inserting-a-value-in-an-identity-column/</link>
		<comments>http://www.winsoft.se/2010/05/inserting-a-value-in-an-identity-column/#comments</comments>
		<pubDate>Tue, 25 May 2010 15:12:11 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 9]]></category>
		<category><![CDATA[Data access]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1148</guid>
		<description><![CDATA[In SQL Server 2008, when a column is set to auto increment (Identity Specification), you are normally not allowed to give that column a value. If you are copying data from one database to another in Visual Basic, perhaps by loading a DataSet object using one connection, and inserting it&#8217;s data using another, this can [...]]]></description>
			<content:encoded><![CDATA[<p>In SQL Server 2008, when a column is set to auto increment (Identity Specification), you are normally not allowed to give that column a value. If you are copying data from one database to another in Visual Basic, perhaps by loading a DataSet object using one connection, and inserting it&#8217;s data using another, this can be a problem.</p>
<p>The table option you must set, to be allowed to insert a value in an identity column is called <strong>IDENTITY_INSERT</strong>. So add this line to your command object to make it work:</p>
<pre>SET IDENTITY_INSERT dbo.Products ON;</pre>
<p>The following <strong>INSERT</strong> statement can give a value to the identity column, if the value complies with the rules for that column. The line of code could look something like this:</p>
<pre>Using X As New SqlClient.SqlCommand( _
    "SET IDENTITY_INSERT dbo.Products ON; INSERT...</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/05/inserting-a-value-in-an-identity-column/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET controls in Microsoft Access</title>
		<link>http://www.winsoft.se/2010/05/net-controls-in-microsoft-access/</link>
		<comments>http://www.winsoft.se/2010/05/net-controls-in-microsoft-access/#comments</comments>
		<pubDate>Wed, 05 May 2010 19:37:37 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 8]]></category>
		<category><![CDATA[ActiveX]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1136</guid>
		<description><![CDATA[Creating ActiveX objects in Visual Basic .NET is easy from Visual Studio 2008. There are a few checkboxes to tick, and a class attribute to add. But if you are doing an ActiveX control for use in let&#8217;s say an Microsoft Access application, there are some requirements that must be met, otherwise Access will not [...]]]></description>
			<content:encoded><![CDATA[<p>Creating ActiveX objects in Visual Basic .NET is easy from Visual Studio 2008. There are a few checkboxes to tick, and a class attribute to add. But if you are doing an ActiveX control for use in let&#8217;s say an Microsoft Access application, there are some requirements that must be met, otherwise Access will not list your control. Of course, Microsoft gives you a <a href="http://msdn.microsoft.com/en-us/vbasic/bb419144.aspx" target="_blank">template</a> for this, and then you´re off. Thanks to Håkan Ståhl for pointing out this template to me.</p>
<p>Create a VB6 Interop UserControl project. The project that is shown on the screenshot below uses the standard name InteropUserControlLibrary1. I already hold a control. To rename the control, rename all three files (the vb file, the manifest and the bitmap). When the vb file is renamed, you will be asked if you want to rename the references too. Do that. Finally, fix the broken references in the manifest file (enter the new name in the <strong>progid</strong> attribute and the <strong>name</strong> attribute). Now you´re on your way. You can add a test project to the solution and start coding. The control is registered when you compile the project.</p>
<p><img alt="" src="http://imghost.winsoft.se/upload/820421273088160access.jpg" class="alignnone" width="401" height="321" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/05/net-controls-in-microsoft-access/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easy handeling of command line arguments</title>
		<link>http://www.winsoft.se/2010/04/easy-handeling-of-command-line-arguments/</link>
		<comments>http://www.winsoft.se/2010/04/easy-handeling-of-command-line-arguments/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 19:34:44 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 9]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1057</guid>
		<description><![CDATA[The CommandLineArguments class is a useful tool when you are building an application that accepts different arguments from the command line, and argument with parameters. The class lets you check if the program was started using a specific argument, and also read the argument to the right, as if it was a parameter. The ArgumentExists [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>CommandLineArguments</strong> class is a useful tool when you are building an application that accepts different arguments from the command line, and argument with parameters. The class lets you check if the program was started using a specific argument, and also read the argument to the right, as if it was a parameter.</p>
<p>The <strong>ArgumentExists</strong> function checks for the presence of an argument and the <strong>GetIndexOfArgument</strong> function gives you the index of an argument, or negative one if not present. The <strong>GetArgumentParameter</strong> returns the argument next to a given argument.</p>
<p>Let&#8217;s say that you are writing an application called <strong>test.exe</strong>, and it is started like this:</p>
<pre>test.exe -f hello -qq "hello again"</pre>
<p>If you pass &#8220;<strong>-qq</strong>&#8221; to the <strong>GetArgumentParameter</strong> function, it will return &#8220;<strong>hello again</strong>&#8220;. If you pass &#8220;<strong>-f</strong>&#8221; to the <strong>ArgumentExists</strong> function, it returns <strong>true</strong>. And if you pass &#8220;<strong>yeah</strong>&#8221; to the <strong>GetIndexOfArgument</strong> function, it returns <strong>-1</strong>.</p>
<p>Example:</p>
<pre class="csharpcode"><span class="kwrd">Dim</span> Args <span class="kwrd">As</span> <span class="kwrd">New</span> CommandLineArguments()
Console.WriteLine(<span class="str">"Param: "</span> &amp; Args.ArgumentExists(<span class="str">"-f"</span>).ToString())
Console.ReadLine()</pre>
<p>This is the source code for the class:</p>
<pre class="csharpcode"><span class="kwrd">Public</span> <span class="kwrd">Class</span> CommandLineArguments
    <span class="kwrd">Inherits</span> CollectionBase

    <span class="kwrd">Private</span> mIgnoreCase <span class="kwrd">As</span> <span class="kwrd">Boolean</span>

    <span class="kwrd">Public</span> <span class="kwrd">Sub</span> <span class="kwrd">New</span>()
        <span class="kwrd">Me</span>.<span class="kwrd">New</span>(<span class="kwrd">True</span>)
    <span class="kwrd">End</span> <span class="kwrd">Sub</span>

    <span class="kwrd">Public</span> <span class="kwrd">Sub</span> <span class="kwrd">New</span>(<span class="kwrd">ByVal</span> IgnoreCase <span class="kwrd">As</span> <span class="kwrd">Boolean</span>)
        <span class="kwrd">Dim</span> S() <span class="kwrd">As</span> <span class="kwrd">String</span> = System.Environment.GetCommandLineArgs()
        <span class="kwrd">If</span> S.Length &gt; 1 <span class="kwrd">Then</span>
            <span class="kwrd">For</span> I <span class="kwrd">As</span> <span class="kwrd">Integer</span> = 1 <span class="kwrd">To</span> S.Length - 1
                List.Add(S(I))
            <span class="kwrd">Next</span>
        <span class="kwrd">End</span> <span class="kwrd">If</span>
        <span class="kwrd">Me</span>.mIgnoreCase = IgnoreCase
    <span class="kwrd">End</span> <span class="kwrd">Sub</span>

    <span class="kwrd">Default</span> <span class="kwrd">Public</span> <span class="kwrd">ReadOnly</span> <span class="kwrd">Property</span> Item(<span class="kwrd">ByVal</span> Index <span class="kwrd">As</span> <span class="kwrd">Integer</span>) <span class="kwrd">As</span> <span class="kwrd">String</span>
        <span class="kwrd">Get</span>
            <span class="kwrd">Return</span> <span class="kwrd">CType</span>(List(Index), <span class="kwrd">String</span>)
        <span class="kwrd">End</span> <span class="kwrd">Get</span>
    <span class="kwrd">End</span> <span class="kwrd">Property</span>

    <span class="kwrd">Public</span> <span class="kwrd">Function</span> ArgumentExists(<span class="kwrd">ByVal</span> Arg <span class="kwrd">As</span> <span class="kwrd">String</span>) <span class="kwrd">As</span> <span class="kwrd">Boolean</span>
        <span class="kwrd">For</span> <span class="kwrd">Each</span> S <span class="kwrd">As</span> <span class="kwrd">String</span> <span class="kwrd">In</span> <span class="kwrd">Me</span>
            <span class="kwrd">If</span> <span class="kwrd">String</span>.Compare(S, Arg, <span class="kwrd">Me</span>.mIgnoreCase) = 0 <span class="kwrd">Then</span>
                <span class="kwrd">Return</span> <span class="kwrd">True</span>
            <span class="kwrd">End</span> <span class="kwrd">If</span>
        <span class="kwrd">Next</span>
        <span class="kwrd">Return</span> <span class="kwrd">False</span>
    <span class="kwrd">End</span> <span class="kwrd">Function</span>

    <span class="kwrd">Public</span> <span class="kwrd">Function</span> GetIndexOfArgument(<span class="kwrd">ByVal</span> Arg <span class="kwrd">As</span> <span class="kwrd">String</span>) <span class="kwrd">As</span> <span class="kwrd">Integer</span>
        <span class="kwrd">If</span> <span class="kwrd">Me</span>.Count &gt; 0 <span class="kwrd">Then</span>
            <span class="kwrd">For</span> I <span class="kwrd">As</span> <span class="kwrd">Integer</span> = 0 <span class="kwrd">To</span> <span class="kwrd">Me</span>.Count - 1
                <span class="kwrd">If</span> <span class="kwrd">String</span>.Compare(<span class="kwrd">Me</span>(I), Arg, <span class="kwrd">Me</span>.mIgnoreCase) = 0 <span class="kwrd">Then</span>
                    <span class="kwrd">Return</span> I
                <span class="kwrd">End</span> <span class="kwrd">If</span>
            <span class="kwrd">Next</span>
            <span class="kwrd">Return</span> -1
        <span class="kwrd">Else</span>
            <span class="kwrd">Return</span> -1
        <span class="kwrd">End</span> <span class="kwrd">If</span>
    <span class="kwrd">End</span> <span class="kwrd">Function</span>

    <span class="kwrd">Public</span> <span class="kwrd">Function</span> GetArgumentParameter(<span class="kwrd">ByVal</span> Arg <span class="kwrd">As</span> <span class="kwrd">String</span>) <span class="kwrd">As</span> <span class="kwrd">String</span>
        <span class="kwrd">Dim</span> Index <span class="kwrd">As</span> <span class="kwrd">Integer</span> = <span class="kwrd">Me</span>.GetIndexOfArgument(Arg)
        <span class="kwrd">If</span> Index &gt; -1 <span class="kwrd">Then</span>
            <span class="kwrd">If</span> Index &lt; (<span class="kwrd">Me</span>.Count - 1) <span class="kwrd">Then</span>
                <span class="kwrd">Return</span> <span class="kwrd">Me</span>(Index + 1)
            <span class="kwrd">Else</span>
                <span class="kwrd">Return</span> <span class="str">""</span>
            <span class="kwrd">End</span> <span class="kwrd">If</span>
        <span class="kwrd">Else</span>
            <span class="kwrd">Return</span> <span class="str">""</span>
        <span class="kwrd">End</span> <span class="kwrd">If</span>
    <span class="kwrd">End</span> <span class="kwrd">Function</span>

<span class="kwrd">End</span> <span class="kwrd">Class</span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/04/easy-handeling-of-command-line-arguments/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Monkeybone: The Bar instruction</title>
		<link>http://www.winsoft.se/2010/03/monkeybone-the-bar-instruction/</link>
		<comments>http://www.winsoft.se/2010/03/monkeybone-the-bar-instruction/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 22:26:12 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Microsoft .NET]]></category>
		<category><![CDATA[Monkeybone]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1032</guid>
		<description><![CDATA[The Bar instruction, as it appears in a Monkeybone Image (a text file with .mob ending) is a line with at least five parameters. The color of the bar, the X and Y position of the bar, the width and height of the bar. The following lines in a .mob file creates a green rectangular [...]]]></description>
			<content:encoded><![CDATA[<p>The Bar instruction, as it appears in a Monkeybone Image (a text file with .mob ending) is a line with at least five parameters. The color of the bar, the X and Y position of the bar, the width and height of the bar. The following lines in a .mob file creates a green rectangular image with a centered yellow box in it:</p>
<pre>Clear 200x200 #337733
Bar #ffff00 X:50 Y:50 W:100 H:100</pre>
<p>Optionally, you can set the percentage of the fill in either horizontal or vertical direction using HFill or VFill, followed by a percent value. The following code will give a 50 pixel high box. The specified height is 100 (pixels) but VFill:50% tells Monkeybone Viewer to only display half height of the box:</p>
<pre>Clear 200x200 #337733
Bar #ffff00 X:50 Y:50 W:100 H:100 VFill:50%</pre>
<p>One way of producing an image like this, is to use Notepad to create a text file that ends with .mob instead of .txt. Using Notepad is not the easiest way, and rather impossible if you want to generate dynamic images. For dynamically generate Monkeybone Images, the Monkeybone .NET library or the Monkeybone .NET control can be used.</p>
<p>The Monkeybone .NET library is a class library that contains a writer with the capability of streaming Monkeybone instructions to a file. The following Visual Basic code requires a reference to Monkeybone.dll, and produces a file called &#8220;Bars.mob&#8221; that can be opened using the Monkeybone Viewer.</p>
<pre>Using Sw As New Monkeybone.MonkeyboneWriter("C:\Bars.mob", 200, 200, _
      System.Drawing.Color.FromArgb(51, 119, 51))
   Dim Bar As New Monkeybone.Instructions.Bar(50, 50, 100, 100)
   Bar.BarFillPercent = 50
   Bar.Color = System.Drawing.Color.FromArgb(255, 255, 0)
   Bar.Orientation = Monkeybone.Instructions.Bar.BarOrientation.Vertical
   Sw.WriteLine(Bar)
End Using</pre>
<p>If you want your image to display on a form, you can use the Monkeybox control. The Monkeybox control displays your image directly on screen, with an option to save it as a .mob file that can be opened in Monkeybone Viewer. The following code is written in the Shown event of a form that has a Monkeybox control on it, called MBox1. Here, I do not actually save the image, just displays the source in a messagebox. The messagebox will contain exactly the same two instructions as the last example produces. Also, on the screen, you will see a green Monkeybone image with a yellow box in it.</p>
<pre class="csharpcode">
<span class="kwrd">Public</span> <span class="kwrd">Class</span> Form1

    <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_Shown(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.EventArgs) <span class="kwrd">Handles</span> <span class="kwrd">Me</span>.Shown
        <span class="rem">'Size is normaly set in the designer, but here I set it manually.</span>
        MBox1.Width = 200
        MBox1.Height = 200

        <span class="rem">'Set the background color.</span>
        MBox1.BackColor = System.Drawing.Color.FromArgb(51, 119, 51)

        <span class="rem">'Add a bar.</span>
        MBox1.AddBar(Color.FromArgb(255, 255, 0), 50, 50, 100, 100, 50, _
           Monkeybox.Bar.BarOrientation.Vertical)

        <span class="rem">'Redraw is not trigged by adding and instruction.</span>
        MBox1.Invalidate()

        MessageBox.Show(MBox1.GetSource())
    <span class="kwrd">End</span> <span class="kwrd">Sub</span>

<span class="kwrd">End</span> Class</pre>
<p><img alt="" src="http://imghost.winsoft.se/upload/340711272206662box.jpg" class="alignnone" width="384" height="336" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/03/monkeybone-the-bar-instruction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Filtersearching a Data Grid View</title>
		<link>http://www.winsoft.se/2010/03/filtersearching-a-data-grid-view/</link>
		<comments>http://www.winsoft.se/2010/03/filtersearching-a-data-grid-view/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 17:25:32 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 9]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=1025</guid>
		<description><![CDATA[Some applications have that cool search feature that filters on what you search for, and displays the result in real time. The data that you are searching in is loaded, and then the record(s) that matches your search from that set of data is display. Usually, you would do this if you are handling fairly [...]]]></description>
			<content:encoded><![CDATA[<p>Some applications have that cool search feature that filters on what you search for, and displays the result in real time. The data that you are searching in is loaded, and then the record(s) that matches your search from that set of data is display. Usually, you would do this if you are handling fairly little data, and you would do this on a separate thread from the GUI thread, to prevent that your application get poor responsiveness. This whole process works incredible well with the Data Grid View of the .NET Framework.</p>
<p>The following example requires a form with a textbox (TextBox1) and a data grid view (DataGridView1). To use the application, just type something in the textbox, and watch the grid filtering its records. The Load event of the form populates the grid. The TextChanged of the textbox filters the grid.</p>
<pre class="csharpcode">
<span class="kwrd">Private</span> <span class="kwrd">Sub</span> Form1_Load(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> System.<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.EventArgs) <span class="kwrd">Handles</span> <span class="kwrd">MyBase</span>.Load

    <span class="rem">'Configure the data grid view.</span>
    DataGridView1.<span class="kwrd">ReadOnly</span> = <span class="kwrd">True</span>
    DataGridView1.Columns.Add(<span class="str">"ColA"</span>, <span class="str">"Some column"</span>)
    DataGridView1.Columns.Add(<span class="str">"ColB"</span>, <span class="str">"Some other column"</span>)
    DataGridView1.AllowUserToAddRows = <span class="kwrd">False</span>
    DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    DataGridView1.Rows.Clear()

    <span class="rem">'Add lots of rows.</span>
    <span class="kwrd">For</span> A <span class="kwrd">As</span> <span class="kwrd">Integer</span> = 500 <span class="kwrd">To</span> 999
        <span class="kwrd">Dim</span> Index <span class="kwrd">As</span> <span class="kwrd">Integer</span> = DataGridView1.Rows.Add()
        DataGridView1.Rows(Index).Cells(0).Value = A.ToString(<span class="str">"000"</span>)
        DataGridView1.Rows(Index).Cells(1).Value = <span class="str">"Hello"</span>
    <span class="kwrd">Next</span>

    <span class="rem">'Add two rows with some names in them.</span>
    <span class="kwrd">Dim</span> Temp <span class="kwrd">As</span> <span class="kwrd">Integer</span> = DataGridView1.Rows.Add()
    DataGridView1.Rows(Temp).Cells(0).Value = <span class="str">"Anders"</span>
    DataGridView1.Rows(Temp).Cells(1).Value = <span class="str">"Bengt"</span>

    Temp = DataGridView1.Rows.Add()
    DataGridView1.Rows(Temp).Cells(0).Value = <span class="str">"Calle"</span>
    DataGridView1.Rows(Temp).Cells(1).Value = <span class="str">"David"</span>

    <span class="rem">'Again, add lots of rows.</span>
    <span class="kwrd">For</span> A <span class="kwrd">As</span> <span class="kwrd">Integer</span> = 300 <span class="kwrd">To</span> 999
        <span class="kwrd">Dim</span> Index <span class="kwrd">As</span> <span class="kwrd">Integer</span> = DataGridView1.Rows.Add()
        DataGridView1.Rows(Index).Cells(0).Value = A.ToString(<span class="str">"000"</span>)
        DataGridView1.Rows(Index).Cells(1).Value = <span class="str">"Good bye"</span>
    <span class="kwrd">Next</span>

    <span class="rem">'Now we have 1.202 rows in the grid. Do some resizing of the columns.</span>
    DataGridView1.AutoResizeColumns()

<span class="kwrd">End</span> <span class="kwrd">Sub</span>

<span class="kwrd">Private</span> <span class="kwrd">Sub</span> TextBox1_TextChanged(<span class="kwrd">ByVal</span> _
     sender <span class="kwrd">As</span> System.<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.EventArgs) <span class="kwrd">Handles</span> TextBox1.TextChanged
    <span class="kwrd">If</span> DataGridView1.RowCount &gt; 0 <span class="kwrd">Then</span>

        <span class="rem">'What is the user searching for? Remove case information.</span>
        <span class="kwrd">Dim</span> SearchFor <span class="kwrd">As</span> <span class="kwrd">String</span> = TextBox1.Text.ToLower().Trim()

        <span class="rem">'If the user has cleared the search box, show all rows.</span>
        <span class="kwrd">If</span> SearchFor = <span class="str">""</span> <span class="kwrd">Then</span>
            <span class="kwrd">For</span> I <span class="kwrd">As</span> <span class="kwrd">Integer</span> = 0 <span class="kwrd">To</span> DataGridView1.Rows.Count - 1
                DataGridView1.Rows(I).Visible = <span class="kwrd">True</span>
            <span class="kwrd">Next</span>
        <span class="kwrd">Else</span>

            <span class="rem">'When the user types something in the textbox, search for</span>
            <span class="rem">'rows with cells that holds that value.</span>
            <span class="kwrd">For</span> I <span class="kwrd">As</span> <span class="kwrd">Integer</span> = 0 <span class="kwrd">To</span> DataGridView1.Rows.Count - 1

                <span class="rem">'Extract the values from the grid. Remove case information.</span>
                <span class="kwrd">Dim</span> ColA <span class="kwrd">As</span> <span class="kwrd">String</span> = <span class="kwrd">CType</span>(DataGridView1.Rows(I).Cells(0).Value, _
<span class="kwrd">String</span>).ToLower()
                <span class="kwrd">Dim</span> ColB <span class="kwrd">As</span> <span class="kwrd">String</span> = <span class="kwrd">CType</span>(DataGridView1.Rows(I).Cells(1).Value, _
<span class="kwrd">String</span>).ToLower()

                <span class="rem">'Show only matching rows.</span>
                <span class="kwrd">If</span> ColA.IndexOf(SearchFor) &gt; -1 <span class="kwrd">OrElse</span> ColB.IndexOf(SearchFor) &gt; -1 <span class="kwrd">Then</span>
                    DataGridView1.Rows(I).Visible = <span class="kwrd">True</span>
                <span class="kwrd">Else</span>
                    DataGridView1.Rows(I).Visible = <span class="kwrd">False</span>
                <span class="kwrd">End</span> <span class="kwrd">If</span>

            <span class="kwrd">Next</span>
        <span class="kwrd">End</span> <span class="kwrd">If</span>

        <span class="rem">'Select the first visible row.</span>
        <span class="kwrd">For</span> I <span class="kwrd">As</span> <span class="kwrd">Integer</span> = 0 <span class="kwrd">To</span> DataGridView1.Rows.Count - 1
            <span class="kwrd">If</span> DataGridView1.Rows(I).Visible <span class="kwrd">Then</span>
                DataGridView1.CurrentCell = DataGridView1.Rows(I).Cells(0)
                <span class="kwrd">Exit</span> <span class="kwrd">For</span>
            <span class="kwrd">End</span> <span class="kwrd">If</span>
        <span class="kwrd">Next</span>

    <span class="kwrd">End</span> <span class="kwrd">If</span>
<span class="kwrd">End</span> <span class="kwrd">Sub</span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/03/filtersearching-a-data-grid-view/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
