userform中命令按钮的自定义颜色

时间:2015-10-20 10:26:57

标签: excel vba 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

3 个答案:

答案 0 :(得分:2)

只有当第二个命令按钮位于名为blogs = Blog.object.filter(tags__tag__in=['1', '2', '3']) 的用户窗体中时,您的代码才有效,因为事件处理程序中有这些内容:UserForm2

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

UserForm

答案 1 :(得分:1)

在@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

答案 2 :(得分:0)

我认为最好使用Private Sub UserForm_Initialize(),在这里你可以拥有你在Userform中使用的所有CommandButtons,Labels,Textboxes等的所有格式:)

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