2017-06-02 69 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