Custom List Box Control with DblClick for Click and Double Click Events

clock August 11, 2008 13:05 by author administrator

kick it on DotNetKicks.com

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

 

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Creating a Custom Control Extending the power of Panel

clock August 4, 2008 17:57 by author administrator

In this article I will explain how to quickly and easily extend the power of the panel to create a nice looking container which you will soon be able to view at my Job Site

kick it on DotNetKicks.com

 

If you want to skip all the mumbo jumbo and download the example project then go here: CustomControl.zip (56.06 kb)

Now what's the result? What do you get out of developing a custom panel control? First you get to drag and drop reusable and modifiable UI Container elements. Now admittadly i could have really enabled support for multiple themes even true theme support but hey i'm just throwing this out there maybe you can extend it and send it back.

The kewl thing about a custom control is that it enables child controls ad design time a panel is especially useful as a custom control because we can put controls in it at design time.

 

This picture shows you how the control renders

This picture shows you  how it looks at design time.

 

 

 

 

Here is the main part of the code. On thing to keep in mind when developing custom controls is if you want to theme a custom control (not really done here) you need to add a registery assebler to the theme skin file.

 

<%@ Register Assembly="Simmons.Controls" Namespace="Simmons.Controls.CustomControls" TagPrefix="cc1" %>

 

 

This is the main part of the code.

 

 

Imports System

Imports System.Drawing

Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Text

Imports System.Web.UI

Imports System.Web.UI.HtmlControls

Imports System.Web.UI.WebControls

 

 

Namespace CustomControls

    <ToolboxData("<{0}:Container runat='server' HeaderText='Heading' HeaderWidth='300' ></{0}:RoundedContainer>")> _

    Public Class Container

        Inherits Panel

        Private _headerText As String

        Private _headerwidth As String

 

        Public Property HeaderText() As String

            Get

                Return _headerText

            End Get

            Set(ByVal value As String)

                _headerText = value

            End Set

        End Property

 

 

        <Category("HeaderStyle"), PersistenceMode(PersistenceMode.InnerProperty), Themeable(True)> _

        Public Property HeaderWidth() As String

            Get

                Return _headerwidth

            End Get

            Set(ByVal value As String)

                _headerwidth = value

            End Set

        End Property

 

 

        Protected Overloads Overrides Sub RenderChildren(ByVal writer As HtmlTextWriter)

 

            Dim strContainerWidth As String

            Dim strHeaderWidth As String

 

            strContainerWidth = _headerwidth + "px"

            strHeaderWidth = Convert.ToString(Convert.ToInt16(_headerwidth) - 22) + "px"

 

            ' FUll container

            writer.AddAttribute(HtmlTextWriterAttribute.[Class], "ContainerFull")

            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, strContainerWidth)

            writer.RenderBeginTag(HtmlTextWriterTag.Div)

 

            'header left

            writer.AddAttribute(HtmlTextWriterAttribute.[Class], "ContainerHeadLeft")

            writer.RenderBeginTag(HtmlTextWriterTag.Div)

            'header left

            writer.RenderEndTag()

 

            'header right

            writer.AddAttribute(HtmlTextWriterAttribute.[Class], "ContainerHeadRight")

            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, strHeaderWidth)

            writer.RenderBeginTag(HtmlTextWriterTag.Div)

            writer.AddAttribute(HtmlTextWriterAttribute.[Class], "ContainerHeadText")

            writer.RenderBeginTag(HtmlTextWriterTag.Div)

            writer.Write(_headerText)

            writer.RenderEndTag()

            'header right

            writer.RenderEndTag()

 

            ' Write Visual Contents

            writer.AddAttribute(HtmlTextWriterAttribute.[Class], "ContainerChildren")

 

            writer.RenderBeginTag(HtmlTextWriterTag.Div)

 

            writer.AddStyleAttribute(HtmlTextWriterStyle.Visibility, "hidden")

            writer.RenderBeginTag(HtmlTextWriterTag.Div)

            writer.Write("")

            writer.RenderEndTag()

 

            MyBase.RenderChildren(writer)

            writer.RenderEndTag()

 

            ' full container end

            writer.RenderEndTag()

 

        End Sub

        ' RenderChildren

 

 

    End Class

    ' class

End Namespace

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5




Sign in