N
Neil Wallace
Hi,
This is an odd one. I've been struggling to get "double click" to work well
for my controls.
The same event handler works perfectly for buttons, but not for labels.
Can anyone tell me why not?
Below is an working VB.Net example to illustrate what I mean.
regards
Neil.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Label1 = New System.Windows.Forms.Label
Me.Button1 = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'Label1
'
Me.Label1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte),
CType(128, Byte), CType(128, Byte))
Me.Label1.Location = New System.Drawing.Point(192, 104)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(80, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Label"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Button1
'
Me.Button1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte),
CType(128, Byte), CType(128, Byte))
Me.Button1.Location = New System.Drawing.Point(24, 104)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button"
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
Me.Label2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label2.Location = New System.Drawing.Point(40, 24)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(216, 48)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Double Click Successful"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.Label2.Visible = False
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(40, 152)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(208, 80)
Me.Label3.TabIndex = 3
Me.Label3.Text = "Try double clicking on the button and the label. Both have
the same event handle" & _
"r. However, the label doesn't behave as anticipated and for some reason
requires" & _
" a ""triple click""."
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Timer1
'
Me.Timer1.Interval = 200
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim lastclicktime As DateTime
Dim lastclicked As String
Dim waittwoticks As Integer
Private Sub Double_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'the equals checks if the sender is the same as the last clicked control
i.e. "button" or "label"
'also check if two events on the same control have occurred within the
allowed time period.
If Equals(lastclicked, sender.text) And
Now.Subtract(lastclicktime).TotalMilliseconds <
Windows.Forms.SystemInformation.DoubleClickTime Then
'if they have then show the "double click" label
Label2.Visible = True
Else : Label2.Visible = False
End If
lastclicked = sender.text
lastclicktime = Now
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler Label1.Click, AddressOf Me.Double_Click
AddHandler Button1.Click, AddressOf Me.Double_Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
If Label2.Visible = True Then
If waittwoticks = 3 Then
Label2.Visible = False
Else : waittwoticks += 1
End If
Else : waittwoticks = 0
End If
End Sub
End Class
This is an odd one. I've been struggling to get "double click" to work well
for my controls.
The same event handler works perfectly for buttons, but not for labels.
Can anyone tell me why not?
Below is an working VB.Net example to illustrate what I mean.
regards
Neil.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Label1 = New System.Windows.Forms.Label
Me.Button1 = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'Label1
'
Me.Label1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte),
CType(128, Byte), CType(128, Byte))
Me.Label1.Location = New System.Drawing.Point(192, 104)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(80, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Label"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Button1
'
Me.Button1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte),
CType(128, Byte), CType(128, Byte))
Me.Button1.Location = New System.Drawing.Point(24, 104)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button"
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
Me.Label2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label2.Location = New System.Drawing.Point(40, 24)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(216, 48)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Double Click Successful"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.Label2.Visible = False
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(40, 152)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(208, 80)
Me.Label3.TabIndex = 3
Me.Label3.Text = "Try double clicking on the button and the label. Both have
the same event handle" & _
"r. However, the label doesn't behave as anticipated and for some reason
requires" & _
" a ""triple click""."
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Timer1
'
Me.Timer1.Interval = 200
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim lastclicktime As DateTime
Dim lastclicked As String
Dim waittwoticks As Integer
Private Sub Double_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'the equals checks if the sender is the same as the last clicked control
i.e. "button" or "label"
'also check if two events on the same control have occurred within the
allowed time period.
If Equals(lastclicked, sender.text) And
Now.Subtract(lastclicktime).TotalMilliseconds <
Windows.Forms.SystemInformation.DoubleClickTime Then
'if they have then show the "double click" label
Label2.Visible = True
Else : Label2.Visible = False
End If
lastclicked = sender.text
lastclicktime = Now
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler Label1.Click, AddressOf Me.Double_Click
AddHandler Button1.Click, AddressOf Me.Double_Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
If Label2.Visible = True Then
If waittwoticks = 3 Then
Label2.Visible = False
Else : waittwoticks += 1
End If
Else : waittwoticks = 0
End If
End Sub
End Class