用户窗体中命令按钮的自定义颜色

Custom colors of commandbutton in userform

我已经成功地为我的用户表单中的一个按钮 (commandbutton_1) 的字体和背景着色:

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
    CommandButton1.BackColor = RGB(220, 230, 241)
    CommandButton1.ForeColor = RGB(0, 0, 0)
End Sub


Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
    CommandButton1.BackColor = RGB(22, 54, 92)
    CommandButton1.ForeColor = RGB(255, 255, 255)
End Sub

Private Sub UserForm_Activate()
    CommandButton1.BackColor = RGB(22, 54, 92)
    CommandButton1.ForeColor = RGB(225, 225, 225)
    UserForm.BackColor = RGB(22, 54, 92)
End Sub

但是当我将相同的代码应用到我的第二个按钮 (CommandButton2) 时,它无法正常工作:

Private Sub CommandButton2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
    CommandButton2.BackColor = RGB(220, 230, 241)
    CommandButton2.ForeColor = RGB(0, 0, 0)
End Sub


Private Sub UserForm2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
    CommandButton2.BackColor = RGB(22, 54, 92)
    CommandButton2.ForeColor = RGB(255, 255, 255)
End Sub

Private Sub UserForm2_Activate()
    CommandButton2.BackColor = RGB(22, 54, 92)
    CommandButton2.ForeColor = RGB(225, 225, 225)
    UserForm.BackColor = RGB(22, 54, 92)
End Sub

只有当第二个命令按钮位于名为 UserForm2 的用户窗体中时,您的代码才会起作用,因为这就是您的事件处理程序中的内容:UserForm2_MouseMove

将代码合并到您调用的表单的事件中UserForm:

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
    CommandButton1.BackColor = RGB(22, 54, 92)
    CommandButton1.ForeColor = RGB(255, 255, 255)

    CommandButton2.BackColor = CommandButton1.BackColor
    CommandButton2.ForeColor = CommandButton1.ForeColor
End Sub

在@Alex K. 的帮助下,我找到了答案:

    Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
    CommandButton1.BackColor = RGB(220, 230, 241)
    CommandButton1.ForeColor = RGB(0, 0, 0)
End Sub

Private Sub CommandButton2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
    CommandButton2.BackColor = RGB(220, 230, 241)
    CommandButton2.ForeColor = RGB(0, 0, 0)
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
    CommandButton1.BackColor = RGB(22, 54, 92)
    CommandButton1.ForeColor = RGB(255, 255, 255)

    CommandButton2.BackColor = CommandButton1.BackColor
    CommandButton2.ForeColor = CommandButton1.ForeColor
End Sub

Private Sub UserForm_Activate()
    CommandButton1.BackColor = RGB(22, 54, 92)
    CommandButton1.ForeColor = RGB(225, 225, 225)

    CommandButton2.BackColor = CommandButton1.BackColor
    CommandButton2.ForeColor = CommandButton1.ForeColor

    UserForm.BackColor = RGB(22, 54, 92)
End Sub

我认为最好使用 Private Sub UserForm_Initialize() ,在这里您可以拥有您在用户表单中使用的所有命令按钮、标签、文本框等的所有格式 :)

Private Sub UserForm_Initialize()

    CommandButton1.BackColor = RGB(22, 54, 92)
    CommandButton1.ForeColor = RGB(225, 225, 225)
    UserForm.BackColor = RGB(22, 54, 92)

    CommandButton2.BackColor = RGB(22, 54, 92)
    CommandButton2.ForeColor = RGB(255, 255, 255)

End Sub