OverPaint Checkbox Box with image ?

Kresha7

Member
Joined
Apr 29, 2011
Messages
6
Programming Experience
1-3
Hello everyone I'm trying to build a custom checkbox and I wanted to ask how could i overpaint the checkbox box with an image the image is made of a part that displays an unchecked box and another with the checked box so If someone could suggest me which way would be the best to start making the control i would be really thankful.

Thanks in advanced for any help and sorry for my bad english


P.s. I attach the checkbox image so you have an understanding what i want to do. checkbox.png
 
Well, I would start with just a User Control then use the mouse down / mouse up / click events to change the images. I wouldn't use a CheckBox control at all.

To make a User Control (in Visual Studio 2008), in the menu, under 'Project' select 'Add User Control...'. Name your control, then you have a blank control to work with. You will want to add some custom properties to the control for the CheckState and Checked properties. Here's an example of a Checked property:

VB.NET:
Private IsChecked As Boolean
Property Checked() As Boolean
    Get
        Return IsChecked
    End Get
    Set(ByVal value As Boolean)
        If value = True Then
            'check the checkbox
        Else
            'uncheck the checkbox
        End If
    End Set
End Property

I hope this helps you, let us know if you need anything else.
-Josh
 
well thanks Joshdbr but I'm already doing that. But i want to overwrite the checkbox so i don't have to use labels and stuff, and I want my control to inheritate all Checkbox Properties.
 
Sorry about that, didn't realize that you already had that part :)
To my knowledge there is no way to just overwrite the CheckBox checked/unchecked images. I think you will have to create a custom control.

-Josh

You can check into the CheckBox's Paint event though, you can overwrite the graphics there. But it overwrites ALL the graphics, including the text.
 
Thanks, I would appreciate it.
 
Hi,

Here's an image Checkbox I wrote ....

Public Class ImageCheckBox
Inherits WebControl
Implements ICheckBoxControl
Implements IButtonControl


Private handler As ImageCheckBoxClickEventHandler
Public Event CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements ICheckBoxControl.CheckedChanged


Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
Dim image = New Image()
image.ID = Me.ClientID & "_Image"
image.AlternateText = Me.Checked.ToString()
If Not Me.ShowCheckBox Then
MyBase.Style.Add("display", "none")
End If
If Me.Checked Then
image.ImageUrl = Me.CheckedStateImage
Else
image.ImageUrl = Me.UncheckedStateImage
End If
AddHandler Me.ImageCheckBoxClicked, AddressOf OnImageCheckBoxClick
image.RenderControl(output)
MyBase.Render(output)
End Sub


Public Custom Event ImageCheckBoxClicked As ImageCheckBoxClickEventHandler
AddHandler(ByVal value As ImageCheckBoxClickEventHandler)
handler = value
End AddHandler
RemoveHandler(ByVal value As ImageCheckBoxClickEventHandler)
handler = Nothing
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As ImageCheckBoxClickEventArgs)
handler.Invoke(Me, e)
End RaiseEvent
End Event


Public Sub OnImageCheckBoxClick(ByVal sender As Object, ByVal e As ImageCheckBoxClickEventArgs)
RaiseEvent ImageCheckBoxClicked(sender, e)
End Sub


Public Sub OnCheckChanged(ByVal e As EventArgs)
RaiseEvent CheckedChanged(Me, e)
End Sub


<Bindable(True), DefaultValue(0), Description("Specifies Image for the Checked Image"), Category("Appearance")> _
Public Property CheckedStateImage() As String
Get
Dim str As String = DirectCast(Me.ViewState("CheckedStateImage"), String)
If str Is Nothing Then
Return Navigator.ResolveStoreAdminLocation + "images/checked.png"
End If
Return str
End Get
Set(ByVal value As String)
Me.ViewState("CheckedStateImage") = value
End Set
End Property


<Category("Appearance"), Description("Specifies an Image for the UnChecked Image"), Bindable(True), DefaultValue(0)> _
Public Property UncheckedStateImage() As String
Get
Dim str As String = DirectCast(Me.ViewState("UncheckedStateImage"), String)
If str Is Nothing Then
Return Navigator.ResolveStoreAdminLocation + "images/unchecked.png"
End If
Return str
End Get
Set(ByVal value As String)
Me.ViewState("UncheckedStateImage") = value
End Set
End Property


<Bindable(True), Category("Appearance"), DefaultValue(0), Description("Show or hide the checkbox")> _
Public Property ShowCheckBox() As Boolean
Get
If Me.ViewState("ShowCheckBox") Is Nothing Then
Return False
End If
Return CBool(Me.ViewState("ShowCheckBox"))
End Get
Set(ByVal value As Boolean)
Me.ViewState("ShowCheckBox") = value
End Set
End Property


<Bindable(True), Category("Appearance"), DefaultValue(False), Description("Returns the checked state of the control")> _
Public Property Checked As Boolean Implements ICheckBoxControl.Checked
Get
Dim flag As Boolean = CBool(Me.ViewState("Checked"))
If flag.Equals(Nothing) Then
Return False
End If
Return flag
End Get
Set(ByVal value As Boolean)
Me.ViewState("Checked") = value
End Set
End Property


Public Property CausesValidation As Boolean Implements IButtonControl.CausesValidation
Get
Return False
End Get
Set(ByVal value As Boolean)
'MyBase.CausesValidation = value
End Set
End Property


Public Event Click(ByVal sender As Object, ByVal e As System.EventArgs) Implements IButtonControl.Click
Public Event Command(ByVal sender As Object, ByVal e As CommandEventArgs) Implements IButtonControl.Command


Public Sub OnCommand(ByVal sender As Object, ByVal e As CommandEventArgs)
RaiseEvent Command(sender, e)
End Sub


Public Sub OnClick(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim arg = New ImageCheckBoxClickEventArgs(If(Me.Checked, False, True))
OnImageCheckBoxClick(Me, arg)
Me.Checked = arg.Checked
RaiseEvent Click(sender, e)
End Sub


Public Property CommandArgument As String Implements IButtonControl.CommandArgument
Get
Return CStr(Me.ViewState("CommandArgument"))
End Get
Set(ByVal value As String)
Me.ViewState("CommandArgument") = value
End Set
End Property


Public Property CommandName As String Implements IButtonControl.CommandName
Get
Return CStr(Me.ViewState("CommandName"))
End Get
Set(ByVal value As String)
Me.ViewState("CommandName") = value
End Set
End Property


Public Property PostBackUrl As String Implements IButtonControl.PostBackUrl
Get
Return CStr(Me.ViewState("PostBackUrl"))
End Get
Set(ByVal value As String)
Me.ViewState("PostBackUrl") = value
End Set
End Property


Public Property Text As String Implements IButtonControl.Text
Get
Return CStr(Me.ViewState("Text"))
End Get
Set(ByVal value As String)
Me.ViewState("Text") = value
End Set
End Property


Public Property ValidationGroup As String Implements IButtonControl.ValidationGroup
Get
Return CStr(Me.ViewState("ValidationGroup"))
End Get
Set(ByVal value As String)
Me.ViewState("ValidationGroup") = value
End Set
End Property
End Class


Here's the Handler Class

Public Delegate Sub ImageCheckBoxClickEventHandler(ByVal sender As Object, ByVal e As ImageCheckBoxClickEventArgs)


Public Class ImageCheckBoxClickEventArgs
Inherits System.EventArgs


Public Sub New(ByVal Checked As Boolean)
Me.Checked = Checked
End Sub


Private _checked As Boolean
Public Property Checked As Boolean
Get
Return _checked
End Get
Set(ByVal value As Boolean)
_checked = value
End Set
End Property
End Class


Hope this helps!!
 
Back
Top