Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I use dinamycal button and add TableLayoutPanel on button
Now need to use labels from that Table layout on my button
My code for button click

void btn_Click(object sender, EventArgs e)
       {

           Button btn = (Button)sender;
           Label Label = (Label)sender;

           var itemAndPrice = btn.Text;
           var item = Label.Text;

           int sno = dataGridView1.Rows.Count + 1;
           string nazivartikla = item[0].ToString();
           string exists = "";

           foreach (DataGridViewRow g1 in dataGridView1.Rows)
           {
               if (g1.Cells[2].Value.ToString().Trim() == item[0].ToString().Trim())
               {
                   exists = "y";
                   g1.Cells[3].Value = Convert.ToInt32(g1.Cells[3].Value) + 1;
               }
           }

           if (exists != "y")
           {
               dataGridView1.Rows.Add(sno, SifraTextBox.Text, nazivartikla, 1);
           }

           dataGridView1.Refresh();

       }


What I have tried:

Button button = new Button();
                button.Size = new Size(128, 158);
                button.BackColor = Color.Transparent;
                button.FlatAppearance.BorderSize = 0;
                button.Font = new System.Drawing.Font("Roboto", 10);
                button.TextAlign = ContentAlignment.BottomCenter;
                button.BackgroundImageLayout = ImageLayout.Zoom;

                // Create TableLayoutPanel
                TableLayoutPanel table = new TableLayoutPanel();
                table.Dock = DockStyle.Fill;
                table.RowCount = 3;
                table.RowStyles.Add(new RowStyle(SizeType.Percent, 70F));
                table.RowStyles.Add(new RowStyle(SizeType.Percent, 15F));
                table.RowStyles.Add(new RowStyle(SizeType.Percent, 15F));

                // Image
                PictureBox pictureBox = new PictureBox();
                pictureBox.Dock = DockStyle.Fill;
                pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                
                if (ItemTable.Rows[i]["data"] != DBNull.Value)
                {
                    byte[] _byte = (byte[])ItemTable.Rows[i]["data"];
                    MemoryStream ms = new MemoryStream(_byte);
                    pictureBox.Image = Image.FromStream(ms);
                }

                // Label for name
                Label nameLabel = new Label();
                nameLabel.Dock = DockStyle.Fill;
                nameLabel.TextAlign = ContentAlignment.MiddleCenter;
                nameLabel.Font = new System.Drawing.Font("Roboto", 12, FontStyle.Bold);
                nameLabel.ForeColor = System.Drawing.Color.Gray;
                nameLabel.Text = ItemTable.Rows[i]["naziv"].ToString();

                // Label for price
                Label priceLabel = new Label();
                priceLabel.Dock = DockStyle.Fill;
                priceLabel.TextAlign = ContentAlignment.MiddleCenter;
                priceLabel.ForeColor = System.Drawing.Color.DarkOrange;
                priceLabel.Font = new System.Drawing.Font("Roboto", 12, FontStyle.Bold);
                priceLabel.Text = ItemTable.Rows[i]["cijena"].ToString()+" KM";

                // Add controls to TableLayoutPanel
                table.Controls.Add(pictureBox, 0, 0);
                table.Controls.Add(nameLabel, 0, 1);
                table.Controls.Add(priceLabel, 0, 2);

                button.Left = left;
                button.Top = top;


                // Add TableLayoutPanel to button
                button.Controls.Add(table);

                button.Click += new EventHandler(this.btn_Click);

                panel1.Controls.Add(button);

                if ((i + 1) % 4 == 0)
                {
                    left = 5;
                    top += button.Height + 2;
                }
                else
                {
                    left += button.Width + 2;
                }
Posted

1 solution

Your description makes no sense, and you're not describing a problem at all.

This code will not work. The sender is THE ONE control you clicked on. It cannot be both a Button and a Label at the same time.
C#
void btn_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    Label Label = (Label)sender;

If you use the button as the control that was clicked on, you have no good way of determining which label control is sitting next to the button.
 
Share this answer
 
Comments
Jo_vb.net 28-Mar-24 14:50pm    
I think the question is related to his/her previous question
https://www.codeproject.com/Questions/5379681/Dynamically-button-add-picture-and-text-Csharp

Perhaps if using arrays for buttons and labels it would be possible to adress button[i] to label[i]
Dave Kreskowiak 28-Mar-24 22:10pm    
Not the best solution, but it would work. Using arrays of controls like that is a bit ... fragile.
0x01AA 31-Mar-24 6:56am    
Or the very dirty way: Use Button.Tag to have a reference to the Label :-)
Dave Kreskowiak 31-Mar-24 10:28am    
Yeah, still fragile. I absolutely detest using controls for "data storage". I'd probably make a UserControl if a label really needs to be "attached" to a button in some way.
0x01AA 31-Mar-24 10:39am    
I agree, your way is the correct one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900