Public Overloads Overrides Function GetEditStyle( _ ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle Return UITypeEditorEditStyle.DropDown End Function 为了显示图像资源列表,我必须列举一个给定程序集中的所有资源并且仅在列表中显示图像资源。为了简化,我决定使用一种简单的约定:当一个资源名以一个有效图像文件扩展名(.bmp,.jpg,.gif...)结束时,我们就认为这是一种图像资源,并且把它包括到该下拉列表框中。而且,我使用图像资源名的集合来填充这个下拉ListBox控件,后面详预以详述。
Private Function LoadResourceImage(ByVal resourceName As String) As Image Debug.Assert(Not resourceName Is Nothing) Dim ImageStream As System.IO.Stream = Me.ResourceAssembly.GetManifestResourceStream(resourceName) Return System.Drawing.Bitmap.FromStream(ImageStream) End Function 下拉用户接口是通过在重载的EditValue方法内动态地创建和填充一个ListBox控件实现的。编辑器也处理由ListBox生成的Click和KeyDown事件,因为这是拦截ENTER和ESC键所必需的。下列伪码显示了在EditValue方法中的实现逻辑:
Public Overloads Overrides Function EditValue(...) '存储上下文信息以用于下拉ListBox事件处理器。 '创建并使用可用的图像资源名填充该ListBox。 '添加我们的特殊“Browse...”项。 '绑定ListBox事件。 '在一个下拉窗口中显示该ListBox。 End Function