2013-01-03 106 views
0

这是我的代码,我用我的列表和循环通过它:列表的按钮下一个和上

public List<String> InfoList = new List<String>(); 
int i = 0; 

private void populatelblDesc() 
    { 
     conn.Open(); 
     string query; 
     query = "select distinct dp.current_location_code,dci.dest_location_code,dps.order_no,dps.carton_code,dps.company_id_no,dps.no_of_full_cartons,dps.no_of_total_packs,dg.dc_grv_id_no,dg.start_rcv_datetime,s.sku_code,(pt.product_type_desc|| ' ' ||ps.prod_size_desc|| ' ' ||c.colour_desc) product_desc from dc_pallet_stock dps,dc_pallet dp,sku s,purch_order_carton_sku pocs,dc_crane_instruc dci,dc_grv dg,product_type pt,prod_size ps,colour c where dp.pallet_id_no = dps.pallet_id_no and dps.order_no = pocs.order_no and dps.company_id_no = pocs.company_id_no and dps.carton_code = pocs.carton_code and s.sku_id_no = pocs.sku_id_no and s.company_id_no = dps.company_id_no and dp.pallet_id_no ='"+palletId+"' and dci.pallet_id_no(+) = dps.pallet_id_no and dg.dc_grv_id_no = dps.dc_grv_id_no and s.prod_size_id_no = ps.prod_size_id_no(+) and s.colour_id_no = c.colour_id_no(+) order by sku_code"; 
     OracleCommand cmd = new OracleCommand(query, conn); 
     OracleDataReader dr = cmd.ExecuteReader(); 
     while (dr.Read()) 
     { 
      this.InfoList.Add(dr["current_location_code"].ToString()); 
      this.InfoList.Add(dr["dest_location_code"].ToString()); 
      this.InfoList.Add(dr["order_no"].ToString()); 
      this.InfoList.Add(dr["company_id_no"].ToString()); 
      this.InfoList.Add(dr["no_of_full_cartons"].ToString()); 
      this.InfoList.Add(dr["no_of_total_packs"].ToString()); 
      this.InfoList.Add(dr["dc_grv_id_no"].ToString()); 
      this.InfoList.Add(dr["start_rcv_datetime"].ToString()); 
      this.InfoList.Add(dr["sku_code"].ToString()); 
      this.InfoList.Add(dr["product_desc"].ToString()); 
     } 
     dr.Close(); 
     conn.Close(); 
    } 

下一步按钮

private void button1_Click_1(object sender, EventArgs e) 
    { 
     populatecompany_name(); 

     if (i + 1 < this.InfoList.Count) 
      lbl1.Text = this.InfoList[++i]; 
     if (i + 1 < this.InfoList.Count) 
      lbl2.Text = this.InfoList[++i]; 
     if (i + 1 < this.InfoList.Count) 
      lbl3.Text = this.InfoList[++i]; 
     if (i + 1 < this.InfoList.Count) 
      lbl4.Text = this.InfoList[++i]; 
     if (i + 1 < this.InfoList.Count) 
      lbl5.Text = this.InfoList[++i]; 
     if (i + 1 < this.InfoList.Count) 
      lbl6.Text = this.InfoList[++i]; 
     if (i + 1 < this.InfoList.Count) 
      lbl7.Text = this.InfoList[++i]; 
     if (i + 1 < this.InfoList.Count) 
      lbl8.Text = this.InfoList[++i]; 
     if (i + 1 < this.InfoList.Count) 
      lbl9.Text = this.InfoList[++i]; 
     if (i + 1 < this.InfoList.Count) 
      lbl10.Text = this.InfoList[++i]; 
    } 

以前按钮

private void button2_Click(object sender, EventArgs e) 
    { 
     populatecompany_name(); 

     if (i - 1 < this.InfoList.Count) 
      lbl9.Text = this.InfoList[--i]; 
     if (i - 1 < this.InfoList.Count) 
      lbl8.Text = this.InfoList[--i]; 
     if (i - 1 < this.InfoList.Count) 
      lbl7.Text = this.InfoList[--i]; 
     if (i - 1 < this.InfoList.Count) 
      lbl6.Text = this.InfoList[--i]; 
     if (i - 1 < this.InfoList.Count) 
      lbl5.Text = this.InfoList[--i]; 
     if (i - 1 < this.InfoList.Count) 
      lbl4.Text = this.InfoList[--i]; 
     if (i - 1 < this.InfoList.Count) 
      lbl3.Text = this.InfoList[--i]; 
     if (i - 1 < this.InfoList.Count) 
      lbl2.Text = this.InfoList[--i]; 
     if (i - 1 < this.InfoList.Count) 
      lbl1.Text = this.InfoList[--i]; 
     if (i - 1 < this.InfoList.Count) 
      lbl10.Text = this.InfoList[--i]; 
    } 

这可能不是最好的办法。正如你在前面的按钮代码中看到的那样,我必须改变表单上标签的填充顺序,否则它将出现乱码。

我的问题是: 当我点击以前并没有更多的数据去我得到一个例外:

ArgumentOutOfRangeException was unhandles - Specified argument was out of the range of valid values. 
Parameter name: index 

我该如何解决这个问题。我试过了我能想到的一切。

在此先感谢。

+0

也许你可以检查,如果'我> 0' – horgh

+0

有将要太多的建议,以改善该代码。对于例外情况,请检查'if(this.InfoList.Count == 0)' – shahkalpesh

回答

0

假设你有你的窗体上的所有标签,它们都通过名称都有名字LBL#,那么你可以做水木清华这样把所有的标签和排序:

private void button1_Click(object sender, EventArgs e) 
{ 
    int i = 0; 
    foreach (Label l in this.Controls 
          .OfType<Label>() 
          .OrderBy(l => int.Parse(l.Name.Substring(3)))) 
    { 
     if (i < InfoList.Count) 
      l.Text = InfoList[i++]; 
    } 
} 

我不知道为什么你试图改变顺序,因为最后,我想,InfoList的第i个元素应该放入第i个标签中。不过这里有一个例子反相从0以10和10〜0:

public partial class Form1 : Form 
{ 
    List<string> InfoList = new List<string> 
      { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; 
    const int labelNameLen = 3; 

    public Form1() { InitializeComponent(); } 

    private IEnumerable<Label> Labels 
    { 
     get 
     { 
      return this.Controls 
         .OfType<Label>() 
         .OrderBy(l => int.Parse(l.Name.Substring(labelNameLen))); 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     int i = 0; 
     foreach (Label l in Labels) 
     { 
      if (i < InfoList.Count) 
       l.Text = InfoList[i++]; 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     int i = InfoList.Count; 
     foreach (Label l in Labels) 
     { 
      if (i > 0) 
       l.Text = InfoList[--i]; 
     } 
    } 
} 
相关问题