Results 1 to 22 of 22

Thread: timer and mousemove problems

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Resolved timer and mousemove problems

    Hi folks.

    I am creating a screensaver app, using a timer to display a different picture every 10 seconds.

    1. Why does my code result in an initial blank picturebox displayed for 10 seconds, and then the other pics are displayed after? I don't understand why the blank picturebox is displayed.

    2. I want the form to close when the mouse is moved(the screensaver to stop running), so I set the mousemove event to close the form, but when I move the mouse, the form doesn't close. Why?

    Thanks in advance.

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Static ctr As Integer = 0
            Dim pics() As Image = {My.Resources._1, My.Resources._2, My.Resources._3, My.Resources._4, My.Resources._5, My.Resources._6}
    
            PictureBox1.Image = pics(ctr)
            ctr = ctr + 1
            If ctr > 5 Then
                ctr = 0
            End If
        End Sub
    
        Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
            Me.Close()
        End Sub
    
        Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            Me.Close()
        End Sub
    End Class
    Last edited by vbforever23; May 9th, 2013 at 07:08 AM.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: timer and mousemove problems

    I don't know what's the cause of your first problem but I'm willing to bet that you spread the PictureBox on the whole surface of the Form hence the cause of your second problem. The PictureBox and not the Form would receive the MouseMove. You need to trap PictureBox1's MouseMove.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: timer and mousemove problems

    As for the first problem, the simplest and least likely answer is that your first image is blank. If that isn't the case, try this right after you set the image:

    Picturebox1.Refresh

    This may have some negative impacts on the display, such as flickering, but it would be an interesting test to see whether it did cause the first image to show correctly.
    My usual boring signature: Nothing

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: timer and mousemove problems

    The only place you set the Image is in the Timer Tick sub. But if the timer interval is 10 seconds, the first tick will happen 10 seconds after you start it. So it's not surprising that the PictureBox stays blank for 10 seconds.

    The answer is to set the first image in the Load sub, just before or after you start the timer: PictureBox1.Image = My.Resources._1. Then, in the Timer Tick sub, declare ctr = 1 instead of 0.

    NN

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: timer and mousemove problems

    Thanks for the replies.

    The picturebox fills the form, and the form is maximised. When I set the mousemove for the picturebox, the form doesn't even load at all(or it loads and closes too fast to see). This is the same effect as mousehover(when the mouse is over the picturebox). I would like the screensaver to stop when the mouse is moved, not when the mouse is over the picturebox, which fills the whole screen. This results in the program stopping immediately after loading.

    The first image is definitely not blank. The strange thing is, this also happens when I have an array of strings(instead of an array of pics) - a blank textbox is displayed, before the strings are displayed. So the reason has to do with the other bits of code. Is anyone else experiencing this? Very strange.

    Thank you.

    EDIT: Didn't see boops boops reply whilst I was typing this. Will change the code when I am on the other machine later, and will mark the thread resolved if necessary.
    Last edited by vbforever23; May 8th, 2013 at 07:48 PM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: timer and mousemove problems

    Adding mousemoves to the picturebox causes the program to immediately close - in the title bar of Visual Basic 2010 Express, "(running)" quickly displays, and then immediately disappears. Adding mousemoves to the form results in nothing happening when the mouse is moved. Help? :/

  7. #7
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: timer and mousemove problems

    if your using this as a screensaver,
    why not just simple have the code to display the pictures and remove everything else, save your program as "program.SCR" and let windows handle when it apears and disapears,
    just put it with the other SCR files in window DIR, or manually select it.
    im assuming windows 7 handles screen savers just like XP.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: timer and mousemove problems

    Quote Originally Posted by GBeats View Post
    if your using this as a screensaver,
    why not just simple have the code to display the pictures and remove everything else, save your program as "program.SCR" and let windows handle when it apears and disapears,
    just put it with the other SCR files in window DIR, or manually select it.
    im assuming windows 7 handles screen savers just like XP.
    Thanks, but that's above my level of experience.

    This is the code in my mousemove handler. Picturebox fills the screen. I start the program with F5, and am not even touching the mouse, so I don't know why the program would run then close immediately. If I remove the mousemove handler and click the picturebox, then the program works as expected.

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Static ctr As Integer = 0
            Dim pics() As Image = {My.Resources._1, My.Resources._2, My.Resources._3, My.Resources._4, My.Resources._5, My.Resources._6}
    
            PictureBox1.Image = pics(ctr)
            ctr = ctr + 1
            If ctr > 5 Then
                ctr = 0
            End If
        End Sub
    
        Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
            Me.Close()
        End Sub
    
        
        Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            Me.Close()
        End Sub
    End Class

  9. #9
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: timer and mousemove problems

    to let windows handle it is sooooo much easier all you need is this

    Code:
    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Static ctr As Integer = 0
            Dim pics() As Image = {My.Resources._1, My.Resources._2, My.Resources._3, My.Resources._4, My.Resources._5, My.Resources._6}
    
            PictureBox1.Image = pics(ctr)
            ctr = ctr + 1
            If ctr > 5 Then
                ctr = 0
            End If
        End Sub
    save it
    get your exe file from the project folder
    change the name to <name>.SCR
    and thats it

    just set your screensaver on windows to your program and it will open and close automatically because windows will do that by itself.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  10. #10
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: timer and mousemove problems

    Sorry i just looked at a book i have here, you will still need to handle the mouse_move event to close it.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  11. #11
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: timer and mousemove problems

    I've had instances of unexpected methods for various controls being run on form Load too. Try this, it may or may not work. It is designed to prevent the sub from running if it is called by form Load.
    Code:
    Public Class Form1
    Dim FirstTime as Boolean = True
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Static ctr As Integer = 0
            Dim pics() As Image = {My.Resources._1, My.Resources._2, My.Resources._3, My.Resources._4, My.Resources._5, My.Resources._6}
    
            PictureBox1.Image = pics(ctr)
            ctr = ctr + 1
            If ctr > 5 Then
                ctr = 0
            End If
        End Sub
    
        Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
            Me.Close()
        End Sub
    
        
        Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
           If FirstTime= True Then
              FirstTime=False
           Else
              Me.Close()
           End If
        End Sub
    End Class

  12. #12
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: timer and mousemove problems

    try using the windows.forms.threading.sleep 'i think its that namespace'

    let it sleep for a second after you start the counter see if that helps
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: timer and mousemove problems

    Quote Originally Posted by Españolita View Post
    I've had instances of unexpected methods for various controls being run on form Load too. Try this, it may or may not work. It is designed to prevent the sub from running if it is called by form Load.
    Code:
    Public Class Form1
    Dim FirstTime as Boolean = True
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Static ctr As Integer = 0
            Dim pics() As Image = {My.Resources._1, My.Resources._2, My.Resources._3, My.Resources._4, My.Resources._5, My.Resources._6}
    
            PictureBox1.Image = pics(ctr)
            ctr = ctr + 1
            If ctr > 5 Then
                ctr = 0
            End If
        End Sub
    
        Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
            Me.Close()
        End Sub
    
        
        Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
           If FirstTime= True Then
              FirstTime=False
           Else
              Me.Close()
           End If
        End Sub
    End Class
    That got some results. The screensaver shows the first pic for about 3 seconds, and then the program closes. If I move the mouse any time before 3 seconds, then it closes, as expected. Now if only the program would keep running.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: timer and mousemove problems

    Quote Originally Posted by GBeats View Post
    save it
    get your exe file from the project folder
    change the name to <name>.SCR
    and thats it

    just set your screensaver on windows to your program and it will open and close automatically because windows will do that by itself.
    Thanks for the info. It really does seem easy. I'll still try to get it to work my way, so I can learn from it. Thanks.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: timer and mousemove problems

    Quote Originally Posted by boops boops View Post
    The only place you set the Image is in the Timer Tick sub. But if the timer interval is 10 seconds, the first tick will happen 10 seconds after you start it. So it's not surprising that the PictureBox stays blank for 10 seconds.

    The answer is to set the first image in the Load sub, just before or after you start the timer: PictureBox1.Image = My.Resources._1. Then, in the Timer Tick sub, declare ctr = 1 instead of 0.

    NN
    Works perfectly, thanks.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: timer and mousemove problems

    I am going to mark this thread as resolved. My first problem has been fixed, and this mousemove problem is confusing but I can just click the mouse or press a key to close the screensaver. Thanks for all the help.

  17. #17
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: timer and mousemove problems

    Quote Originally Posted by vbforever23 View Post
    I'll still try to get it to work my way, so I can learn from it.
    Then why are you abandoning this thread, it is not resolved.
    You need to understand why it is doing what it is doing. The first step for that is to take out the Boolean that I suggested and replace the close statement with a beep and a counter(maybe displayed in a label, or in the form's title?) in the picturebox_Click event. You can then find out how often and how many times the event is firing.
    With this information, you (we) can then investigate further.
    I know that my suggestion is like putting a plaster on a broken bone and I certainly would like to know the reason why. The last thread this topic came up on was also abandoned.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: [RESOLVED] timer and mousemove problems

    You are right, it's not resolved. But I just don't care enough to spend any more time on this problem.

    But I can see that you do. It would be easy for you to reproduce this problem and investigate it on your own. Just put a panel to fill a form, and put a picturebox to fill the panel. Then make the form TopMost = True. Then add a timer and pics to your project.

    Is this a Net 4.0 problem? If so, is there a way to report Net 4.0 problems?

    Code:
    Public Class Form1
        Dim FirstTime As Boolean = True
    
        
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            PictureBox1.Image = My.Resources._1
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Static ctr As Integer = 1
            Dim pics() As Image = {My.Resources._1, My.Resources._2, My.Resources._3, My.Resources._4, My.Resources._5, My.Resources._6}
    
            PictureBox1.Image = pics(ctr)
            ctr = ctr + 1
            If ctr > 5 Then
                ctr = 0
            End If
        End Sub
    
        Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
            Me.Close()
        End Sub
    
    
        'Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        '    If FirstTime = True Then
        '        FirstTime = False
        '    Else
        '        Me.Close()
        '    End If
        'End Sub
    End Class

  19. #19
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: [RESOLVED] timer and mousemove problems

    Maybe you have a hypersensitive mouse which responds to whiffs of cheese in the vicinity. One thing you might like to try is to check if the mouse has moved more than a certain distance, say 2 pixels, since the previous mouse move. Here's how you could code it the MouseMove event:
    Code:
    	Static lastPosition As Point = MousePosition
    	If Math.Abs(MousePosition.X - lastPosition.X) > 1 _
              OrElse Math.Abs(MousePosition.Y - lastPosition.Y) > 1 Then
    		Me.Close()
    	End If
    	lastPosition = MousePosition
    You probably won't need the FirstTime variable if you do that.

    BB

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: timer and mousemove problems

    I'd put a breakpoint on the line in the MouseMove event. I just recently found that altering the tooltip triggers a MouseMove event, even if the mouse isn't moving. I'm not sure what is causing that, but it strikes me as being somewhat similar to your changing the picturebox image, or something else of that sort. In short: There are things other than moving the mouse that can raise MouseMove events. You have probably found one. Putting a breakpoint in the MouseMove event, or changing the event to show some increasing count in a label rather than closing the form, may show you that you are getting LOTS of MouseMove events that you were not expecting. Doing something like what Boops suggested could solve that problem, and it is probably essential, though I think that the distance moved should be greater than 1. In fact, I'd suggest that it be greater than 10.
    My usual boring signature: Nothing

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: [RESOLVED] timer and mousemove problems

    Quote Originally Posted by boops boops View Post
    Maybe you have a hypersensitive mouse which responds to whiffs of cheese in the vicinity
    I've got the same probem on 2 different machines, using 2 different mice.

    Can I confirm if anyone else is experiencing this problem on their computer? Or is it just me? I can't tell from the replies if other people are having a problem with mousemove as well, making this a .NET problem(which should then be reported for fixing by Microsoft?). Thank you.

  22. #22
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: timer and mousemove problems

    I've had problems with MouseMove and Tooltips. Why would a Tooltip trigger a MouseMove event? It could have something to do with painting, or it could be something else. At the very least, there are things that trigger MouseMove events other than just moving the mouse.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width