2013-04-11 61 views
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表中,以便每行在一列中具有复选框的名称,并且在下一行中具有对应的文本框值,但我希望是一种比较常用和被接受的方法。

预先感谢您!

+1

伟大的问题!值得投票和收藏。 – Kraxed 2013-04-11 18:26:17

回答

0

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

+0

感谢您的提示。我不熟悉流媒体阅读器/作家,但现在开始阅读。有趣的是,属性内的属性似乎并不罕见。 。 。希望有一些VB大师可以显示它是如何完成的:) – Lopsided 2013-04-11 18:41:43

+1

是的,他们是非常有用的好吧,我一直在使用它们:)方便的保存设置形式:)是的,我希望一个人来:L – Kraxed 2013-04-11 19:02:58

+0

不会在这里工作,但我喜欢使用它来保存设置的想法。 +1评论haha – Lopsided 2013-04-11 23:53:03

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

这不能完成,因为相应的文本框充当此复选框的标签。复选框文本值应该为空。 – Lopsided 2013-04-11 19:26:39

+1

@错过我添加了一些代码,显示如何合并自定义元素。 – 2013-04-11 21:48:22

+0

这确实给了我一个想法。我可以将每个“其他”复选框放入只有文本框对应的面板中。然后,我可以很容易地引用每个像这样panelName.Controls(0)复选框和panelName.Controls(1)的文本框。 – Lopsided 2013-04-12 14:43:35

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 

对于每个项目,如果你要取回。文本,你会得到您的文本框的值或您的复选框标签(如果它是一个正常的复选框)

以及如何利用您的代码的其他部分。再次,这仅仅是一个例子。您仍然需要使用分配给OtherCheckBox的文本框,以便将文本写入该文本,并将其读入该类的.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 
+0

这看起来非常好。我曾经见过这样的具体覆盖,但从未深入研究过它们的用法。我明天会试试这个,如果它能正常工作,请将其标记为正确!再次感谢。 – Lopsided 2013-04-11 23:47:29

+0

这不适用于我:/ IDE在这里不会让我使用Overrides,它说我需要使用阴影。我花了几个小时阅读覆盖,阴影,拓宽操作符,派生类和基类,但似乎没有任何工作。我为这个特定的项目找到了一个解决方案,但我仍然希望看到一个创建属性属性的成功例子,或者像您建议的那样替换类型。 – Lopsided 2013-04-12 15:26:28

+1

我改变了上面的代码,让你了解Shadows是如何工作的。当然,需要做更多的工作才能使它适用于你(例如让它从文本框读取/写入),但是如果将它粘贴到IDE中,则应该明白。另一个想法是创建一个用户控件,它将Checkbox或OptionalCheckBox控件作为一个属性,然后执行您需要的操作。 (显示文本框,隐藏复选框文本等)。将用户控件想象成一个具有视觉元素的类。 – APrough 2013-04-12 17:15:44

相关问题