Goran Bibic

Goran Bibic

  • 450
  • 2.9k
  • 180.4k

Dynamically button add picture and text C#

Mar 27 2024 6:24 PM

Need to set smaller size image in button
Need to setup

picture
label
label

In same button
With my code text is over picture
Need to add some frame or something for picture
Thank you

 

My code

 

private void TopliNapitciButton_Click(object sender, EventArgs e)
       {
           panel1.Controls.Clear();

           string conString = ConnectionClass.PullData(labelgodina.Text);
           SqlConnection con = new SqlConnection(conString);
           SqlCommand cmd = new SqlCommand("SELECT naziv+' - '+CAST(FORMAT(cijena_sa_porezom, 'N2') AS varchar), data FROM dbo.roba_usluge where grupa_artikala='Napitci' and status='Aktivan'", con);

           var da = new SqlDataAdapter(cmd);
           var ItemTable = new DataTable();
           da.Fill(ItemTable);

           con.Open();
           Int32 count = ItemTable.Rows.Count;
           con.Close();

           int top = 190;
           int left = 5;
           for (int i = 1; i <= count; i++)
           {
               Button button = new Button();
               button.Size = new Size(128, 128);
               button.BackColor = Color.Transparent;
               //button.FlatStyle = FlatStyle.Flat;
               button.FlatAppearance.BorderSize = 0;
               button.Font = new System.Drawing.Font("Trebuchet MS", 10);
               button.TextAlign = ContentAlignment.BottomCenter;
               button.BackgroundImageLayout = ImageLayout.Zoom;

               button.Left = left;
               button.Top = top;
               button.Text = ItemTable.Rows[i - 1][0].ToString();

               if (ItemTable.Rows[i - 1][1] != null)
               {
                   try
                   {
                       byte[] _byte = (byte[])ItemTable.Rows[i - 1][1];
                       MemoryStream ms = new MemoryStream(_byte);
                       button.BackgroundImage = System.Drawing.Image.FromStream(ms);//bilo image
                   }
                   catch { }
               }

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

               panel1.Controls.Add(button);

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

 


Answers (2)