【问题标题】:Grouping form controls using dictionary, collection or class使用字典、集合或类对表单控件进行分组
【发布时间】:2019-01-17 02:37:58
【问题描述】:

我正在尝试对表单控件进行分类/分组,以便能够一次将更改应用于其中的几个。例如,我可能想要启用/禁用它们。

我应该将表单控件添加到集合、字典还是应该创建类?

理想情况下,我想创建类别和子类别。我将为类别和子类别定义属性。类别的属性将传递给“子”子类别。 例如,如果 CategoryA 的字体是“arial”,那么子类别 A1、A2 等的字体也将是“arial”。 我只会“存储”子类别中的对象。如果您对我如何构建这样的架构有任何想法,请提出一些建议。

在这个阶段我已经创建了一个字典。我很确定我无法使用字典和集合创建我想要的类别/子类别,但这仍然是朝着正确方向迈出的第一步(批量更改)。

我在使用字典时遇到的问题是特定于控件的属性/方法不会显示在 IntelliSense 中。我怎样才能使它们可用?

    Public dct As New Dictionary(Of Object, Integer)

    Dim ctlr As Control
    Dim i As Integer

        i = 1

        For Each ctlr In Controls
            dct.Add(ctlr.Name, i)
            i = i + 1
        Next

        For Each Item In dct
            'Enabled is not available
            Item.Enabled = False
        Next

【问题讨论】:

  • 您尝试使用Dictionary(Of Control, Integer) 吗?您当前的代码仍然可以工作,只是无法知道 Object 肯定是 Control,因此 Intellisense 不会提供 Control 属性。
  • 还有 Tab 属性,您可以在其中添加额外信息
  • @A 朋友:是的,我有,但没有任何改变
  • 您的项目中有 Option Explicit On 吗?你需要做For each Item as Control In dct
  • @一个朋友:不,它没有。添加“作为控件”会生成错误“'System.Collections.Generic.KeyValuePair(Of System.Windows.Forms.Control, Integer)'类型的值无法转换为'System.Windows.Forms.Control'”

标签: vb.net winforms class dictionary collections


【解决方案1】:

我会转储字典并使用 List(Of T)。将实际对象添加到列表中,并且属性应该可用。无论如何,这里是字典代码。在线评论和解释。

Public dct As New Dictionary(Of String, Integer)
        'I changed Object to String because that is what you are
        'adding to the dictionary. I don't see where the .Value
        'is ever used so I suggest changint to List(Of Button) or List(Of Control)
Private Sub OpCode2()

            Dim ctlr As Control
            Dim i As Integer

            i = 1
            'This saves the name of the control to the key of the dictionary
            'This is just a string unrelated to a control as far as the dictionary knows
            For Each ctlr In Controls
                dct.Add(ctlr.Name, i)
                i = i + 1
            Next

            For Each Item As KeyValuePair(Of String, Integer) In dct
                'The .Find method returns a control using the control name
                Dim ctrl As Control = Controls.Find(Item.Key, True).FirstOrDefault()
                'The properties inherited from Control will be available in intellisense
                'If you need a property of a particular type of control 
                'you will need to cast ctrl to the type you need
                ctrl.Enabled = False
            Next
End Sub

以下代码使用 List(Of T),并且更短且更易于阅读。

Public lst As New List(Of Control)
Private Sub OpCode2()
        Dim ctlr As Control
        For Each ctlr In Controls
            lst.Add(ctlr)
        Next

        For Each Item In lst
            'The list actually contains a reference to the control
            'so properties of Control are available
            Item.Enabled = False
        Next
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 2015-04-27
    • 1970-01-01
    • 2012-06-04
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多