2017-06-02 70 views
0

我需要button_Click事件上的不同变量。对于每个按钮,我需要不同的billDetail和rowAmount值。其实每个按钮都会给出var Size的最后一个值。 我认为是一个简单的问题,但我这样做是为了爱好......请帮助我!c#将不同的变量传递给按钮onClick事件

private void CashCounter_Load(object sender, EventArgs e) 
{ 
    var posXbutton = 120; 
    var posYbutton = 10; 
    var gapXbutton = 105; 
    var gapYbutton = 65; 
    var posXlabel = 5; 
    var posYlabel = 28; 
    var gapYlabel = 65; 

    using (var db = new GelatoProjectDBEntities()) 
    { 
     #region working code 
     var items = (from x in db.ProductsLists 
        select new { x.Id, x.Product, x.Category, x.Size, x.Price, x.Market, x.Image } 
        ).Where(x => x.Market == "Retail") 
        .ToArray(); 

     var categories = (from x in items 
          select x.Category) 
          .Distinct(); 

     foreach (var category in categories) 
     { 
      TabPage TabProduct = new TabPage(category); 
      TabProduct.BackColor = System.Drawing.Color.White; 
      tabControl1.TabPages.Add(TabProduct); 

      var products = (from i in items 
          where i.Category == category 
          select i.Product) 
          .Distinct() 
          .ToList(); 
      foreach (var product in products) 
      { 
       string labelName = "label" + product; 
       var label = new Label(); 
       label.AutoSize = false; 
       label.Location = new System.Drawing.Point(posXlabel, posYlabel); 
       label.Name = labelName; 
       label.Size = new System.Drawing.Size(110, 17); 
       label.TabIndex = 0; 
       label.Text = product; 
       label.RightToLeft = RightToLeft.Yes; 
       posYlabel = posYlabel + gapYlabel; 
       TabProduct.Controls.Add(label); 

       var sizes = (from i in items 
          where i.Product == product 
          //select i.Size) 
          select new { i.Id, i.Product, i.Category, i.Size, i.Price, i.Market, i.Image }) 
          .Distinct() 
          .ToList(); 

       foreach (var size in sizes) 
       { 
        BillDetail = size.Size+" "+product; 
        BillTotal = "£ "+size.Price; 
        RowAmount = size.Price; 
        string buttonName = "button" + product; 
        var button = new Button(); 
        button.Location = new Point(posXbutton, posYbutton); 
        button.Name = buttonName; 
        button.Size = new Size(100, 50); 
        button.Text = size.Size; 
        button.BackColor = System.Drawing.Color.LightGray; 
        button.Click += new System.EventHandler(button_Click); 
        posXbutton = posXbutton + gapXbutton; 
        TabProduct.Controls.Add(button); 
       } 
       posXbutton = 120; 
       posYbutton = posYbutton + gapYbutton; 
      } 
      posXbutton = 120; 
      posYbutton = 10; 
      gapXbutton = 105; 
      gapYbutton = 65; 
      posXlabel = 5; 
      posYlabel = 28; 
      gapYlabel = 65; 
     } 
    } 
#endregion 
} 

private void button_Click (object sender, EventArgs e) 
{ 
    ListViewItem NewBillProduct = new ListViewItem(BillDetail); 
    NewBillProduct.SubItems.Add("£"); 
    NewBillProduct.SubItems.Add(RowAmount.ToString("F")); 
    listViewBillDetail.Items.Add(NewBillProduct); 

    BillTotalAmount = BillTotalAmount + double.Parse(RowAmount.ToString("F")); 
    ItemsTotal = ItemsTotal + 1; 
    textBoxTotal.Text = (BillTotalAmount.ToString("C", nfi)).ToString(); 
    textBoxItems.Text = (ItemsTotal).ToString(); 
} 
+1

制作你自己定制的'EventArgs' – Svek

+1

不用手动创建和定位按钮,可以考虑使用'DataGridView',它将根据提供的数据为你做到 – Fabio

回答

1

可以使用Tag财产Button存储与Size对象有:

button.Tag = size; 

然后在单击事件处理程序,你可以检索该对象:

private void button_Click (object sender, EventArgs e) 
{ 
    var button = (Button)sender; 
    var size = (Size)button.Tag; 
    // use size.Size or size.Price   
    // etc 
} 

注意:您可以如果您将它们放入数组或列表中,则在Tag中存储多个对象。

+0

谢谢你,谢尔盖,我正在尝试,但使用大小。它不显示任何大小的变量... – Mirko

+0

@Mirko你可能忘了将'Tag'强制转换为'Size'类型 –

+0

这是进入foreach语句: button.Tag = size; private void button_Click(object sender,EventArgs e) { var button =(Button)sender; var size =(Size)button.Tag; – Mirko