2016-11-26 62 views
0

我有以下函数循环通过表单上的控件来将有用的数据存储到字符串数组中。它跳过2,因为第一个控件是标签。第二个控件可以是文本框或复选框。我的功能允许我收集所有的文本框信息,但不是复选框。我如何修改我的功能来收集复选框信息?我可以用类似的东西真/假,选中/未选中等动态控件

Public Function createInput() As String() 
    Dim int = myControls.Length 
    Dim count As Integer = 0 
    Dim str(int) As String 
    For i = 1 To int - 1 Step 2 
     str(count) = myControls(i).Text 
     count += 1 
    Next 
    Return str 
End Function 

回答

0

我怎么能修改我的功能来收集信息复选框?

可以使用在控制集合OfType分机,假定myControls是一个控制集合:

For Each cb As CheckBox in myControls.OfType(Of CheckBox) 

Next 
0

可以检查对象的类型,并采取相应的行动

Public Function createInput() As String() 
    Dim int = myControls.Length 
    Dim count As Integer = 0 

    Dim ctrlData As New List(Of String)() 
    For Each c As Control In myControls 
     If TypeOf c Is TextBox Then 
      ctrlData.Add(c.Text) 
     ElseIf TypeOf c Is CheckBox Then 
      ctrlData.Add(DirectCast(c, CheckBox).Checked) 
     ElseIf TypeOf c Is ListBox Then 
      Dim lb As ListBox = DirectCast(c, ListBox) 
      If lb.SelectedIndex > -1 Then 
       ctrlData.Add(lb.SelectedValue) 
      End If 
     ElseIf . . . . . Then 
       . . . . 
     Else 
       . . . . . . 
     End if 
    Next 
    Return ctrlData.ToArray() ' <-- Note that List is more flexible than array 
End Function 

这是参照您正在查找:https://msdn.microsoft.com/en-us/library/s4zz68xc.aspx