應用程式/命令按鈕切換的 Visual Basic
外觀
此 VBA 程式碼模組是為 Microsoft Excel 製作的,但很容易適應其他可以使用 VBA 執行使用者窗體的 Office 應用程式。
- 提供了兩種改進的CommandButton_Click()過程。它們在按下按鈕時顯示更好的強調。CmmandButton1_Click() 有一個切換操作,例如設定兩種可能的模式之一,而 CommandButton2_Click() 執行通常的單一功能任務。在每種情況下
- 使用者設定的標題在轉換時會發生變化以反映按鈕的當前狀態,例如正在執行等。這在切換狀態或當過程需要很長時間執行時可能很有用。
- 按鈕大小以其中心為中心膨脹。這避免了從固定左上角點進行放大。增加的量可以在程式中設定。
- 按鈕的背景顏色和字型顏色在程式碼中設定,因此很容易修改。兩種狀態可以使用不同的顏色。
- Me.Repaint行確保按鈕格式立即更新。如果它們不存在,過程將在完成之前開始,也許也會結束。如果沒有Me.Repaint,但當DoEvents存在於要執行的過程時,重新繪製似乎仍然可以正常工作,直到執行一個不需要DoEvents的過程為止。對於那些打算研究這一點的人來說,它在 CommandButton2_Click() 過程中的表現最好。
將UserForm_Initialize()、CommandButton1_Click()和CommandButton2_Click()過程複製到 Excel 專案的使用者窗體模組中。這可以透過首先插入一個名為UserForm1的窗體,其中包含一個名為CommandButton1的命令按鈕和另一個名為CommandButton2的按鈕來實現。雙擊設計模式中的窗體以訪問其模組。Workbook_Open()過程進入ThisWorkbook模組。然後,儲存工作簿,並執行Workbook_Open()(或重新開啟工作簿)以測試窗體上的按鈕。
- 2019 年 1 月 20 日,添加了之前遺漏的Me.Repaint程式碼行
Private Sub Workbook_Open()
'run this to show form
Load UserForm1
UserForm1.Show
End Sub
Option Explicit
Private Sub UserForm_Initialize()
With CommandButton1
.Height = 50
.Width = 50
.Caption = "Turn ON"
.BackColor = RGB(255, 205, 183)
End With
With CommandButton2
.Height = 50
.Width = 50
.Caption = "Turn ON"
.BackColor = RGB(255, 205, 183)
End With
End Sub
Private Sub CommandButton1_Click()
'TOGGLES caption, color and size-about-center
'of a typical CommandButton control, between
'say, two stable modes of working.
Dim nInc As Integer, n As Long
'set size increase (say 0 to 10)
nInc = 8
With CommandButton1
'run the OFF code
If .Caption = "Turn OFF" Then
.Width = .Width - nInc
.Height = .Height - nInc
.Caption = "Turn ON"
.Left = .Left + nInc / 2
.Top = .Top + nInc / 2
.BackColor = RGB(255, 205, 183)
.ForeColor = RGB(0, 0, 0)
Me.Repaint 'redraw form
'add procedure here for the OFF state
'(simulated here with a short delay)
For n = 1 To 100000
DoEvents 'yield as required
Next
Else 'run the ON code
.Width = .Width + nInc
.Height = .Height + nInc
.Caption = "Turn OFF"
.Left = .Left - nInc / 2
.Top = .Top - nInc / 2
.BackColor = RGB(255, 217, 183)
.ForeColor = RGB(0, 0, 0)
Me.Repaint 'redraw form
'add procedure here for the ON state
'(simulated here with a short delay)
For n = 1 To 100000
DoEvents 'yield as required
Next
End If
End With
End Sub
Private Sub CommandButton2_Click()
'Changes color and size-about-center
'of a typical CommandButton control,
'holds formats during process
'and restores all on exit.
Dim nInc As Integer, n As Long
'set size increase (say 0 to 10)
nInc = 8
'detect OFF state
With CommandButton2
If .Caption = "Turn ON" Then
.Width = .Width + nInc
.Height = .Height + nInc
.Caption = "Running"
.Left = .Left - nInc / 2
.Top = .Top - nInc / 2
.BackColor = RGB(255, 217, 183)
.ForeColor = RGB(0, 0, 0)
End If
End With
Me.Repaint 'redraw form
'add procedure here for the ON state
'(simulated here with a short delay)
For n = 1 To 100000
DoEvents 'yield as required
Next
'restore button just before exit
With CommandButton2
If .Caption = "Running" Then
.Width = .Width - nInc
.Height = .Height - nInc
.Caption = "Turn ON"
.Left = .Left + nInc / 2
.Top = .Top + nInc / 2
.BackColor = RGB(255, 205, 183)
.ForeColor = RGB(0, 0, 0)
End If
End With
Me.Repaint 'redraw form
End Sub