Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MousePointer 1

Status
Not open for further replies.

TRuff

Programmer
Dec 13, 2002
6
US
I am trying to change the mouse pointer to an hourglass in an event procedure. In most cases this is easy, but in any event procedure that has a msgBox or CommonDialog object the pointer wiil not change from the default (arrow) to the hourglass. I have tried many of the suggested fixes in other threads. Any other suggestions?
 
Are you sure about this? I've never had difficulty turning on and off the hourglass.

Consider the following:

Private Sub Command1_Click()
Me.MousePointer = vbHourglass
CommonDialog1.ShowColor
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Me.MousePointer <> vbHourglass Then Me.MousePointer = vbHourglass
End Sub

When the commondialog.showcolor is called, it does take control of the mouse pointer, obviously, since it wouldn't look right to be choosing a color using an hourglass, but as soon as the dialog closes you again have control over the mouse pointer.

With a little more detail on what you're doing, perhaps I would understand your problem better.
Tuna - It's fat free until you add the Mayo!
 
Thanks for the response. Here is the abbreviated code for my procedure:

Private Sub CmdCompare_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)'Originally I tried the Click event but it wasn't working either

Variable declarations here


CommonDialog1.Filter = &quot;Excel Files (*.XLS)|*.XLS&quot;
CommonDialog1.ShowOpen
Set ExcelCmp = CreateObject(&quot;Excel.Application&quot;)
Set ExcelCmp = GetObject(CommonDialog1.filename)

For i = 1 To numPlot
Comprange(i) = ExcelCmp.Worksheets(1).range(&quot;A&quot; & i + 1).Value
Compangle(i) = ExcelCmp.Worksheets(1).range(&quot;B&quot; & i + 1).Value
Next i

ExcelCmp.Application.Quit
Set ExcelCmp = Nothing

CmdCompare.MousePointer = 11 'This isn't working here or before the common dialog call


'Sort new array
sortx = FncSort(Comprange(), Compangle())

Some looping code here to calculate Tot_per
Label6.Caption = Tot_per

CmdCompare.MousePointer = 0 'Pointer never changed

End Sub
 
You might try a DoEvents after the mouse pointer assignment.

CmdCompare.MousePointer = 11
DoEvents

sortx = FncSort(Comprange(), Compangle())
Label6.Caption = Tot_per

CmdCompare.MousePointer = 0
DoEvents
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks. I tried DoEvents both before and after my MousePointer calls. It did not help. Any other suggestions?
 
Firstly, you do realise that you are only setting the mousepointer for the command button, don't you? So if you never wave the mouse pointer over the button you'll never see the changed pointer.

Secondly, even if you follow pokermat suggestion of using

Screen.MousePointer = vbHourGlass

VB only changes the pointer when it is over a VB control or Form (rather than anywhere on the screen, as the name would lead one to believe). THIS IS AN IMPORTANT POINT. If your mouse pointer is not over a VB form or control when you call the function the pointer will NOT change. If you then go straight into tight loop (as you do), your pointer will stay as the default (since VB never gets a windows message to tell it that the pointer is over a VB window).

Solution:
Put DoEvents into your tight loop.

(A hack would be to temporarily move the pointer over the right form through code, issue a doevents, and then return the cursor to the original location just before calling the loop).



Hmm, I think I've explained badly, but I'm in a hurry...



 
Thanks for the response. I am aware that I was only changing the mousepointer for the command button, but I do the exact same thing in an event procedure for another command button that does not have a common dialog call and the mouse pointer changes as long as the mouse is over the form. Despite this, I did try Screen.MousePointer and Form.MousePointer and had the same problem. I will try putting a DoEvents inside the loop and I'll change back to Form.mousepointer = 11 and let you know. I appreciate the help.
 
Yes! The suggestion from strongm worked. Thanks very much. I changed to Form.MousePointer and added DoEvents inside my loops. The mystery of MousePointer working in my other event procedures is due to the mouse being over the command button when the MousePointer call was made. In event procedures where a msgBox or commondialog was called, the mouse was moved to respond to the new screen and the cmd.MousePointer call was missed. Thanks to all who responded.

 
The following should illustrate the point. You need two forms, the first should have 4 command buttons.
[tt]
Option Explicit
Private Declare Function SetCursorPos Lib &quot;user32&quot; (ByVal x As Long, ByVal y As Long) As Long

Private Sub Command1_Click()
' Appears to work as expected, but in fact this is because
' when you move the mouse pointer out of the form it get's switched
' by whatever application the mouse is now over, rather than by VB
' As far as VB is concerned it never (apart from the actual point you
' make the Mousepointer call) gets the opportunity
' to switch pointer as mouse moves in and out of form as the
' tight loop stops any mouse messages getting to it
' You can see this effect when you move the cursor over the overlapped Form2.
' You'd expect the pointer to change back to the default, but it doesn't.
Dim StartTime As Date
ButtonToggle
StartTime = Now

Form1.MousePointer = vbHourglass
Do Until DateDiff(&quot;s&quot;, StartTime, Now) > 10
Loop
Form1.MousePointer = vbDefault
ButtonToggle
End Sub

Private Sub Command2_Click()
' Now we defer to the OS in the loop, so VB gets a chance to
' switch the pointer. It looks exactly the same as above, but in fact
' VB is doing the pointer switching. Moving the pointer over the overlapped
' Form2. Unlike the previous method above, this time the cursor DOES change
Dim StartTime As Date

ButtonToggle
StartTime = Now
Form1.MousePointer = vbHourglass
Do Until DateDiff(&quot;s&quot;, StartTime, Now) > 10
DoEvents
Loop
Form1.MousePointer = vbDefault
ButtonToggle
End Sub


Private Sub Command3_Click()

' The SetCursorPos effectively does what having a common dialog
' or a MsgBox might do - i.e to move the cursor OFF the form for
' which we are setting the pointer
' VB only tries to switch the pointers when it gets a message from the mouse
' saying 'Hello, I'm over the form', and we prevent that ever happening
' by having a tight loop
Dim StartTime As Date
ButtonToggle

StartTime = Now

SetCursorPos 0, 0

Form1.MousePointer = vbHourglass
Do Until DateDiff(&quot;s&quot;, StartTime, Now) > 10
Loop
Form1.MousePointer = vbDefault
ButtonToggle
End Sub

Private Sub Command4_Click()
' Same as above, only this time we defer to the OS during the loop,
' so VB gets the necessary mouse messages, and we get the behaviour we want
Dim StartTime As Date
ButtonToggle

StartTime = Now

SetCursorPos 0, 0
Form1.MousePointer = vbHourglass
Do Until DateDiff(&quot;s&quot;, StartTime, Now) > 10
DoEvents
Loop
Form1.MousePointer = vbDefault
ButtonToggle
End Sub

Private Sub Form_Load()
' Just make sure form isn't placed at 0,0
Form1.Move 100, 100
' Offset Form2
Form2.Move Form1.Left + 300, Form1.Top + 300
Form2.Show
End Sub

Private Sub ButtonToggle()
Dim tmpButton As Control

For Each tmpButton In Form1.Controls
tmpButton.Enabled = Not tmpButton.Enabled
Next
End Sub
 
Thanks for the example code. I wanted to know why my problem was happening instead of just applying a fix on faith. Your code helped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top