0

I would need different variables on button_Click event. For each button I need different billDetail and rowAmount values. Actually every button is giving the last values of the var Size. I think is a easy problem but I'm doing this as hobby...please help me!

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();
}
2
  • 1
    Make your own custom EventArgs Commented Jun 2, 2017 at 11:12
  • 1
    Instead of manually create and locate buttons consider to use DataGridView which will do it for you based on the provided data Commented Jun 2, 2017 at 11:43

1 Answer 1

1

You can use Tag property of Button to store related Size object there:

 button.Tag = size;

Then in click event handler you can retrieve this object:

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

Note: You can store several objects in Tag if you'll put them into array or list.

Sign up to request clarification or add additional context in comments.

14 Comments

Thank you so much Sergey, I'm trying but using size. it doesn't show me any size variables...
@Mirko probably you forgot to cast Tag to Size type
this is into the foreach statement: button.Tag = size; private void button_Click (object sender, EventArgs e) { var button = (Button)sender; var size = (Size)button.Tag;
@Mirko that is an assignment. I'm talking about reading tag value in event handler
@Mirko will, I already wrote a sample above - see button_Click code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.