跳转到内容

应用程序/命令按钮切换的 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

另请参阅

[编辑 | 编辑源代码]
华夏公益教科书