2016-12-21 73 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 
相關問題