Visual Basic .NET Forums    

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

VB.NET Forums Newsletter Signup:
Email address:


Windows Forms Discussion related to Winforms application development

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-30-2008, 12:43 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Aug 2008
Age: 37
Posts: 3
Blog Entries: 1
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?
Reply With Quote
  #2 (permalink)  
Old 08-30-2008, 9:35 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 36
Posts: 7,894
Reputation: 819
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
__________________
See this thread about how to use forum markup codes for code blocks etc (present the problem/post properly )
Some useful links: Learning videoes, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ


DR. WEIR: Download it to a non-networked, firewalled computer.
TECHNICIAN: Yes, ma'am.
Reply With Quote
  #3 (permalink)  
Old 09-10-2008, 1:45 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Aug 2008
Age: 37
Posts: 3
Blog Entries: 1
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
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 3:16 PM.




Click to advertise here

Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
For advertising opportunities click here.