Item doesn't show text on selection

H

HKSHK

Hi guys,

I have a problem with the combo box I'm writing on.
What I want is custom items, which are fine so far, but when I select
one the combo box shows the project name
("WindowsApplication1+ListItems")... What's wrong? The source code is below.

Thanks in advance!

Best regards,

HKSHK

#Region "ComboBoxVB6"
Public Class ComboBoxVB6
Inherits System.Windows.Forms.ComboBox

Public Class ListItems
Public Item As Object
Public ItemData As Long

Public Sub New(ByVal Item As Object, ByVal ItemData As Long)
Me.Item = Item
Me.ItemData = ItemData
End Sub

Public Property Text() As String
Get
Return Me.Item.ToString
End Get
Set(ByVal Value As String)
Me.Item = Value
End Set
End Property

Public Overridable Shadows Function ToString() As String
Return Me.Item.ToString
End Function
End Class


Public Function AddItem(ByVal item As Object) As Integer
Return AddItem(item, 0)
End Function
Public Function AddItem(ByVal Item As Object, ByVal ItemData As Long) As
Integer
Dim t As New ListItems(Item, ItemData)
Return Items.Add(t)
End Function

Public Property ItemData(ByVal index As Integer) As Long
Get
If TypeOf (Items.Item(index)) Is ListItems Then
Dim t As ListItems
t = Items.Item(index)
Return t.ItemData
Else
Return 0
End If
End Get
Set(ByVal Value As Long)
If TypeOf (Items.Item(index)) Is ListItems Then
Dim t As ListItems
t = Items.Item(index)
t.ItemData = Value
Items.Item(index) = t
Else
Dim t As New ListItems(Items.Item(index), Value)
Items.Item(index) = t
End If
End Set
End Property

Public Sub New()
Me.DrawMode = DrawMode.OwnerDrawFixed
End Sub

Protected Overrides Sub OnDrawItem(ByVal e As
System.Windows.Forms.DrawItemEventArgs)
'draw background & focus rect
e.DrawBackground()
e.DrawFocusRectangle()

'check if it is an item from the ListItems collection
If e.Index < 0 Then
'not an item, draw the text (indented)
e.Graphics.DrawString(Me.Text, e.Font, New SolidBrush(e.ForeColor),
e.Bounds.Left, e.Bounds.Top)
Else
'check if item is a ListItems item

If TypeOf (Me.Items(e.Index)) Is ListItems Then

'get item to draw
Dim item As ListItems = Me.Items(e.Index)

' draw text
e.Graphics.DrawString(item.Text, Font, New
SolidBrush(e.ForeColor), e.Bounds.Left, e.Bounds.Top)

Else
' it is not an ListItems item, draw it
e.Graphics.DrawString(Me.Items(e.Index).ToString(), e.Font, New
SolidBrush(e.ForeColor), e.Bounds.Left, e.Bounds.Top)
End If

MyBase.OnDrawItem(e)
End If

End Sub

Public Overrides Function ToString() As String
Dim t As ListItems
t = SelectedItem

Return t.ToString
End Function


End Class
#End Region
 
C

Claes Bergefall

Probably beacuse you're shadowing the ToString method in the ListItems
class. Try overriding it instead

/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top