如何在VB.NET中添加用户控件下拉属性

时间:2016-08-14 07:21:13

标签: vb.net

我在Visual Basic 2010 Express Edition中使用类库来制作自定义文本框控件。如何为文本框添加下拉属性?

我需要一个动态下拉菜单,与使用

时不同
Enum MaxValue
   item1 = 0
End Enum

因为我必须从数据库中获取项目。

我尝试添加一个可浏览的选项但没有发生任何事情:

<Browsable(True)>
Property Max_Value() As String

     Get
        Return MaxValue
     End Get

     Set(value As String
        MaxValue = value
     End Set
End Property

1 个答案:

答案 0 :(得分:1)

    Imports System.ComponentModel
    Imports System.Drawing.Design
    Imports System.Windows.Forms.Design

    Public Class TestTextBox
            Inherits TextBox

            <Browsable(True)>
            <Editor(GetType(Editor), GetType(UITypeEditor))>
            <DefaultValue("Hello")>
            Public Property MyProperty As String

            Private Class Editor
                    Inherits UITypeEditor

                    Private mSvc As IWindowsFormsEditorService

                    Public Overrides Function GetEditStyle(context As ITypeDescriptorContext) As UITypeEditorEditStyle
                            Return UITypeEditorEditStyle.DropDown
                    End Function

                    Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
                            mSvc = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)

                            Dim lb As New ListBox()
                            For Each value In {"Hello", "Whats", "Happening"}
                                    lb.Items.Add(value)
                            Next

                            If value IsNot Nothing Then
                                    lb.SelectedItem = value
                            End If

                            mSvc.DropDownControl(lb)

                            value = DirectCast(lb.SelectedItem, String)

                            Return value
                    End Function

            End Class

    End Class