Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Images in a Databound ComboBox?

Status
Not open for further replies.

avarga82

Programmer
May 19, 2003
62
US
Hello,

I'm trying to draw an icon for images in my combobox, and I've successfully done so, but only with a non-databound combobox. I was wondering how to do it with a databound combobox. I can't seem to get the dispaymember of the combobox item in the DrawItem event (only the Text property...ie: MyBase.Item(e.index).Text ).

Next question: How can I draw an image in the edit portion of a databound combobox as well?

Any ideas or a point in the right direction would be most appreciated.

Thanks!
 
Pretty certain you will have to custom draw the combo items. I use code such as follows which then raises an event for fetching the Image Index. This drawitem method is also used to display as many fields I require in the combo, and is part of my base class for datacombo.

You should be able to get something up and running pretty quickly from overriding the drawitem event. Dont forget to set the combos OwnerDrawn property to True


Code:
Private Sub comDataCombo_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MyBase.DrawItem

If Me.Items.Count = 0 Then Exit Sub

'Draw the background of the ListBox control for each item.
e.DrawBackground()

' Create a new Brush
Dim myBrush As Brush

' Get List Item
Dim oCurrentListItem As oListItem = CType(Me.Items(e.Index), oListItem)

' Set Brush Color depending if Item Selected
If InStr(e.State.ToString, "Selected") <> 0 Then
    myBrush = Brushes.White
Else
    myBrush = Brushes.Black
End If

Dim LeftAddition As Integer = 0
'Any Image
If Not Me.oImageList Is Nothing Then
    Dim i As Integer
    RaiseEvent GettingImage(Me, i, e)

    If i >= 0 Then
        e.Graphics.DrawImage(Me.oImageList.Images(i), e.Bounds.X, e.Bounds.Y)
    End If

    LeftAddition = 20
End If


' Draw Text Field Value(s)
e.Graphics.DrawString(oCurrentListItem.sText, e.Font, myBrush, New RectangleF(e.Bounds.X + LeftAddition, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))

' Go thru Display Columns and Draw each Field Value
For Each oDisplayColumn As DisplayColumns In oCurrentListItem.oDisplayColumns
            e.Graphics.TranslateTransform(oDisplayColumn.Left + LeftAddition, 0)
            e.Graphics.DrawString(oDisplayColumn.NameOrValue, e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))

Next

' If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle()



Sweep
...if it works dont f*** with it
curse.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top