Share via


Visual Basic 6.0 使用者可用的 CommandButton 控制項

更新:2007 年 11 月

Visual Basic 6.0 的 CommandButton 控制項在 Visual Basic 2008 中會以 Windows Form Button 控制項取代。有些屬性 (Property)、方法、事件及常數的名稱會改變,而且在某些情況下,行為方面也會有差異。

概念上的差異

Default 和 Cancel 屬性

在 Visual Basic 6.0 中,CommandButton 控制項的布林 (Boolean) Default 和 Cancel 屬性,會決定控制項是否會分別回應 ENTER 或 ESC 鍵。

在 Visual Basic 2008 中,Button 控制項不再具有 Default 或 Cancel 屬性。Form 物件具有 AcceptButtonCancelButton 屬性,可提供相同的功能。這些屬性會使用 Button 控制項的名稱當做引數。

Value 屬性

在 Visual Basic 6.0 中,CommandButton 控制項的布林 Value 屬性指出是否已選擇控制項。將屬性設為 True,會叫用 (Invoke) 按鈕的 Click 事件。

在 Visual Basic 2008 中,Button 控制項不再具有 Value 屬性。您可以使用 GotFocus 事件,判斷是否已選擇控制項。PerformClick 方法可用來叫用 Click 事件。

Style、Picture、DownPicture 和 DisabledPicture 屬性

在 Visual Basic 6.0 中,將 CommandButton 控制項的 [Style] 屬性設定為 [1 – 圖形],會變更控制項的外觀,以顯示影像。Picture、DownPicture 和 DisabledPicture 屬性可用來指派將顯示的影像,以回應狀態變更。例如,按一下 CommandButton 控制項時,會顯示 DownPicture 影像。如果停用控制項,則會顯示 DisabledPicture 影像。

Visual Basic 2008 已不再支援 Style、Picture、DownPicture 或 DisabledPicture 屬性。Style 屬性不再必要,將圖片指派給 Image 屬性和將 Style 屬性設定為 Graphical 有相同的效果。Picture 屬性已由 Image 屬性取代。DownPicture 和 DisabledPicture 屬性的功能,可以使用包含多個影像的 ImageList 控制項來模擬。

MaskColor 屬性

在 Visual Basic 6.0 中,CommandButton 控制項的 MaskColor 屬性可用來定義成為透明的色彩,讓背景影像可以顯示出來。若要使用這個屬性,[Style] 屬性必須設定為 [Graphical]、UseMaskColor 屬性必須設定為 True,而且必須指派點陣圖給 Picture 屬性。

在 Visual Basic 2008 中,沒有 MaskColor 屬性的直接對等用法。不過,您可以使用 Bitmap 物件的 MakeTransparent 方法,設定控制項的透明度。

其他差異

此外,還有許多概念不同處,適用於所有控制項,包括資料繫結、字型處理、拖放、說明支援等方面的差別。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的 Windows Form 控制項

CommandButton 控制項的程式碼變更

下列程式碼範例說明 Visual Basic 6.0 和 Visual Basic 2008 之間,程式碼撰寫技術的差異。

設定預設值和取消按鈕的程式碼變更

下列範例會示範將兩個按鈕設為表單的預設值按鈕和取消按鈕。

' 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

加入透明度至按鈕的程式碼變更

下列範例會示範在含有影像的按鈕上定義透明工作區 (Transparent Region) 的方法,影像中的白色部分會變成透明。

' 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 列舉型別 (Enumeration) 都會對應到 System.Windows.Forms 命名空間 (Namespace)。

這些表格列出了說明行為差異的主題連結。若 Visual Basic 2008 中沒有直接的對等用法時,則提供連結至替代的主題。

屬性

Visual Basic 6.0

Visual Basic 2008 對等用法

Appearance

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的 Appearance 和 BorderStyle 屬性

BackColor

BackColor

注意事項:

在 Visual Basic 2008 中會以不同方式處理色彩。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的色彩處理方式

Cancel

CancelButton (Form 物件)

Caption

Text

Container

Parent

Default

AcceptButton (Form 物件)

DisabledPicture

DownPicture

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的樣式屬性

DragIcon

DragMode

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的拖放動作

Font

FontBold

FontItalic

FontName

FontSize

FontStrikethrough

FontUnderline

Font

注意事項:

在 Visual Basic 2008 中會以不同方式處理字型。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的字型物件

Height

Height, Size

注意事項:

在 Visual Basic 2008 中會以不同方式處理座標。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的座標系統

HelpContextID

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的說明支援

HWnd

Handle

Index

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的控制項陣列

Left

Left

注意事項:

在 Visual Basic 2008 中會以不同方式處理座標。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的座標系統

MaskColor

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的 MaskColor

MouseIcon

新實作。如需詳細資訊,請參閱無法設定自訂 MousePointer

MousePointer

Cursor

如需常數的清單,請參閱 Visual Basic 6.0 使用者可用的 MousePointer

OLEDropMode

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的拖放動作

Parent

FindForm 方法

Picture

Image

RightToLeft

RightToLeft

Style

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的樣式屬性

ToolTipText

ToolTip 元件

如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的 ToolTip 支援

Top

Top

注意事項:

在 Visual Basic 2008 中會以不同方式處理座標。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的座標系統

UseMaskColor

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的 MaskColor

Value

新實作。使用 PerformClick 方法和將 Value 設為 True 相同。對於 Value 屬性的其他用途來說,並沒有對等用法。

WhatsThisHelpID

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的說明支援

Width

Width, Size

注意事項:

在 Visual Basic 2008 中會以不同方式處理座標。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的座標系統

方法

Visual Basic 6.0

Visual Basic 2008 對等用法

Drag

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的拖放動作

Move

SetBounds

注意事項:

在 Visual Basic 2008 中會以不同方式處理座標。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的座標系統

OLEDrag

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的拖放動作

SetFocus

Focus

ShowWhatsThis

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的說明支援

ZOrder

BringToFrontSendToBack 函式

事件

Visual Basic 6.0

Visual Basic 2008 對等用法

DragDrop

DragOver

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的拖放動作

GotFocus

Enter

LostFocus

Leave

OLECompleteDrag

OLEDragDrop

OLEDragOver

OLEGiveFeedback

OLESetData

OLEStartDrag

新實作。如需詳細資訊,請參閱 Visual Basic 6.0 使用者可用的拖放動作

升級注意事項

使用升級精靈升級 Visual Basic 6.0 應用程式時,所有 CommandButton 控制項都會升級至 Windows Form Button 控制項,而且程式碼會更新為使用對等的屬性、方法和事件。當沒有對等用法或可能有行為上的差異時,程式碼中會插入註解,其中包含至說明主題的連結。

當應用程式升級為 Visual Basic 2008 時,如果 Style 屬性設為 1 – Graphical,升級後控制項的 FlatStyle 屬性會設為 Standard,而所有在設計階段指派給 Picture 屬性的影像,都會指派給升級後控制項的 Image 屬性。

如果已在設計階段或執行階段時設定 DownPicture 或 DisabledPicture 屬性,您需要將升級後的應用程式修改為使用 ImageList 控制項。如需詳細資訊,請參閱 HOW TO:在升級應用程式中模擬 Visual Basic 6.0 三種狀態的控制項

升級精靈不會升級使用 MaskColor 屬性的程式碼。升級警告將插入至您的程式碼中。在您執行應用程式之前需先修改程式碼。

請參閱

概念

Visual Basic 6.0 使用者可用的 MaskColor