Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > VB.NET > Windows Forms

Windows Forms Discussion related to Winforms application development

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-29-2008, 11:43 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Aug 2008
Age: 38
Posts: 3
Reputation: 0
kavithavr is on a distinguished programming path ahead
Arrow checked list box

how to change the bkcolor of checked list box checked item only in vb.net?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-30-2008, 8:35 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,322
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

You have to inherit CheckedListBox and override OnDrawItem where you custom paint the items. You use this control in form instead of the default CheckedListBox. Here is an example:
Code:
Imports System.ComponentModel
Public Class CustomCheckedListBox
    Inherits CheckedListBox

    Private _CheckedBackColor As Color = Color.Yellow
    <Category("Appearance"), Description("The background color of checked items.")> _
    Public Property CheckedBackColor() As Color
        Get
            Return _CheckedBackColor
        End Get
        Set(ByVal value As Color)
            _CheckedBackColor = value
        End Set
    End Property

    Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
       
        Dim checked As Boolean = Me.CheckedIndices.Contains(e.Index)

        Dim textrectangle As Rectangle = e.Bounds
        textrectangle.X += textrectangle.Height
        textrectangle.Width -= textrectangle.Height + 1

        'back color
        If checked Then
            Using sb As New SolidBrush(Me._CheckedBackColor)
                e.Graphics.FillRectangle(sb, textrectangle)
            End Using
        Else
            Using sb As New SolidBrush(Me.BackColor)
                e.Graphics.FillRectangle(sb, textrectangle)
            End Using
        End If

        'checkbox            
        Dim boxlocation As Point = e.Bounds.Location
        boxlocation.Offset(1, 1)

        Dim checkstate As VisualStyles.CheckBoxState = VisualStyles.CheckBoxState.UncheckedNormal
        If checked Then
            checkstate = VisualStyles.CheckBoxState.CheckedNormal
        End If

        CheckBoxRenderer.DrawCheckBox(e.Graphics, boxlocation, checkstate)

        'text    
        Using sb As New SolidBrush(Me.ForeColor)
            Dim text As String = Me.Name
            If Me.Items.Count > 0 Then
                text = Me.GetItemText(Me.Items(e.Index))
            End If
            Dim format As New StringFormat
            format.LineAlignment = StringAlignment.Center
            textrectangle.Inflate(-3, 0)
            e.Graphics.DrawString(text, Me.Font, sb, textrectangle, format)
        End Using

        'focus rectangle
        Dim focused As Boolean = (e.State And DrawItemState.Selected) = DrawItemState.Selected
        If focused Then
            Using p As New Pen(Color.Black)
                p.DashStyle = Drawing2D.DashStyle.Dot                
                textrectangle.Inflate(2, -1)
                e.Graphics.DrawRectangle(p, textrectangle)
            End Using
        End If
    End Sub
End Class
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-10-2008, 12:45 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Aug 2008
Age: 38
Posts: 3
Reputation: 0
kavithavr is on a distinguished programming path ahead
Default

Quote:
Originally Posted by kavithavr View Post
how to change the bkcolor of checked list box checked item only in vb.net?
Thank you for the reply
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 4:14 AM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.