更新:2007 年 11 月
Visual Basic 6.0 中的 CommandButton 控件在 Visual Basic 2008 中由 Windows 窗体 Button 控件所取代。某些属性、方法、事件和常数的名称是不同的,在某些情况下,行为也有所不同。
概念差异
Default 和 Cancel 属性
在 Visual Basic 6.0 中,CommandButton 控件的布尔型 Default 和 Cancel 属性分别确定控件是否响应 Enter 或 Esc 键。
在 Visual Basic 2008 中,Button 控件不再具有 Default 和 Cancel 属性。Form 对象的 AcceptButton 和 CancelButton 属性可提供相同功能;这些属性将 Button 控件的名称作为参数。
Value 属性
在 Visual Basic 6.0 中,CommandButton 控件的布尔型的 Value 属性指示是否已选择该控件;将该属性设置为 True 将调用按钮的 Click 事件。
在 Visual Basic 2008 中,Button 控件不再具有 Value 属性。可以使用 GotFocus 事件确定是否选择了控件;PerformClick 方法可用于调用 Click 事件。
Style、Picture、DownPicture 和 DisabledPicture 属性
在 Visual Basic 6.0 中,将 CommandButton 控件的“Style”属性设置为“1 – Graphical”可更改控件的外观以显示图像。Picture、DownPicture 和 DisabledPicture 属性用于分配为响应状态更改所显示的图像。例如,单击 CommandButton 控件时,显示 DownPicture 图像;如果禁用该控件,则显示 DisabledPicture 图像。
Visual Basic 2008 不再支持 Style、Picture、DownPicture 和 DisabledPicture 属性。Style 属性不再是必需的;将图片指定给 Image 属性和将 Style 属性设置为 Graphical 的具有相同的效果。Picture 属性被 Image 属性所取代。可使用包含多个图像的 ImageList 控件模拟 DownPicture 和 DisabledPicture 属性的功能。
MaskColor 属性
在 Visual Basic 6.0 中,CommandButton 控件的 MaskColor 属性用于定义透明的颜色,以便能够显示背景图像。若要使用此属性,“Style”属性必须设置为“Graphical”,UseMaskColor 属性必须设置为 True,并且必须将一幅位图赋给 Picture 属性。
在 Visual Basic 2008 中没有 MaskColor 属性的直接等效项。但是可以使用 Bitmap 对象的 MakeTransparent 方法设置控件的透明度。
其他差异
此外,还有许多适用于所有控件的概念性差异,例如数据绑定、字体处理、拖放、帮助支持等方面的差异。有关更多信息,请参见 适用于 Visual Basic 6.0 用户的 Windows 窗体控件。
CommandButton 控件的代码更改
下面的代码示例演示 Visual Basic 6.0 和 Visual Basic 2008 在编码方法上的不同之处。
设置 Default 和 Cancel 按钮代码更改
下面的示例演示将窗体的两个按钮设置为 default 和 cancel 按钮。
' 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
为按钮添加透明度的代码更改
下面的示例演示定义包含图像的按钮上的透明区域的方法;图像上的所有白色部分将变为透明。
' 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 As New System.Drawing.Bitmap(Button1.BackgroundImage)
ButtonBitmap.MakeTransparent(System.Drawing.Color.White)
Button1.BackgroundImage = ButtonBitmap
CommandButton 控件的属性、方法和事件的等效项
下表列出了 Visual Basic 6.0 属性、方法和事件,以及它们的 Visual Basic 2008 等效项。具有相同的名称和行为的属性、方法和事件未予列出。如果适用,常量在属性或方法的下面缩进。除非另有说明,否则所有的 Visual Basic 2008 枚举都映射到 System.Windows.Forms 命名空间。
下面的表列出指向解释行为差异的主题的链接。如果 Visual Basic 2008 中没有直接等效项,则提供指向介绍替代项的链接。
属性
| Visual Basic 6.0 | Visual Basic 2008 等效项 | ||
|---|---|---|---|
| Appearance | 新的实现。有关更多信息,请参见 Appearance 和 BorderStyle 属性(针对 Visual Basic 6.0 用户)。 | ||
| BackColor | 
 | ||
| Cancel | CancelButton(Form 对象) | ||
| Caption | |||
| Container | |||
| Default | AcceptButton(Form 对象) | ||
| DisabledPicture DownPicture | 新的实现。有关更多信息,请参见 Style 属性(针对 Visual Basic 6.0 用户)。 | ||
| DragIcon DragMode | 新的实现。有关更多信息,请参见拖放(针对 Visual Basic 6.0 用户)。 | ||
| Font FontBold FontItalic FontName FontSize FontStrikethrough FontUnderline | 
 | ||
| Height | 
 | ||
| HelpContextID | 新的实现。有关更多信息,请参见帮助支持(针对 Visual Basic 6.0 用户)。 | ||
| HWnd | |||
| Index | 新的实现。有关更多信息,请参见控件数组(针对 Visual Basic 6.0 用户)。 | ||
| Left | 
 | ||
| MaskColor | 新的实现。有关更多信息,请参见 MaskColor(针对 Visual Basic 6.0 用户)。 | ||
| MouseIcon | 新的实现。有关更多信息,请参见无法设置自定义 MousePointer。 | ||
| MousePointer | 有关常数的列表,请参见 MousePointer(针对 Visual Basic 6.0 用户)。 | ||
| OLEDropMode | 新的实现。有关更多信息,请参见拖放(针对 Visual Basic 6.0 用户)。 | ||
| Parent | FindForm 方法 | ||
| Picture | |||
| RightToLeft | |||
| Style | 新的实现。有关更多信息,请参见 Style 属性(针对 Visual Basic 6.0 用户)。 | ||
| ToolTipText | ToolTip 组件 有关更多信息,请参见工具提示支持(针对 Visual Basic 6.0 用户)。 | ||
| Top | 
 | ||
| UseMaskColor | 新的实现。有关更多信息,请参见 MaskColor(针对 Visual Basic 6.0 用户)。 | ||
| Value | 新的实现。PerformClick 方法和将 Value 设置为 True 的效果相同;对于 Value 属性的任何其他用途,没有等效项。 | ||
| WhatsThisHelpID | 新的实现。有关更多信息,请参见帮助支持(针对 Visual Basic 6.0 用户)。 | ||
| Width | 
 | 
方法
| Visual Basic 6.0 | Visual Basic 2008 等效项 | ||
|---|---|---|---|
| Drag | 新的实现。有关更多信息,请参见拖放(针对 Visual Basic 6.0 用户)。 | ||
| Move | 
 | ||
| OLEDrag | 新的实现。有关更多信息,请参见拖放(针对 Visual Basic 6.0 用户)。 | ||
| SetFocus | |||
| ShowWhatsThis | 新的实现。有关更多信息,请参见帮助支持(针对 Visual Basic 6.0 用户)。 | ||
| ZOrder | BringToFront 或 SendToBack 函数 | 
事件
| Visual Basic 6.0 | Visual Basic 2008 等效项 | 
|---|---|
| DragDrop DragOver | 新的实现。有关更多信息,请参见拖放(针对 Visual Basic 6.0 用户)。 | 
| GotFocus | |
| LostFocus | |
| OLECompleteDrag OLEDragDrop OLEDragOver OLEGiveFeedback OLESetData OLEStartDrag | 新的实现。有关更多信息,请参见拖放(针对 Visual Basic 6.0 用户)。 | 
升级说明
使用升级向导升级 Visual Basic 6.0 应用程序时,任何 CommandButton 控件将升级到 Windows 窗体 Button 控件,并且更新代码以使用等效的属性、方法和事件。没有等效项或可能存在行为差异的地方,将在代码中插入注释和指向帮助主题的链接。
将应用程序升级到 Visual Basic 2008 时,如果 Style 属性设置为 1 – Graphical,则升级后的控件的 FlatStyle 属性将设置为 Standard,并且将在设计时分配给 Picture 属性的任何图像都分配给升级后的控件的 Image 属性。
如果设计时或运行时设置了 DownPicture 或 DisabledPicture 属性,则需要修改升级后的应用程序,使之使用 ImageList 控件。有关更多信息,请参见如何:在升级应用程序中模拟 Visual Basic 6.0 三态控件。
升级向导不升级使用 MaskColor 属性的代码。代码中将插入升级警告。运行应用程序前需要修改代码。
.gif) 说明:
说明: