I am trying to use the MouseMove event wi开发者_如何学运维th a command button, but cannot figure out how to prevent the following error:
Compile error: Procedure declaration does not match description of event or procedure having the same name.
The code is very simple. I used "dummy" code here because I can never enter the event handler to get to something useful.
Private Sub Button_Name_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim X as Integer
x = x+1
End Sub
I have taken the parameter list directly from the object browser. I get similar results with other events, including DblClick(), but not Click(). I think that I must be making some fundamental error, but have not been able to identify it. Help!
I don't quite understand why you have taken the parameter list directly from the object browser. If you have the code of your user form open in the VBA window, you can select the command button in the left drop down list just above the code and the MouseMove event in the right drop down list. Then the required code is inserted automatically. It will slightly differ from your code:
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)
End Sub
The next error you will run into is a duplicate declaration of X. It's already a parameter of the subroutine. So you cannot reuse it within the subroutine.
With these two things fixed, the mouse move event will fire, even on a command button.
The error occurs because you have changed the default argument specification (you removed the ByVal
). The proper argument specifications appears automatically if you select MouseMove from the top-right drop-down menu in the VBA editor (after having selected your command button in the top-left drop-down). The argument specification should not be changed, else it won't work. VBA expects this specific list of arguments and if it finds something different, it won't compile. (The only thing you can change is the name of the argument variables -- but nothing else.)
This works:
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
X = X + 1 ' or whatever you need to do with X and Y
MsgBox "MouseMove! " & X & " " & Y
End Sub
I have to admit, I find that using a MouseMove event on a command button is a rather peculiar thing to do -- but maybe you have your reasons? I'd sure like to hear them.
Also, as pointed out by @Codo, you shouldn't re-declare X
inside the Sub. It's already been declared as an argument of the Sub, and you can use it straight away (e.g. X = X + 1
or whatever). You don't have to redeclare it using Dim
-- in fact, doing so will give a compile-time error.
精彩评论