2016-12-21 41 views
-1

这是形式之一,这种形式的所有用户控件的值将在My.Settings存储什么是循环此程序的最佳方法?

form

我有一个FlowLayoutPanel的,每次另一种形式,当应用程序启动, 如果主动选中,那么它会向FlowLayoutPanel添加一个带有折扣值的按钮。

我应该将这些usercontrol添加到列表中,然后遍历列表?或者解决这类问题的最好方法是什么?

修订

我如何可以添加多个项目在1码列表?我收到这个错误时的系统运行到线路5

类型“System.NullReferenceException”发生在XXX.exe的例外,但是在用户代码没有处理

其他信息:对象没有设置到的实例一个东西。

Public Sub RefreshDiscount(ByRef ref As scr_mainDiscount) 
    Dim li_disName As New List(Of TextBox) 
    Dim li_disValue As New List(Of TextBox) 
    Dim li_disType As New List(Of ComboBox) 
    Dim li_active As New List(Of CheckBox) 
    Dim tb_disName As TextBox() = {ref.tb_name1, ref.tb_name2, ref.tb_name3, ref.tb_name4, ref.tb_name5, ref.tb_name6, ref.tb_name7, ref.tb_name8, ref.tb_name9, ref.tb_name10} 
    Dim tb_disValue As TextBox() = {ref.tb_value1, ref.tb_value2, ref.tb_value3, ref.tb_value4, ref.tb_value5, ref.tb_value6, ref.tb_value7, ref.tb_value8, ref.tb_value9, ref.tb_value10} 
    Dim cb_disType As ComboBox() = {ref.cb_type1, ref.cb_type2, ref.cb_type3, ref.cb_type4, ref.cb_type5, ref.cb_type6, ref.cb_type7, ref.cb_type8, ref.cb_type9, ref.cb_type10} 
    Dim chkb_active As CheckBox() = {ref.CheckBox1, ref.CheckBox2, ref.CheckBox3, ref.CheckBox4, ref.CheckBox5, ref.CheckBox6, ref.CheckBox7, ref.CheckBox8, ref.CheckBox9, ref.CheckBox10} 

    li_disName.AddRange(tb_disName) 
    li_disValue.AddRange(tb_disValue) 
    li_disType.AddRange(cb_disType) 
    li_active.AddRange(chkb_active) 

    For index As Integer = 0 To li_active.Count - 1 
     If li_active(index).Checked = False Then 
      li_disName.RemoveAt(index) 
      li_disValue.RemoveAt(index) 
      li_disType.RemoveAt(index) 
      li_active.RemoveAt(index) 
     Else 
      Dim btn As New ctrl_DiscountButton 
      With btn 
       .Text = li_disName(index).Text 
       .Price = li_disValue(index).Text 
       .Type = li_disType(index).Text 
      End With 
      scr_sales.flp_discount.Controls.Add(btn) 
     End If 
    Next 

    li_disName.Clear() 
    li_disValue.Clear() 
    li_disType.Clear() 
    li_active.Clear() 
End Sub 
+2

您可以通过代码创建了这一切。将它们添加到列表中或使用FindControl,这两个选项都是有效的。 –

+2

你真的尝试过了什么?我很欣赏你是.NET新手,但你所有的问题似乎都是要求为你写代码。 –

回答

0

这里是展示如何找到CheckBox1通CheckBox10,“按名称”,使用的Controls.Find()的“searchAllChildren”选项,一个简单的例子:

For i As Integer = 1 To 10 
     Dim ctlName As String = "CheckBox" & i 
     Dim matches() As Control = Me.Controls.Find(ctlName, True) 
     If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then 
      Dim cb As CheckBox = DirectCast(matches(0), CheckBox) 
      ' do something with "cb" 
      If cb.Checked Then 
       ' ... code ... 
       ' possibly use code just like this to find the matching discount value control? 
      End If 
     End If 
    Next 
相关问题