Advertisement

10.18.2008 at 08:17PM PDT, ID: 23827270
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.6

Owner-Drawn Colored ListBox

Asked by AhmedHindy in C# Programming Language, Microsoft Visual Basic.Net, Program Quality Control

Tags:

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

thxStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
<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
[+][-]10.19.2008 at 09:46AM PDT, ID: 22752776

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, Microsoft Visual Basic.Net, Program Quality Control
Tags: VB.NET
Sign Up Now!
Solution Provided By: emoreau
Participating Experts: 1
Solution Grade: A
 
 
[+][-]11.17.2008 at 03:07AM PST, ID: 22974507

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]11.22.2008 at 06:42AM PST, ID: 23020894

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628