Screen Object for Visual Basic 6.0 Users

The Visual Basic 6.0 Screen object has no direct equivalent in Visual Basic 2008, but most of its functionality can be duplicated using the .NET Framework.

Conceptual Differences

In Visual Basic 6.0, the Screen object provides access to the active form and control in your application, provides information about the screen where the application is being displayed, and allows you to control the appearance of the cursor.

In Visual Basic 2008, there is no direct equivalent for the Screen object, but most of its functionality can be duplicated using the .NET Framework.

Note

Visual Basic 2008 does have a Screen property—My.Computer.Screen. Unlike the Visual Basic 6.0 Screen object, My.Computer.Screen only returns read-only information about the screen, such as its device name, its working area, and its color depth. For more information, see My.Computer.Screen Property

ActiveControl Property

In Visual Basic 6.0, the ActiveControl property of the Screen object is used to determine the control that has focus. The ActiveControl property can be used in a global capacity, such as in Screen.ActiveControl, where the active control on the currently selected form is returned. If a specific form is referenced, for example, Form2.ActiveControl, ActiveControl specifies the control that will have the focus if the referenced form is active.

In Visual Basic 2008, there is no longer a global ActiveControl property; each instance of a form has its own ActiveControl property. When referencing a specific form, this property works just as it did in Visual Basic 6.0. To determine the active control on the currently selected form, you must first iterate through the OpenForms collection and check the ContainsFocus property to determine which form is active.

ActiveForm Property

In Visual Basic 6.0, the ActiveForm property of the Screen object is used to determine which form currently has the focus. If an MDI parent form has the focus, ActiveForm returns the MDI child form that last had the focus.

In Visual Basic 2008, there is no longer a global ActiveForm property. To determine the active form, you must iterate through the OpenForms collection and find the form with its ContainsFocus property set to True.

Visual Basic 2008 MDI parent forms (any forms with IsMDIContainer set to True) have an ActiveMDIChild property that can be used to return the active child form without using the OpenForms collection.

MousePointer Property

In Visual Basic 6.0, the MousePointer property of the Screen object is used to change the appearance of the cursor; once set it applies to all forms in the application.

In Visual Basic 2008, there is no longer a global MousePointer property; each form has a Cursor property that can be used to change the appearance of the cursor for that form only.

TwipsPerPixel Properties

In Visual Basic 6.0, the TwipsPerPixelX and TwipsPerPixelY properties of the Screen object are used to convert screen measurements from a logical twip (the standard unit of measurement in Visual Basic 6.0) to pixels.

In Visual Basic 2008, pixels are the standard unit of measurement; there is no longer any need for conversion.

Code Changes for the Screen Object

The following examples illustrate differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.

Code Changes for Determining the Active Control

The following code demonstrates copying the text from the currently selected control on the currently selected form to the Clipboard.

' Visual Basic 6.0
If TypeOf Screen.ActiveControl Is TextBox Then
    Clipboard.SetText Screen.ActiveControl.Text
End If
' Visual BasicDim i AsIntegerFor i = 0 To My.Application.OpenForms.Count - 1
  If My.Application.OpenForms.Item(i).ContainsFocus ThenIfTypeOf (My.Application.OpenForms.Item(i).ActiveControl) _
      Is TextBox Then
        My.Computer.Clipboard.SetText(My.Application.OpenForms. _
          Item(i).ActiveControl.Text)
    EndIfEndIfNext

Code Changes for Determining the Active Form

The following code demonstrates changing the caption of the currently selected form.

' Visual Basic 6.0
Screen.ActiveForm.Caption = "This is the selected form"
' Visual BasicDim i AsIntegerFor i = 0 To My.Application.OpenForms.Count - 1
  If My.Application.OpenForms.Item(i).ContainsFocus Then
    My.Application.OpenForms.Item(i).Text = _
      "This is the selected form"EndIfNext

Code Changes for Determining the Active Form in an MDI Application

The following code demonstrates changing the caption of the currently selected MDI child form.

' Visual Basic 6.0
Screen.ActiveForm.Caption = "This is the selected child form"
' Visual BasicMe.ActiveMdiChild.Text = "This is the selected child form"

Screen Object Property Equivalencies

The following table lists Visual Basic 6.0 properties and their Visual Basic 2008 equivalents. Links are provided as necessary to topics explaining differences in behavior. Where there is no direct equivalent in Visual Basic 2008, links are provided to topics that present alternatives.

Properties

Visual Basic 6.0

Visual Basic 2008 Equivalent

ActiveControl

My.Application.OpenForms(0).ActiveControl

ActiveForm

My.Application.OpenForms(0).ContainsFocus or

ActiveMdiChild (MDI applications)

FontCount

Fonts

New implementation. The behavior for enumerating fonts is different. For more information, see Font Handling for Visual Basic 6.0 Users.

Height

My.Computer.Screen.Bounds.Height

MouseIcon

New implementation. For more information, see Cannot set a custom MousePointer.

MousePointer

System.Windows.Forms.Cursor

TwipsPerPixelX

TwipsPerPixelY

New implementation. In Visual Basic 2008, coordinates are in pixels; twips are not used as a unit of measurement.

Width

My.Computer.Screen.Bounds.Width

Upgrade Notes

When a Visual Basic 6.0 application is upgraded to Visual Basic 2008, any Screen object properties are upgraded to their Visual Basic 2008 equivalent. Where there may be differences in behavior, upgrade comments are inserted into the code.

See Also

Concepts

App Object for Visual Basic 6.0 Users

Font Handling for Visual Basic 6.0 Users

Reference

My.Application.OpenForms Property

My.Computer.Screen Property