Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

do while button is pressed down

jedi06jedi06 Posts: 38Member
I need to implement a pan function and I need to have the function run as long as the button is down. I have been searching this for some time now and have not found any good solutions. Does anyone know of a good way to do it?

Comments

  • jedi06jedi06 Posts: 38Member
    I'm thinking maybe Doevents or using a timer. I am not familiar with either.


    : I need to implement a pan function and I need to have the function run as long as the button is down. I have been searching this for some time now and have not found any good solutions. Does anyone know of a good way to do it?
    :

  • PavlinIIPavlinII Posts: 404Member
    : I'm thinking maybe Doevents or using a timer. I am not familiar with either.
    :
    :
    : : I need to implement a pan function and I need to have the function run as long as the button is down. I have been searching this for some time now and have not found any good solutions. Does anyone know of a good way to do it?
    : :
    :
    :
    Hi,
    there're several ways how to achieve this..

    Use another thread, for example
    [code]Private KeptDown As Boolean
    Dim TDown As Thread

    Private Sub Button5_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button5.MouseDown
    KeptDown = True
    TDown = New Thread(AddressOf GoDown)
    TDown.Start()
    End Sub

    Private Sub Button5_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button5.MouseUp
    KeptDown = False
    [code]'Uncomment this, if you want to wait for that loop to finish and continue here after it finishes.
    'TDown.Join()[/green]
    End Sub

    Private Sub GoDown()
    Dim i As Long
    While KeptDown
    i += 1
    Threading.Thread.Sleep(100)
    End While
    MsgBox(i)
    End Sub[/code]
    If you need to pass some parameters into GoDown, you can use asynchronous method call like this (let's say you have one argument called Arg and you need to pass value of 100)
    [code] Private KeptDown As Boolean
    Private Delegate Sub DownDelegate(ByVal Arg As Integer)

    Private Sub Button5_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button5.MouseDown
    KeptDown = True
    Dim d As DownDelegate = AddressOf GoDown
    d.BeginInvoke(100, Nothing, Nothing)
    End Sub

    Private Sub Button5_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button5.MouseUp
    KeptDown = False
    End Sub

    Private Sub GoDown(ByVal Arg As Integer)
    Dim i As Long = Arg
    While KeptDown
    i += 1
    Threading.Thread.Sleep(100)
    End While
    MsgBox(i)
    End Sub[/code]

    Hope this helps

    [blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]

    [purple]Don't take life too seriously anyway you won't escape alive from it![/purple]


Sign In or Register to comment.