PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
<renamed> Click and Hold Control-VBForums
Results 1 to 3 of 3

Thread: <renamed> Click and Hold Control

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,711

    <renamed> Click and Hold Control

    I put this together because I have a constant need to click a button and repeat its action of it has been held down for a while....

    my share to you....Happy Thanks Merry Valentines Day

    cut and paste into a new project...
    vb Code:
    1. Public Class Form1
    2.  
    3.     Private WithEvents MouseDownTimer As New Timer
    4.     Private WithEvents HoldActiontimer As New Timer
    5.  
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.  
    9.  
    10.         AddHandler MouseDownTimer.Tick, AddressOf MouseDownTimer_Tick
    11.         AddHandler HoldActiontimer.Tick, AddressOf HoldActiontimer_Tick
    12.  
    13.  
    14.         'set the MouseDownTimer.Interval for the time until the action BEGINS to occur
    15.         'set the HoldActionTimer.Interval for the time between actions that occur AFTER the hold peroid expires
    16.         Me.MouseDownTimer.Interval = 1000
    17.         Me.HoldActionTimer.Interval = 100
    18.  
    19.     End Sub
    20.  
    21.     Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
    22.         Me.MouseDownTimer.Start()
    23.     End Sub
    24.  
    25.     Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
    26.         'the mouse is up... stop all of the timers
    27.         Me.MouseDownTimer.Stop()
    28.         Me.HoldActionTimer.Stop()
    29.     End Sub
    30.  
    31.     Private Sub MouseDownTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    32.         Me.HoldActiontimer.Start()
    33.     End Sub
    34.  
    35.     Private Sub HoldActiontimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    36.         Call MouseHoldAction()
    37.     End Sub
    38.  
    39.     Private Sub MouseHoldAction()
    40.         Static i As Integer
    41.         i += 1
    42.         Me.Label1.Text = i.ToString
    43.     End Sub
    44.  
    45.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    46.  
    47.     End Sub
    48. End Class
    Last edited by kebo; Dec 5th, 2008 at 02:32 PM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Click and Hold Event

    Nice and simple code.

    Why not turn it into a UserControl?

    I took the liberty to do that for you, hope you don't mind!

    Put this in a new Class called 'RepeatButton':
    vb.net Code:
    1. Imports System.ComponentModel
    2.  
    3. <DefaultEvent("HoldAction")> _
    4. Public Class RepeatButton
    5.     Inherits Button
    6.  
    7.     Private WithEvents MouseDownTimer As New Timer
    8.     Private WithEvents HoldActionTimer As New Timer
    9.  
    10.     Public Event HoldAction(ByVal sender As Object, ByVal e As EventArgs)
    11.  
    12.     Public Sub New()
    13.         MyBase.New()
    14.  
    15.         AddHandler MouseDownTimer.Tick, AddressOf MouseDownTimer_Tick
    16.         AddHandler HoldActionTimer.Tick, AddressOf HoldActiontimer_Tick
    17.  
    18.         Me.MouseDownTimer.Interval = 1000
    19.         Me.HoldActionTimer.Interval = 100
    20.     End Sub
    21.  
    22.     Protected Overrides Sub OnMouseDown(ByVal mevent As System.Windows.Forms.MouseEventArgs)
    23.         MyBase.OnMouseDown(mevent)
    24.         Me.MouseDownTimer.Start()
    25.     End Sub
    26.  
    27.     Protected Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs)
    28.         MyBase.OnMouseUp(mevent)
    29.         Me.MouseDownTimer.Stop()
    30.         Me.HoldActionTimer.Stop()
    31.     End Sub
    32.  
    33.     Private Sub MouseDownTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    34.         Me.HoldActionTimer.Start()
    35.     End Sub
    36.  
    37.     Private Sub HoldActiontimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    38.         RaiseEvent HoldAction(sender, e)
    39.     End Sub
    40.  
    41. End Class

    All I changed was the fact that your code calls a method in the form when it is 'repeated', while my UserControl fires the "HoldAction" event.

  3. #3

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,711

    Re: Click and Hold Event

    I don't mind at all....it actually makes more sense for it to be a control, and we might as well and a couple of properties for the timer intervals....


    vb Code:
    1. Imports System.ComponentModel
    2.  
    3. <DefaultEvent("HoldAction")> _
    4. Public Class RepeatButton
    5.  
    6.     Inherits Button
    7.  
    8.     Private WithEvents MouseDownTimer As New Timer
    9.     Private WithEvents HoldActionTimer As New Timer
    10.     Public Event HoldAction(ByVal sender As Object, ByVal e As EventArgs)
    11.  
    12.     Public Sub New()
    13.         MyBase.New()
    14.  
    15.         AddHandler MouseDownTimer.Tick, AddressOf MouseDownTimer_Tick
    16.         AddHandler HoldActionTimer.Tick, AddressOf HoldActiontimer_Tick
    17.  
    18.         Me.MouseDownTimer.Interval = 1000
    19.         Me.HoldActionTimer.Interval = 100
    20.     End Sub
    21.  
    22.     Public Property MouseDownTimerInterval() As Integer
    23.         Get
    24.             Return Me.MouseDownTimer.Interval
    25.         End Get
    26.         Set(ByVal value As Integer)
    27.             Me.MouseDownTimer.Interval = value
    28.         End Set
    29.     End Property
    30.  
    31.     Public Property HoldActionTimerInterval() As Integer
    32.         Get
    33.             Return Me.HoldActionTimer.Interval
    34.         End Get
    35.         Set(ByVal value As Integer)
    36.             Me.HoldActionTimer.Interval = value
    37.         End Set
    38.     End Property
    39.  
    40.  
    41.     Protected Overrides Sub OnMouseDown(ByVal mevent As System.Windows.Forms.MouseEventArgs)
    42.         MyBase.OnMouseDown(mevent)
    43.         Me.MouseDownTimer.Start()
    44.     End Sub
    45.  
    46.     Protected Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs)
    47.         MyBase.OnMouseUp(mevent)
    48.         Me.MouseDownTimer.Stop()
    49.         Me.HoldActionTimer.Stop()
    50.     End Sub
    51.  
    52.     Private Sub MouseDownTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    53.         Me.HoldActionTimer.Start()
    54.     End Sub
    55.  
    56.     Private Sub HoldActiontimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    57.         RaiseEvent HoldAction(sender, e)
    58.     End Sub
    59.  
    60. End Class

    nice job Mr. Nick
    Last edited by kebo; Dec 4th, 2008 at 07:59 PM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width