2016-08-14 67 views
0

我正在使用Visual Basic 2010 Express Edition中的類庫來創建自定義文本框控件。如何爲文本框添加下拉屬性?如何在VB.NET中添加用戶控件下拉屬性

我需要一個動態的下拉菜單不喜歡,當你使用

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

聽起來好像你是一個組合框不是一個TextBox之後。這裏有很多在線教程。谷歌的'vb.net如何從數據庫中填充組合框' – FloatingKiwi

+0

感謝FloatingKiwi爲您的重播 –

+0

,但我要做一個自定義用戶控件,並添加一些屬性,我需要的是添加一個下拉屬性列表讓我們用戶從列表中選擇值 –

回答

0
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 
+0

非常感謝:)) 我會試試這個 我看到我需要的是代碼,感謝再次播放 –