Solved

Owner-Drawn Colored ListBox

Posted on 2008-10-18
3
656 Views
Last Modified: 2013-11-25
I'm trying to develop an Owner-Drawn ListBox and here is what i did :

I'm trying to apply a new property called "AlternatingItemsCellStyle", like the one in the DataGridView Control , but i have some problems implementing it.
I have some classes like "ListBoxCellStyle" which have some properties like -- forecolor, backcolor, selectionforecolor, selectionbackcolor and font property.
i also implement a type converter for that cellstyle and a custom item to be added to the listbox
but that code doesn't work when i added some items to the list nothing changed.
I attached a test form to test that list but , every time i'm trying to run the form i get a strange error messagsays 'Assembly 'C:\Documents and Settings\~~~\Desktop\~~~\~~~\obj\Debug\~~~~.dll' doesn't contain any UserControl types.'

NEED Help

thx
<ToolboxBitmap(GetType(ListBox))> _

<DefaultProperty("AlternatingItemsCellStyle")> _

<DefaultEvent("SelectedIndexChanged")> _

Public Class ColoredListBox

    Inherits ListBox
 

    Private _DrawMode As DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable

    <DefaultValue(GetType(DrawMode), "OwnerDrawVariable")> _

    Public Overloads Property DrawMode() As DrawMode

        Get

            Return _DrawMode

        End Get

        Set(ByVal Value As DrawMode)

            _DrawMode = Value

        End Set

    End Property
 

    Private _AlternatingItemsCellStyle As ListBoxCellStyle = New ListBoxCellStyle

    '<DefaultValue(GetType(ListBoxCellStyle), "(White), (Black), (Maroon), (255, 224, 192), (Tahoma, 8pt)")> _

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _

    Public Property AlternatingItemsCellStyle() As ListBoxCellStyle

        Get

            Return _AlternatingItemsCellStyle

        End Get

        Set(ByVal Value As ListBoxCellStyle)

            _AlternatingItemsCellStyle = Value

            AddHandler _AlternatingItemsCellStyle.AlternatingCellStyleChanged, AddressOf AlternatingCellStyleChanged

        End Set

    End Property
 

    Private Sub AlternatingCellStyleChanged(ByVal sender As Object, ByVal e As EventArgs)

        Invalidate()

    End Sub
 

    Private Sub ColoredListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem

        Dim List As ListBox = CType(sender, ListBox)
 

        Dim Item As FormattedListItem = Nothing
 

        If TypeOf List.Items(e.Index) Is FormattedListItem Then

            Item = CType(List.Items(e.Index), FormattedListItem)

        End If
 

        Dim Font As Font = Nothing

        Dim ForeColor As Color = Color.Empty

        Dim BackColor As Color = Color.Empty

        Dim SelectionForeColor As Color = Color.Empty

        Dim SelectionBackColor As Color = Color.Empty
 

        If Item IsNot Nothing Then

            Font = Item.ListBoxItemsStyle.Font

            ForeColor = Item.ListBoxItemsStyle.ForeColor

            BackColor = Item.ListBoxItemsStyle.BackColor

            SelectionForeColor = Item.ListBoxItemsStyle.SelectionForeColor

            SelectionBackColor = Item.ListBoxItemsStyle.SelectionBackColor

        Else

            Font = List.Font

            ForeColor = List.ForeColor

            BackColor = List.BackColor

            SelectionForeColor = System.Drawing.SystemColors.HighlightText

            SelectionBackColor = System.Drawing.SystemColors.Highlight

        End If
 

        Dim ForeBrush As Brush

        Dim BackBrush As Brush
 

        If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then

            ForeBrush = New SolidBrush(SelectionForeColor)

            BackBrush = New SolidBrush(SelectionBackColor)

        Else

            ForeBrush = New SolidBrush(ForeColor)

            BackBrush = New SolidBrush(BackColor)

        End If
 

        e.Graphics.FillRectangle(BackBrush, e.Bounds)
 

        Dim Text As String = List.Items(e.Index).ToString
 

        e.Graphics.DrawString(Text, Font, ForeBrush, e.Bounds.X, e.Bounds.Y)

    End Sub
 

    Private Sub ColoredListBox_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem

        Dim List As ListBox = CType(sender, ListBox)
 

        If (IsPrimeNumber(e.Index)) Then

            Dim MyItem As New FormattedListItem(List.Items(e.Index), Me.AlternatingItemsCellStyle)
 

            If MyItem.ListBoxItemsStyle.Font IsNot Nothing Then

                List.Items.RemoveAt(e.Index)

                List.Items.Insert(e.Index, MyItem)

                e.ItemHeight = MyItem.ListBoxItemsStyle.Font.Height

                Return

            End If

        End If

    End Sub
 

    Private Function IsPrimeNumber(ByVal [Number] As Integer) As Boolean

        Dim TheDouble As Double
 

        TheDouble = Double.Parse(([Number] / 2))

        If TheDouble.ToString.Contains(".") Then

            Return True

        End If

        Return False

    End Function

End Class

Open in new window

0
Comment
Question by:AhmedHindy
3 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 22752776
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It was Monday morning and while heading to work those familiar feelings of frustration began to rise: How was I ever going to get my yard work done?! At the end of every weekend I discovered that I spent more time trying to decide what to work on th…
I worked at a US software company that used offshore contractors for ten years and offshore employees for three years. We had a positive experience and you can too.   When I interviewed people for positions in the US, I would tell them that we wor…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now