Additional profile information on Alfred Thompson at Google+
The registraion for this year's Computer Science & Information Technology Symposium is now open. This symposium is one of the best learning oppertunities for high school computer science teachers And I'm not saying that just because I will be presenting. There are lots of even more impressive people presenting about topics of interest.
This year's symposium is July 8th, 2006 in San Diego in conjunction with NECC. So if you are planning on going to NECC (or even if you are not) think about an extra day in San Diego for this event.
Oh and in the interests of full disclosure, Microsoft is a proud sponsor of this event. It's part of our way of supporting computer science eductation and computer science teachers.
One of the things students seem to want to do rather often to write some code to respond to the user typing keys on the keyboard. This is not hard to do but the way to do it never quite seems obvious. Part of the problem is that different keys cause slightly different events to fire.
Your ordinary letter keys generate KeyPress events and are easily handled in the KeyPress event handler. The special keys, such as the arrow keys that you expect to use to move objects around the screen or form create KeyDown events and are easily handled in a KeyDown event handler. I am oversimplifying here by the way. I hope to write something a little more technical later. For right now I just want to show you a quick way to do some simple things.
The code below lets the user handle a picture box, cleverly called "Picture1" around a form and increase or decrease its size by responding to activity on the keyboard.
The KeyPress event handler looks for the letters "b" and "s" and changes the height or width of the picture box if one of them is pressed. The KeyDown event handler looks for one of the four arrow keys and changes the location of the picture box in response. The code should be fairly self explanatory. Leave a comment if you have a question, or better yet, a better way to do this.
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As _ System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Select Case e.KeyValue.ToStringCase Windows.Forms.Keys.Left
Me.PictureBox1.Left -= 5Case Windows.Forms.Keys.Right
Me.PictureBox1.Left += 5Case Windows.Forms.Keys.Up
Me.PictureBox1.Top -= 5Case Windows.Forms.Keys.Down
Me.PictureBox1.Top += 5End Select
Me.PictureBox1.Left -= 5
Me.PictureBox1.Left += 5
Me.PictureBox1.Top -= 5
Me.PictureBox1.Top += 5
One of the best ways to learn how to do something in a computer program is to look at sample code written by people who know what they are doing. The Microsoft Visual Studio team has published code samples for Visual Basic 2005 here. If you want C# examples go here.
The samples are grouped by catagory:
A bit of something for everyone.
There is also a group of samples for Visual Studo 2003 .NET here. The 2003 set includes samples in both Visual Basic and C#.