Home All Groups Group Topic Archive Search About

Item doesn't show text on selection

Author
18 Jun 2006 6:02 AM
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

Author
19 Jun 2006 3:11 PM
Claes Bergefall
Probably beacuse you're shadowing the ToString method in the ListItems
class. Try overriding it instead

   /claes

Show quoteHide quote
"HKSHK" <hk***@gmx.net> wrote in message
news:4494ec69$0$13348$9b622d9e@news.freenet.de...
> 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