|
-
Nov 28th, 2008, 11:55 PM
#1
<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:
Public Class Form1
Private WithEvents MouseDownTimer As New Timer
Private WithEvents HoldActiontimer As New Timer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler MouseDownTimer.Tick, AddressOf MouseDownTimer_Tick
AddHandler HoldActiontimer.Tick, AddressOf HoldActiontimer_Tick
'set the MouseDownTimer.Interval for the time until the action BEGINS to occur
'set the HoldActionTimer.Interval for the time between actions that occur AFTER the hold peroid expires
Me.MouseDownTimer.Interval = 1000
Me.HoldActionTimer.Interval = 100
End Sub
Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
Me.MouseDownTimer.Start()
End Sub
Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
'the mouse is up... stop all of the timers
Me.MouseDownTimer.Stop()
Me.HoldActionTimer.Stop()
End Sub
Private Sub MouseDownTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.HoldActiontimer.Start()
End Sub
Private Sub HoldActiontimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call MouseHoldAction()
End Sub
Private Sub MouseHoldAction()
Static i As Integer
i += 1
Me.Label1.Text = i.ToString
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
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
-
Dec 3rd, 2008, 05:06 PM
#2
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:
Imports System.ComponentModel <DefaultEvent("HoldAction")> _ Public Class RepeatButton Inherits Button Private WithEvents MouseDownTimer As New Timer Private WithEvents HoldActionTimer As New Timer Public Event HoldAction(ByVal sender As Object, ByVal e As EventArgs) Public Sub New() MyBase.New() AddHandler MouseDownTimer.Tick, AddressOf MouseDownTimer_Tick AddHandler HoldActionTimer.Tick, AddressOf HoldActiontimer_Tick Me.MouseDownTimer.Interval = 1000 Me.HoldActionTimer.Interval = 100 End Sub Protected Overrides Sub OnMouseDown(ByVal mevent As System.Windows.Forms.MouseEventArgs) MyBase.OnMouseDown(mevent) Me.MouseDownTimer.Start() End Sub Protected Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs) MyBase.OnMouseUp(mevent) Me.MouseDownTimer.Stop() Me.HoldActionTimer.Stop() End Sub Private Sub MouseDownTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.HoldActionTimer.Start() End Sub Private Sub HoldActiontimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) RaiseEvent HoldAction(sender, e) End Sub 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.
-
Dec 4th, 2008, 07:44 PM
#3
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:
Imports System.ComponentModel
<DefaultEvent("HoldAction")> _
Public Class RepeatButton
Inherits Button
Private WithEvents MouseDownTimer As New Timer
Private WithEvents HoldActionTimer As New Timer
Public Event HoldAction(ByVal sender As Object, ByVal e As EventArgs)
Public Sub New()
MyBase.New()
AddHandler MouseDownTimer.Tick, AddressOf MouseDownTimer_Tick
AddHandler HoldActionTimer.Tick, AddressOf HoldActiontimer_Tick
Me.MouseDownTimer.Interval = 1000
Me.HoldActionTimer.Interval = 100
End Sub
Public Property MouseDownTimerInterval() As Integer
Get
Return Me.MouseDownTimer.Interval
End Get
Set(ByVal value As Integer)
Me.MouseDownTimer.Interval = value
End Set
End Property
Public Property HoldActionTimerInterval() As Integer
Get
Return Me.HoldActionTimer.Interval
End Get
Set(ByVal value As Integer)
Me.HoldActionTimer.Interval = value
End Set
End Property
Protected Overrides Sub OnMouseDown(ByVal mevent As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(mevent)
Me.MouseDownTimer.Start()
End Sub
Protected Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(mevent)
Me.MouseDownTimer.Stop()
Me.HoldActionTimer.Stop()
End Sub
Private Sub MouseDownTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.HoldActionTimer.Start()
End Sub
Private Sub HoldActiontimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
RaiseEvent HoldAction(sender, e)
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|