Dela via


CommandButton Control for Visual Basic 6.0 Users

The CommandButton control in Visual Basic 6.0 is replaced by the Windows Forms Button control in Visual Basic 2008. The names of some properties, methods, events, and constants are different, and in some cases there are differences in behavior.

Conceptual Differences

Default and Cancel Properties

In Visual Basic 6.0, the Boolean Default and Cancel properties of a CommandButton control determine if the control responds to the ENTER or ESC keys, respectively.

In Visual Basic 2008, the Button control no longer has Default or Cancel properties. The Form object has AcceptButton and CancelButton properties that provide the same functionality; these properties take the name of a Button control as an argument.

Value Property

In Visual Basic 6.0, the Boolean Value property of a CommandButton control indicates whether the control has been chosen; setting the property to True invokes the button's Click event.

In Visual Basic 2008, the Button control no longer has a Value property. You can use the GotFocus event to determine if the control is chosen; the PerformClick method can be used to invoke the Click event.

Style, Picture, DownPicture, and DisabledPicture Properties

In Visual Basic 6.0, setting the Style property of a CommandButton control to 1 – Graphical changes the appearance of the control to display images. The Picture, DownPicture, and DisabledPicture properties are used to assign images to be displayed in response to state changes. For example, when a CommandButton control is clicked, the DownPicture image is displayed; if the control is disabled, the DisabledPicture image is displayed.

Visual Basic 2008 no longer supports the Style, Picture, DownPicture, or DisabledPicture properties. The Style property is no longer necessary; assigning a picture to the Image property has the same effect as setting the Style property to Graphical. The Picture property is replaced by the Image property. The functionality of the DownPicture and DisabledPicture properties can be emulated using an ImageList control that contains multiple images.

MaskColor Property

In Visual Basic 6.0, the MaskColor property of a CommandButton control is used to define a color that becomes transparent, allowing a background image to show through. To use this property, the Style property has to be set to Graphical, the UseMaskColor property has to be set to True, and a bitmap has to be assigned to the Picture property.

In Visual Basic 2008, there is no direct equivalent for the MaskColor property. You can, however, set transparency on a control using the MakeTransparent method of a Bitmap object.

Other Differences

In addition, there are numerous conceptual differences that apply to all controls, including differences in data binding, font handling, drag and drop, Help support and more. For more information, see Windows Forms Controls for Visual Basic 6.0 Users.

Code Changes for the CommandButton Control

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

Code Changes for Setting Default and Cancel Buttons

The following example demonstrates setting two buttons to be the default and cancel buttons for a form.

' Visual Basic 6.0
' Set the first button to respond to the Enter key.
Command1.Default = True
' Set the second button to respond to the Esc key.
Command2.Cancel = True
' Visual Basic' Set the first button to respond to the Enter key.Me.AcceptButton = Button1
' Set the second button to respond to the Esc key.Me.CancelButton = Button2

Code Changes for Adding Transparency to a Button

The following example demonstrates methods for defining a transparent region on a button that contains an image; any portion of the image that is white will become transparent.

' Visual Basic 6.0
' Assumes a picture has been assigned to the Picture property
' and that the Style property has been set to Graphical.
Command1.UseMaskColor = True
Command1.MaskColor = vbWhite
' Visual Basic' Assumes a picture has been assigned to the BackgroundImage property.Dim ButtonBitmap AsNew System.Drawing.Bitmap(Button1.BackgroundImage)
ButtonBitmap.MakeTransparent(System.Drawing.Color.White)
Button1.BackgroundImage = ButtonBitmap

CommandButton Control Property, Method, and Event Equivalencies

The following tables list Visual Basic 6.0 properties, methods, and events, along with their Visual Basic 2008 equivalents. Those properties, methods, and events that have the same names and behaviors are not listed. Where applicable, constants are indented beneath the property or method. All Visual Basic 2008 enumerations map to the System.Windows.Forms namespace unless otherwise noted.

These tables list links to topics explaining differences in behavior. Where there is no direct equivalent in Visual Basic 2008, links are provided to alternatives.

Properties

Visual Basic 6.0

Visual Basic 2008 Equivalent

Appearance

New implementation. For more information, see Appearance and BorderStyle Properties for Visual Basic 6.0 Users.

BackColor

BackColor

NoteNote:
Colors are handled differently in Visual Basic 2008. For more information, see Color Handling for Visual Basic 6.0 Users.

Cancel

CancelButton (Form object)

Caption

Text

Container

Parent

Default

AcceptButton (Form object)

DisabledPicture

DownPicture

New implementation. For more information, see Style Property for Visual Basic 6.0 Users.

DragIcon

DragMode

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

Font

FontBold

FontItalic

FontName

FontSize

FontStrikethrough

FontUnderline

Font

NoteNote:
Fonts are handled differently in Visual Basic 2008. For more information, see Font Object for Visual Basic 6.0 Users.

Height

Height, Size

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

HelpContextID

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

HWnd

Handle

Index

New implementation. For more information, see Control Arrays for Visual Basic 6.0 Users.

Left

Left

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

MaskColor

New implementation. For more information, see MaskColor for Visual Basic 6.0 Users.

MouseIcon

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

MousePointer

Cursor

For a list of constants, see MousePointer for Visual Basic 6.0 Users.

OLEDropMode

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

Parent

FindForm method

Picture

Image

RightToLeft

RightToLeft

Style

New implementation. For more information, see Style Property for Visual Basic 6.0 Users.

ToolTipText

ToolTip component

For more information, see ToolTip Support for Visual Basic 6.0 Users.

Top

Top

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

UseMaskColor

New implementation. For more information, see MaskColor for Visual Basic 6.0 Users.

Value

New implementation. The PerformClick method is the same as setting the Value to True; for any other use of the Value property there is no equivalent.

WhatsThisHelpID

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

Width

Width, Size

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

Methods

Visual Basic 6.0

Visual Basic 2008 Equivalent

Drag

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

Move

SetBounds

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

OLEDrag

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

SetFocus

Focus

ShowWhatsThis

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

ZOrder

BringToFront or SendToBack function

Events

Visual Basic 6.0

Visual Basic 2008 Equivalent

DragDrop

DragOver

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

GotFocus

Enter

LostFocus

Leave

OLECompleteDrag

OLEDragDrop

OLEDragOver

OLEGiveFeedback

OLESetData

OLEStartDrag

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

Upgrade Notes

When upgrading a Visual Basic 6.0 application using the Upgrade Wizard, any CommandButton controls are upgraded to Windows Forms Button controls and code is updated to use the equivalent properties, methods, and events. Where there are no equivalents or where there are potential behavioral differences, comments are inserted in the code with links to Help topics.

When an application is upgraded to Visual Basic 2008, if the Style property is set to 1 – Graphical, the FlatStyle property of the upgraded control is set to Standard, and any image assigned to the Picture property at design time is assigned to the Image property of the upgraded control.

If the DownPicture or DisabledPicture properties were set at design time or at run time, you will need to modify your upgraded application to use an ImageList control. For more information, see How to: Emulate a Visual Basic 6.0 Tri-State Control in an Upgraded Application.

The Upgrade Wizard does not upgrade code that uses the MaskColor property. Upgrade warnings will be inserted into your code. You will need to modify the code before running the application.

See Also

Concepts

MaskColor for Visual Basic 6.0 Users