My ComboBoxEx is not working

C

Carlo

Hello

I'm writing an enhanced version of ComboBox (that inherits from
Windows.Forms.ComboBox) that can display images close to the text.
I've replaced the default Item collection (ComboBox.ObjectCollection, that
is a collecion of Object objects) with my own collection
(ImageComboBoxItemCollection) that holds a number of custom
ImageComboBoxItem items that inherits from Object.

Editor(GetType(ImageComboItemCollectionEditor), GetType(UITypeEditor))> _
Public Shadows ReadOnly Property Items() As ImageComboItemCollection

Everything works fine (serialization included), except for the Overrides of
OnDrawItem that does NOT works. Debugging the ComboBoxEx, I've noted that
OnDrawItem is never called.
If I try to suppress the Shadowing of Items Property, everithing is working
fine again, and OnDrawItem is properly called again.

What's wrong?

Thank you.

Carlo
 
C

Carlo

At a first look, the answer is 'yes'. I'll study the sample in depth.

The only difference I noted is that you used
Public Shadows ReadOnly Property Items() As ObjectCollection
while I used
Public Shadows ReadOnly Property Items() As ImageComboItemCollection

Do you think that the different type of the collection can be responsible
for the problem I have?

Thany you, Mick, for helping me...

Carlo



-------------------------------------------
Carlo, MCP (Windows Based Applications)
(e-mail address removed)


"Mick Doherty"
 
M

Mick Doherty

There are two main differences.

The first, as you pointed out, is the collection type. I never managed to
get this working with a custom collection, even when it was inherited from
ObjectCollection, but to be quite honest, I didn't really try too hard as I
believe the simple solutions are often the best.

The second difference is that my custom item is inherited from Component
rather than Object. Inheriting from Object will probably work, but requires
much more work to get the class to Serialize.
 
C

Carlo

Good morning Mick

in my previous version, I inherited from Object (and serialization works),
but inheriting from Component is much more useful. For example, the built-in
(name) property does not exist in Object.

However, now I'm trying to "inject" some of your ideas into my version. My
goal is to get a quite more complex Items collection (with ImageIndex,
Image, Tag, Text, ForeColor, etc.).

Is thee only a bit of code that I've not understood.

This is my version:

Public Property Text() As String
Get
Return mText
End Get
Set(ByVal Value As String)
mText = Value
End Set
End Property

Public Overrides Function ToString() As String
Return Text
End Function

....and this is your:
Public Property Item() As Object
Get
Return mObject
End Get
Set(ByVal Value As Object)
mObject = Value
End Set
End Property

Public Overrides Function ToString() As String
If mObject Is Nothing Then
Return String.Empty
Else
Return mObject.ToString
End If
End Function

I've not understood the purpose of the Item property. Is is required by the
ObjectCollection to work properly? Do you think can be removed and replaced
with Text?

Thank you once more.

Carlo




-------------------------------------------
Carlo, MCP (Windows Based Applications)
(e-mail address removed)


"Mick Doherty"
There are two main differences.

The first, as you pointed out, is the collection type. I never managed to
get this working with a custom collection, even when it was inherited from
ObjectCollection, but to be quite honest, I didn't really try too hard as
I believe the simple solutions are often the best.

The second difference is that my custom item is inherited from Component
rather than Object. Inheriting from Object will probably work, but
requires much more work to get the class to Serialize.
 
M

Mick Doherty

Hi Carlo,

In a standard ComboBox the Item property is declared as Object. At
DesignTime you can only edit the collection to add Strings as the
CollectionEditor is defined as the StringCollection type. At Runtime though,
you can assign any type of Object to the Items collection (such as our
ImageComboItem type) and the DropDown list will display it's ToString value.
To see this in action just change the DrawMode property back to Normal and
you will see that the ComboBox will display the ImageComboItem.ToString()
value for each item.

If you have no intention of assigning complex types to the ImageCombo then
you can safely do away with it and assign the Text property as you've
defined it.

I should also point out a small bug which I have just found. The
SelectedIndexChanged event fires twice and so the OnSelectedIndexChanged()
code should be modified as follows:

Protected Overrides Sub OnSelectedIndexChanged(ByVal e As System.EventArgs)
If Me.SelectedIndex <> currentIndex Then
currentIndex = Me.SelectedIndex
MyBase.RefreshItem(Me.SelectedIndex)
Else
MyBase.OnSelectedIndexChanged(e)
End If
End Sub
 
C

Carlo

Hello Mick
I need to thank you very much for your valuable help.
Hope to meet you soon in ng!
Sincerely,
Carlo



-------------------------------------------
Carlo, MCP (Windows Based Applications)
(e-mail address removed)


"Mick Doherty"
Hi Carlo,

In a standard ComboBox the Item property is declared as Object. At
DesignTime you can only edit the collection to add Strings as the
CollectionEditor is defined as the StringCollection type. At Runtime
though, you can assign any type of Object to the Items collection (such as
our ImageComboItem type) and the DropDown list will display it's ToString
value. To see this in action just change the DrawMode property back to
Normal and you will see that the ComboBox will display the
ImageComboItem.ToString() value for each item.

If you have no intention of assigning complex types to the ImageCombo then
you can safely do away with it and assign the Text property as you've
defined it.

I should also point out a small bug which I have just found. The
SelectedIndexChanged event fires twice and so the OnSelectedIndexChanged()
code should be modified as follows:

Protected Overrides Sub OnSelectedIndexChanged(ByVal e As
System.EventArgs)
If Me.SelectedIndex <> currentIndex Then
currentIndex = Me.SelectedIndex
MyBase.RefreshItem(Me.SelectedIndex)
Else
MyBase.OnSelectedIndexChanged(e)
End If
End Sub
 

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