Need a double click event for a list box? After searching around i couldn't find any decent vb.net methods so i decided to write one.
As always if you want to skip all the mumbo jumbo you can download the sample project and control here. CustomControlListBox.zip (54.01 kb)
So how does the control work? When the control is rendered it adds a couple of attributes to the HTML listbox for click and ondblclick events.
writer.AddAttribute("ondblclick",
Page.ClientScript.GetPostBackEventReference(Me,
"dblclick"))
By implementing the IpostbackEventHandler for the class we're able to intercept the postback handler and look for the event argument, click and dblclick.
Private Sub RaisePostBackEvent(ByVal
eventArgument As String)
Implements
IPostBackEventHandler.RaisePostBackEvent
If
eventArgument = "click" Then
OnClick(EventArgs.Empty)
End If
If
eventArgument = "dblclick" Then
OnDblClick(EventArgs.Empty)
End If
End Sub
To get the double click even to work you'll need to ensure the autopostback is set to true and the ProcessDbouleClick is true also, you can always use the click event too. Yes the listbox has a selected index changed event, but what if the user clicks on the same list item? that event never get's fired so i added support for a click event also.
How the control shows up on your toolbox menu.
Complete Code Listing:
Imports System
Imports System.Web
Imports
System.Web.UI
Imports
System.Web.UI.WebControls
Imports
System.Text
Imports
System.ComponentModel
<ToolboxData("<{0}:SimmonsListBox ProcessClick=False
ProcessDoubleClick=False runat=server></{0}:SimmonsListBox>")>
_
Public Class SimmonsListBox
Inherits
System.Web.UI.WebControls.ListBox
Implements
IPostBackEventHandler
#Region "Properties"
<DefaultValue(False)>
_
Public Property ProcessClick() As
Boolean
Get
Dim
o As Object =
ViewState("ProcessClick")
Return
IIf((o Is Nothing),
False, CBool(o))
End Get
Set(ByVal value As Boolean)
ViewState("ProcessClick")
= value
If
value = True Then
ProcessDoubleClick = False
End
If
End Set
End Property
<DefaultValue(False)>
_
Public Property ProcessDoubleClick() As Boolean
Get
Dim
o As Object =
ViewState("ProcessDoubleClick")
Return
IIf((o Is Nothing),
False, CBool(o))
End Get
Set(ByVal value As Boolean)
ViewState("ProcessDoubleClick")
= value
If
value = True Then
ProcessClick = False
End
If
End Set
End Property
#End Region
#Region "Event
Handlers"
Dim
ClickEventKey As Object
= New Object
Dim
DblClickEventKey As Object
= New Object
Public Event Click As
EventHandler
Public Event DblClick As
EventHandler
Protected Overridable Sub
OnClick(ByVal e As
EventArgs)
RaiseEvent
Click(Me, e)
End Sub
Protected Overridable Sub
OnDblClick(ByVal e As
EventArgs)
RaiseEvent
DblClick(Me, e)
End Sub
#End Region
Private Sub RaisePostBackEvent(ByVal
eventArgument As String)
Implements
IPostBackEventHandler.RaisePostBackEvent
If
eventArgument = "click" Then
OnClick(EventArgs.Empty)
End If
If
eventArgument = "dblclick" Then
OnDblClick(EventArgs.Empty)
End If
End Sub
Protected Overloads Overrides Sub AddAttributesToRender(ByVal
writer As HtmlTextWriter)
MyBase.AddAttributesToRender(writer)
If
ProcessClick Then
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
Page.ClientScript.GetPostBackEventReference(Me,
"click"))
End If
If
ProcessDoubleClick Then
writer.AddAttribute("ondblclick",
Page.ClientScript.GetPostBackEventReference(Me,
"dblclick"))
End If
MyBase.AddAttributesToRender(writer)
End Sub
Protected Overloads Overrides Sub RaisePostDataChangedEvent()
MyBase.RaisePostDataChangedEvent()
End Sub
End Class