473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Item doesn't show text on selection

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
Jun 18 '06 #1
1 1309
Probably beacuse you're shadowing the ToString method in the ListItems
class. Try overriding it instead

/claes

"HKSHK" <hk***@gmx.net> wrote in message
news:44***********************@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

Jun 19 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lkrubner | last post by:
I've got a function, you can see it below, that is being called onmouseup in the textarea on my main form. The idea is to find a selection if possible and store that text in a global variable. I...
3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
3
by: John Walker | last post by:
Hi, On an ASP.NET page I have a drop down list control. When the user pulls down the list and makes a selection, I perform validation, and if the validation fails I want the selected item in...
3
by: Michael Meckelein | last post by:
Hello, I run into trouble move down a selected item in a listbox. The code moving down the item is the following one: for (int j = lv.SelectedItems.Count-1; j >=0; j--) { ListViewItem...
3
by: Kevin Walzer | last post by:
I'm trying to set the active item in a Tkinter listbox to my application's currently-defined default font. Here's how I get the fonts loaded into the listbox: ...
3
by: Ray | last post by:
Hello World, I made a Windowsform that reads data from a CSV file. It works fine, but when I have read the data of a record I have to re-Debug the form to read another record. So when I put a...
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
4
by: coldfusionstudent | last post by:
i wish to show/appear and dissappear text box based on a the drop down item selected. what do i have to add? thanks under Comm_DEV drop down selection.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.