Keeping It Simple

Sample code is a great way to learn how to do things in computer programming. The problem for many beginners is that a lot of sample code is written by professionals for professionals. This means several things both good and bad. On the good side students are exposed to good coding practices (usually), technically correct code, and powerful techniques. On the bad side sample code for professionals often is very complicated (perhaps even over engineered) and often presents more new concepts than a beginner can safely handle. The problem than becomes separating what a beginner needs for a specific task and what has been included for more complicated problems. This is why I tend to write a lot of sample code and even better highlight sample code by better programmers/explainers than myself. Matt Gertz of the Visual Basic team is one of those people. If you search my blog for his name you’ll find a number of references to his posts on the Visual Basic Team Blog.

Recently I came across something he posted back in 2007 (Simple Animation in Visual Basic (Matt Gertz)) that explains some simple animation techniques in Visual Basic. He does a great job and I recommend the article. The gerbil code (there is a link to it at the bottom of the article or go here includes two simple projects. The Gerbil game and a very simple project that moves a sheep across the screen.

Along with the animation, Matt also includes a couple of other things that are well worth knowing. One of them is states as in state machines. I think that while he doesn’t spend a lot of time on the concept of a state machine the implementation is one that students can easily understand and work with. Once they know about that in practice I think you will find explaining the theory goes quickly and easily. The other thing that a student can pick up easily is drag and drop programming. Students like to do that and it is surprisingly easy once you know the steps.

For what it is worth I created as basic a sample of drag and drop as I could. This code just moves a picture box around the screen in response to pressing, holding and moving the mouse. When you release the  mouse button the picture box stays were you leave it.

   1: Dim offsetX As Integer
   2: Dim offsetY As Integer
   3: Dim isMoving As Boolean
   5: Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
   6:             Handles PictureBox1.MouseDown
   7:     ' Get some initial offset values for the mouse location and flag that we are moving the box
   8:     offsetX = e.X
   9:     offsetY = e.Y
  10:     isMoving = True
  11: End Sub
  13: Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
  14:             Handles PictureBox1.MouseMove
  15:     ' we will move the box if isMoving is true and we will move it in relation to the mouse movement
  16:     If isMoving Then
  17:         PictureBox1.Left += e.X - offsetX
  18:         PictureBox1.Top += e.Y - offsetY
  19:     End If
  20: End Sub
  22: Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
  23:             Handles PictureBox1.MouseUp
  24:     ' We are done moving the box so change the value of isMoving to false
  25:     isMoving = False
  26: End Sub

Try it. Have some fun with it. This week is all about the fun of programming for me.

Comments (0)

Skip to main content