Connecting Tech Pros Worldwide Forums | Help | Site Map

How to add properties to the Item object of a combobox?

Newbie
 
Join Date: Jul 2009
Posts: 4
#1: Jul 31 '09
I would like to add the properties "visible" and "enabled" to the Items object of a combobox in VB .Net so that I can hide or disable individual line items of the combobox. I have created the class "CustomListItem" that inherits the combobox.objectcollection. I have also created the class "CustomComboBox". Now, I would like to override the custombox.Items object with my custom one. Is this possible to do?

Newbie
 
Join Date: Jul 2009
Posts: 4
#2: Aug 5 '09

re: How to add properties to the Item object of a combobox?


I have found a workable solution. Maybe not the prettiest code, but it works so far.

I have created a custom array which holds the data for combobox and visible and enabled boolean.

I have created a custom combobox that inherits the MS combobox. I added a visablerows property. I added the following code to handle the measureitems code:

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

'If (e.Index = -1) Then Return

Dim arrayitem As CustomCBArray

arrayitem = sender.DataSource(e.Index)

If arrayitem.Visable Then
If e.ItemHeight = 0 Or e.ItemWidth = 0 Then
e.ItemHeight = sender.Bounds.Height - 2
e.ItemWidth = sender.Bounds.Width - 4
End If
Else
e.ItemHeight = 0
e.ItemWidth = 0
End If

arrayitem = Nothing

End Sub

----------------------------------------

I created another sub to handle the drawitems event in the custom combobox

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

If (e.Index = -1) Then Return

Dim arrayitem As CustomCBArray
Dim boxcolor As New System.Drawing.Color
Dim fontcolor As System.Drawing.Brush
Dim box_x As Integer = 0
Dim box_y As Integer = 0
Dim box_h As Integer = 0
Dim box_w As Integer = 0
Dim dstring As String = ""
Dim itemind As Integer = 0

itemind = e.Index

boxcolor = sender.BackColor
fontcolor = Brushes.Black

' Draw the background of the item.
e.DrawBackground()

Try
arrayitem = sender.DataSource(itemind)

If Not arrayitem.Visable Then
box_x = 0
box_y = 0
box_w = 0
box_h = 0
Else
box_x = e.Bounds.X
box_y = e.Bounds.Y
box_w = e.Bounds.Width
box_h = e.Bounds.Height
End If

If Not arrayitem.Enabled Then
fontcolor = Brushes.DarkGray
End If

dstring = arrayitem.DisplayString.ToString

Catch
dstring = ""
End Try

arrayitem = Nothing

Dim rectangle As Rectangle = New Rectangle(box_x, _
box_y, box_w, box_h)

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
boxcolor = Color.Black
fontcolor = Brushes.White
End If
e.Graphics.FillRectangle(New SolidBrush(boxcolor), rectangle)

' Draw each string in the array, using a different size, color,
' and font for each item.
e.Graphics.DrawString(dstring, _
e.Font, fontcolor, _
New RectangleF(box_x, box_y, box_w, box_h))

' Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle()

End Sub

--------------------------------------------------------------
I set the "DrawMode" propery of the combobox to "OwnerDrawVariable"

When I fill the control I set the visable and enabled values in the array. When I go to set a value for the combobox while loading the form, if enabled is false and visable is false (an old data value is still being used in the form) and I want it to be visable, I set the visable flag to true and add one to the visablerows property. Then I refill the control as follows:

------------------------------------------------------------

Public Sub O_ReFillControl(ByRef cbo As CustomComboBox)

If IsDBNull(cbo.DataSource) Then Exit Sub

Dim tarry As New ArrayList
Dim vr As Integer

tarry = cbo.DataSource
vr = cbo.VisableRows

With cbo
.DataSource = Nothing
.Items.Clear()

.DisplayMember = "DisplayString"
.ValueMember = "RecordID"
.DataSource = tarry
.VisableRows = NZ(vr, 0)

If .VisableRows < .MaxDropDownItems Then
.DropDownHeight = .VisableRows * .ClientRectangle.Height
End If
End With

End Sub

------------------------------------------------------------------
Note: you should add the last if statement above to the fill control procedure as well.
Reply

Tags
combobox, customize, objectcollection