1. Home
  2. Computing & Technology
  3. Visual Basic

Focus and Select
Microsoft Lies Again!

By , About.com Guide

Sep 25 2007

Experiment 3 was an application that executed the Select and Focus method on a PictureBox control since that was specifically listed by Microsoft's documentation as not being selectable. The form is shown below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The complete code for the program (event parameters are omitted to save space) is shown below:

Public Class Form1
   Private Sub SelectPicBox_Click(ByVal ...
      PictureBox1.Select()
   End Sub
   Private Sub FocusPicBox_Click(ByVal ...
      PictureBox1.Focus()
   End Sub
   Private Sub PictureBox1_Click(ByVal ...
      Debug.Print("PictureBox1 Click Event")
      PictureBox1.BackColor = Color.Blue
   End Sub
   Private Sub PictureBox1_GotFocus(ByVal ...
      Debug.Print("PictureBox1 GotFocus Event")
      Debug.Print("Focused property: " & PictureBox1.Focused.ToString)
      Debug.Print("ContainsFocus property: " & PictureBox1.ContainsFocus.ToString)
      Debug.Print("CanFocus property: " & PictureBox1.CanFocus.ToString)
      Debug.Print("CanSelect property: " & PictureBox1.CanSelect.ToString)
      PictureBox1.BackColor = Color.Red
   End Sub
End Class

This code illustrates one of the two differences I could find between Focus and Select that I could find. Focus has events and properties that are not available for Select such as:

  • GotFocus Event
  • Focused property
  • ContainsFocus property

The goal of the program was the same as before. To see if there was any difference between what Select and Focus did in code. Since Microsoft clearly states that a PictureBox isn't "selectable" I confidently expected to see one. But again ... no difference.

Determined to get to the bottom of this, I created two final experimental programs. One did nothing but Select a PictureBox and the other did nothing but Focus a PictureBox. Then I used the ILDASM utility to examine the actual generated Intermediate Language code generated. The IL code is shown below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The IL code only had two differences:

  1. the actual method called in the System.Windows.Forms.Control object
  2. the method call returned a void for Select and a bool for Focus

Using Visual Studio's Object Browser confirms this. Select and Focus are shown this way:

System.Windows.Forms.Control.Select()
System.Windows.Forms.Control.Focus() As Boolean

In other words, you can code ...

Dim x As Boolean = PictureBox1.Focus()

... but you can't code ...

Dim x As Boolean = PictureBox1.Select() ' invalid

That's the second difference.

After all this, I have concluded that the moral of this story is, "You can't trust Microsoft to tell you everything. Some things you just have to find out for yourself."

Explore Visual Basic
By Category
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Focus and Select

©2010 About.com, a part of The New York Times Company.

All rights reserved.