1

所以我最近掌握了在我的 Visual Basic 编程中使用类的概念,我发现它非常有用。在我当前的项目中,我有几个复选框组框(每个复选框表示一个“行为”),并且在每个组框中,总是有一个复选框具有文本框控件而不是标签(以允许用户指定“其他”行为)。正是那个用户生成的标签给我带来了麻烦......

我创建了一个名为“行为”的类,它基本上执行以下操作:

  1. getChecked > 此方法获取每个选中的复选框并将其添加到给定表单的 BehaviorCollection 中。
  2. behaviorCollection > 表示选中复选框的集合。
  3. getOtherChecked > 与“getChecked”相同,除了“其他行为”复选框。
  4. otherBehaviorCollection > 表示选中的“其他”复选框的集合。

问题是对于每个选中的“其他行为”复选框,我需要存储其相应文本框的值。我想设置我的 getOtherChecked() 方法来做到这一点,这样最后,我就可以做到这样......

Dim myBoxes as new Behaviors
Dim cBox as Checkbox
Dim cBoxLabel as String

myBoxes.getOtherChecked(myUserForm) 'This would get each checked "Other Behaviors" checkbox object, and also somehow add another property to it called "LinkedTextboxLabel" that would be assigned the value of the corresponding textbox.
cBox = myBoxes.otherBehaviorCollection.item(0) 'Assign a checkbox from my "Other Behaviors" collection to a variable.
cBoxLabel = cBox.LinkedTextboxLabel 'Assign the user-inputted value of the linked textbox to a variable.

所以基本上我怎么能/应该向集合项或复选框添加自定义属性?

我想过只是将控件的名称添加到临时 DataTable 或 SQL 表中,以便每一行在一个列中都有一个复选框的名称,在下一列中具有相应的文本框值,但我希望有一个更常见的使用和接受的方法。

先感谢您!

4

3 回答 3

1

我知道它不能回答将属性添加到属性的问题,但是您可以为“其他”复选框创建一个类并覆盖它的功能吗?然后您可以将复选框和 OtherCheckBoxes 添加到您的通用集合中?例如,(绝不是完整的,但你应该明白)

编辑:更改代码以显示阴影

Public Class OptionalCheckbox : Inherits CheckBox
Private mOptionalText As String

Public Shadows Property Text() As String
    Get
        Return mOptionalText
    End Get
    Set(value As String)
        mOptionalText = value
        MyBase.Text = value
    End Set
End Property
End Class

对于每个项目,如果您要检索 .Text,您将获得文本框值或复选框标签(如果它是普通复选框)

以及如何在代码的其他部分中使用。同样,这只是一个例子。您仍然需要使用分配给 OtherCheckBox 的文本框,以使其将文本写入其中,并从中读取到 Class 的 .Text 属性中。

    Dim newCheckBoxCollection As New Collection

    Dim cBox As New CheckBox
    cBox.Text = "Standard Value Here"
    'other properties of the checkbox can be modified here
    newCheckBoxCollection.Add(cBox)

    Dim cOBox As New OptionalCheckbox
    cOBox.Text = "Optional Text Here"
    'other properties of the checkbox can be modified here
    newCheckBoxCollection.Add(cOBox)

    For Each cb As CheckBox In newCheckBoxCollection
        Me.FlowLayoutPanel1.Controls.Add(cb)
    Next
于 2013-04-11T20:39:55.240 回答
1

您可以为与“其他行为”复选框关联的文本添加一个属性。

编辑:您可能试图将您的数据概括得太远,因为“其他行为”是一种特殊情况,值得单独考虑。

如果您查看以下代码(在新的 Windows 窗体项目中)创建的内容,它可能会给您一些想法:

Public Class Form1

    ''' <summary>
    ''' A behaviour domain and its characteristics, with one user-defined entry.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class BehavioursSectionDescriptor
        Property BehaviourTypeName As String
        Property BehaviourNames As List(Of String)
        Property CustomBehaviours As String
    End Class

    ''' <summary>
    ''' Return a GroupBox containing CheckBoxes and one Checkbox with a TextBox adjacent to it.
    ''' </summary>
    ''' <param name="behaviourSet"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetBehaviourGroupPanel(behaviourSet As BehavioursSectionDescriptor) As GroupBox

        Dim gb As New GroupBox
        gb.Text = behaviourSet.BehaviourTypeName

        Dim fixedBehaviourNames As List(Of String) = behaviourSet.BehaviourNames
        Dim customBehavioursValue As String = behaviourSet.CustomBehaviours

        Dim cbVertSeparation As Integer = 4
        Dim gbPadding As Integer = 20

        Dim cb As New CheckBox

        Dim yLoc As Integer = gbPadding

        For i = 0 To fixedBehaviourNames.Count - 1
            cb = New CheckBox
            cb.Location = New Point(gbPadding, yLoc)
            cb.Text = fixedBehaviourNames(i)
            ' you can use the .Tag Object of a Control to store information
            cb.Tag = behaviourSet.BehaviourTypeName & "-Cb-" & i.ToString()
            gb.Controls.Add(cb)
            yLoc += cb.Height + cbVertSeparation

        Next

        cb = New CheckBox
        cb.Text = ""
        cb.Location = New Point(gbPadding, yLoc)
        cb.Tag = behaviourSet.BehaviourTypeName & "-Custom behaviours"
        gb.Controls.Add(cb)

        Dim tb As New TextBox
        tb.Location = New Point(gbPadding + 18, yLoc)
        tb.Width = 100
        tb.Text = customBehavioursValue
        gb.Controls.Add(tb)
        ' make sure the textbox appears in front of the checkbox's label area
        tb.BringToFront()

        gb.Size = New Size(160, yLoc + gbPadding * 2)

        Return gb

    End Function

    Private Function GetTestData() As List(Of BehavioursSectionDescriptor)
        Dim bsds = New List(Of BehavioursSectionDescriptor)
        bsds.Add(New BehavioursSectionDescriptor With {.BehaviourTypeName = "In water", _
                                                     .BehaviourNames = New List(Of String) From {"Floats", "Spins"}, _
                                                     .CustomBehaviours = "Paddles"})

        bsds.Add(New BehavioursSectionDescriptor With {.BehaviourTypeName = "Under light", _
                                                     .BehaviourNames = New List(Of String) From {"Shines", "Glows", "Reflects"}, _
                                                     .CustomBehaviours = "Iridesces"})

        bsds.Add(New BehavioursSectionDescriptor With {.BehaviourTypeName = "Near food", _
                                                     .BehaviourNames = New List(Of String) From {"Sniffs", "Looks"}, _
                                                     .CustomBehaviours = ""})

        Return bsds

    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim bsds As List(Of BehavioursSectionDescriptor) = GetTestData()

        Dim gbs As New List(Of GroupBox)
        Dim xLoc As Integer = 20
        Dim yLoc As Integer = 20

        ' make some GroupBoxes to present the data input fields
        For i = 0 To bsds.Count - 1
            Dim gb = GetBehaviourGroupPanel(bsds(i))
            gb.Location = New Point(xLoc, yLoc)
            gb.Dock = DockStyle.None
            yLoc += gb.Height + 30
            Me.Controls.Add(gb)
        Next

        ' size the form to fit the content
        Me.Size = New Size(240, yLoc + 40)

    End Sub


End Class
于 2013-04-11T19:16:48.610 回答
0

如果您只是尝试将数据保存到 DataTable 或 SQL 表之类的东西中,那么代码会有点矫枉过正。我建议您使用流读取器/写入器并尝试以这种方式检查值,因为代码会更简单。

于 2013-04-11T18:25:38.773 回答