I've written a custom ComboBox control that inherits from System.Windows.Forms.ComboBox. It has a "Value" property that I've written as follows:
Public Property Value() As Object
Get
If SelectedIndex = -1 Then Return Nothing
If String.IsNullOrWhitespace(ValueMember) Then
Return Items(SelectedIndex)
Else
Return FilterItemOnProperty(Items(SelectedIndex), ValueMember)
End If
End Get
Set(ByVal newvalue As Object)
'...
End Set
End Property
Basically, reading the Value will return Nothing
if nothing is selected, the selected object itself if ValueMember is not set, or the appropriate property value of the selected object if the ValueMember is set.
Up to this point, the code has worked just fine.
However, a recent change has been made to one of the classes used to populate the ComboBox as follows:
Partial Public Class Modality
<Browsable(False)>
Public Property ModalityID As Integer
Public Property ModalityName As String
Public Property ModalityAbbrevName As String
End Class
The ModalityID property, which is used as the ValueMember of the ComboBox, has had the Browsable(False) attribute added, so it does not show up as a column when a collection of Modality objects gets bound to a DataGridView control elsewhere in the application.
This now breaks the application, because MyComboBox1.Value is now returning the selected Modality object, and not the ModalityID of the selected Modality object, even though the ComboBox's ValueMember is still set to "ModalityID".
Digging into the .NET Framework's source code for ListControl.FilterItemOnProperty(object item, string field)
is not enlightening me.
The question is, why does adding a <Browsable(False)>
attribute to a class property prevent FilterItemOnProperty()
from properly retrieving the property's value?