为自定义控件创建有效属性值的下拉列表

发布于 2024-08-17 08:49:19 字数 173 浏览 5 评论 0原文

我创建了一个具有多个属性的自定义用户控件。其中之一指定我希望控件访问哪个数据库。我希望能够向控件的用户提供一个下拉菜单,他可以从中选择控件将与哪个数据库交互。

如何让下拉菜单发挥作用?我可以获得默认值,但尚未弄清楚如何获取可选列表。

任何帮助表示赞赏。

谢谢。

马歇尔

I've created a custom user control that has several properties. One specifies which database I want the control to access. I want to be able to present the user of the control a drop down from which he can select which database the control will interact with.

How do I get the dropdown to work? I can get default values, but have yet to figure out how to get the selectable list.

Any help is apprectiated.

Thanks.

Marshall

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁虫ゝ 2024-08-24 08:49:19

您只需将自己的 TypeConverter 附加到您的属性即可。您将重写 GetStandardValuesSupportedGetStandardValues 方法(也可能是 GetStandardValuesExclusive)以返回要显示的数据库列表。

如果您不熟悉 PropertyGrid 和 TypeConverter,这里有一个文档

You just need to attach your own TypeConverter to your property. You will override the GetStandardValuesSupported and GetStandardValues methods (maybe GetStandardValuesExclusive too) to return the list of databases you want to show.

If you are new to the PropertyGrid and the TypeConverter, here is a document.

陪我终i 2024-08-24 08:49:19

事实证明比我想象的要简单。

我为该属性设置了枚举,但在将其用于属性类型时遇到问题。说在课堂之外无法访问。

然后我愣了一下,将枚举从 Friend 更改为 Public,然后我就可以使用枚举作为属性类型。因此,当我查看控件的该属性的值时,枚举中的值会列在下拉列表中。

感谢所有回答。

马歇尔

It turns out to be simpler than I thought.

I had an enumeration set up for the property, but was having trouble using it for the property type. Said it was unaccessable outside of the class.

Then I had a 'duh' moment and changed the enumeration from Friend to Public, and then I was able to use the enumeration as the property type. As a result the values from the enumeration are listed in a dropdown when I look at the values for that property of the controls.

Thanks to all that answered.

Marshall

笨死的猪 2024-08-24 08:49:19

我对你的问题有点困惑。

如果您的用户控件包含 DropDownList 控件,只需在用户控件中的某个位置进行初始化即可。

最简单的方法是在用户控件的代码隐藏中,只需执行 DropDownList.Items.Add() 或任何用于添加项目的语法。

I'm a little confused about your problem.

If your user control contains a DropDownList control, just inititalize somewhere in the user control.

The easiest way is in the Codebehind for the usercontrol, just do DropDownList.Items.Add() or whatever the syntax is for adding an item.

安静 2024-08-24 08:49:19

这是我用来为组合框创建自定义数据源的模板:

    Private Class Listing

    Private _List As New ArrayList

    Public Sub Add(ByVal ItemNumber As Integer, ByVal ItemName As String)

        _List.Add(New dataItem(ItemNumber, ItemName))

    End Sub

    Public ReadOnly Property List() As ArrayList
        Get
            Return _List
        End Get
    End Property

End Class

Private Class dataItem
    Private _ItemNumber As Integer
    Private _ItemName As String

    Public Sub New(ByVal intItemNumber As Integer, ByVal strItemName As String)
        Me._ItemNumber = intItemNumber
        Me._ItemName = strItemName
    End Sub

    Public ReadOnly Property ItemName() As String
        Get
            Return _ItemName
        End Get
    End Property

    Public ReadOnly Property ItemNumber() As Integer
        Get
            Return _ItemNumber
        End Get
    End Property

    Public ReadOnly Property DisplayValue() As String

        Get
            Return CStr(Me._ItemNumber).Trim & " - " & _ItemName.Trim
        End Get

    End Property

    Public Overrides Function ToString() As String

        Return CStr(Me._ItemNumber).Trim & " - " & _ItemName.Trim

    End Function

End Class

这就是我加载它的方式:

    ListBindSource = New Listing

    Me.BindingSource.MoveFirst()
    For Each Row As DataRowView In Me.BindingSource.List
        Dim strName As String = String.Empty
        Dim intPos As Integer = Me.BindingSource.Find("Number", Row("Number"))
        If intPos > -1 Then
            Me.BindingSource.Position = intPos
            strName = Me.BindingSource.Current("Name")
        End If
        ListBindSource.Add(Row("Number"), strName)
    Next

    cboNumber.DataSource = ListBindSource.POList
    cboNumber.DisplayMember = "DisplayValue"
    cboNumber.ValueMember = "Number"
    AddHandler cboNumber.SelectedIndexChanged, AddressOf _
            cboNumber_SelectedIndexChanged

希望这会有所帮助。需要记住的一件事是,如果 cboNumber 有一个已分配给 SelectedIndexchanged 事件的处理程序,您将遇到问题。所以不要创建默认事件。

Here is the template I use to create a custom datasource for my comboboxes:

    Private Class Listing

    Private _List As New ArrayList

    Public Sub Add(ByVal ItemNumber As Integer, ByVal ItemName As String)

        _List.Add(New dataItem(ItemNumber, ItemName))

    End Sub

    Public ReadOnly Property List() As ArrayList
        Get
            Return _List
        End Get
    End Property

End Class

Private Class dataItem
    Private _ItemNumber As Integer
    Private _ItemName As String

    Public Sub New(ByVal intItemNumber As Integer, ByVal strItemName As String)
        Me._ItemNumber = intItemNumber
        Me._ItemName = strItemName
    End Sub

    Public ReadOnly Property ItemName() As String
        Get
            Return _ItemName
        End Get
    End Property

    Public ReadOnly Property ItemNumber() As Integer
        Get
            Return _ItemNumber
        End Get
    End Property

    Public ReadOnly Property DisplayValue() As String

        Get
            Return CStr(Me._ItemNumber).Trim & " - " & _ItemName.Trim
        End Get

    End Property

    Public Overrides Function ToString() As String

        Return CStr(Me._ItemNumber).Trim & " - " & _ItemName.Trim

    End Function

End Class

And this is how I load it:

    ListBindSource = New Listing

    Me.BindingSource.MoveFirst()
    For Each Row As DataRowView In Me.BindingSource.List
        Dim strName As String = String.Empty
        Dim intPos As Integer = Me.BindingSource.Find("Number", Row("Number"))
        If intPos > -1 Then
            Me.BindingSource.Position = intPos
            strName = Me.BindingSource.Current("Name")
        End If
        ListBindSource.Add(Row("Number"), strName)
    Next

    cboNumber.DataSource = ListBindSource.POList
    cboNumber.DisplayMember = "DisplayValue"
    cboNumber.ValueMember = "Number"
    AddHandler cboNumber.SelectedIndexChanged, AddressOf _
            cboNumber_SelectedIndexChanged

Hope this helps. One thing to keep in mind is that if cboNumber has a handler already assigned to the SelectedIndexchanged event, you will encounter problems. So don't create a default event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文