在用户窗体中自定义颜色的commandbutton
我已经成功地为我的用户窗体中的一个button(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
但是,当我将相同的代码应用到我的第二个button(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
只有当第二个命令button位于名为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
我认为这是最好使用私人小组UserForm_Initialize(),在这里你可以有所有你的格式所有的CommandButtons,标签,文本框等,你在你的Userform中使用:)
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