Soran
c# Dinamik butonlara farklı iş

Soru
-
Tüm Yanıtlar
-
Eğer ver,tabanından gelen bir değere göre farklı kod çalıştıracaksanız;
veriTabanındanGelenKayıtlar .ToList() .ForEach(p=> { var buton = new Button { Tag = p.Tip,
gibi bişey yapabilirsiniz.
Text = $"{p.Ad} isimli hesaba ait {p.Tip} işlemi yap" }; buton.Click += (s,evt) => { switch(((Button)s).Tag.ToString()) { case "Tahsil": TahsilatYap(); break; case "Tediye": TediyeYap(); break; case "Transfer": TransferYap(); break; } }; });
e-mail: onay[nokta]yalciner[at]hotmail[nokta]com
- Düzenleyen Önay YALÇINERModerator 28 Mart 2019 Perşembe 12:35
- Yanıt Olarak Öneren CetinBasozEditor 28 Mart 2019 Perşembe 14:19
-
string cmdText = "SELECT * FROM KATEGORİ"; SqlCommand cmd = new SqlCommand(cmdText); cmd.Connection = connection; connection.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { ürünler.Add(dr.GetString(0)); } int sayi = ürünler.Count; Button[] btn = new Button[sayi]; for (int i = 0; i < btn.Length; i++) { btn[i] = new Button(); btn[i].Text = ürünler[i].ToString(); flowLayoutPanel1.Controls.Add(btn[i]); // btn[i].Top = i * 00; btn[i].Left = 40; btn[i].Width = 80; btn[i].Height = 50; btn[i].TextAlign = ContentAlignment.MiddleLeft; btn[i].Click += new EventHandler(b_2); } this.AutoScroll = true; this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.None; this.flowLayoutPanel1.AutoSize = false; this.flowLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.flowLayoutPanel1.Location = new System.Drawing.Point(12, 40); this.flowLayoutPanel1.Name = "kategori"; this.flowLayoutPanel1.Size = new System.Drawing.Size(927, 525); connection.Close();
bu şekilde buton oluşturuyorum. oluşturduğum butonların hepsinde farklı işlemler yapmak istiyorum.
mesela buton ismi muratsa şunu yap gibi komutlar vermek istiyorum.
- Düzenleyen gomer_222 28 Mart 2019 Perşembe 12:50
-
private void b_2(object sender, EventArgs e) { Button b = sender as Button; if (b != null) { if (b.Name == "Murat") { SunuYap(); } else if (b.Name == "...") { BunuYap(); } } }
Not: Senin kodunda .Name eklememişsin.
Not2: Select * komutu çok tehlikeli olmuş, sonrasında da GetString(0). Madem tek bir tane string kolonla ilgileniyorsun:
select kolonAdi from Kategori;
- Düzenleyen CetinBasozEditor 28 Mart 2019 Perşembe 14:22
-
private void Form1_Load(object sender, EventArgs e) { string cmdText = "SELECT * FROM KATEGORİ"; SqlCommand cmd = new SqlCommand(cmdText); cmd.Connection = connection; connection.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { ürünler.Add(dr.GetString(0)); } int sayi = ürünler.Count; Button[] btn = new Button[sayi]; for (int i = 0; i < btn.Length; i++) { btn[i] = new Button(); btn[i].Text = ürünler[i].ToString(); btn[i].Name = ürünler[i].ToString(); flowLayoutPanel1.Controls.Add(btn[i]); // btn[i].Top = i * 00; btn[i].Left = 40; btn[i].Width = 80; btn[i].Height = 50; btn[i].TextAlign = ContentAlignment.MiddleLeft; btn[i].Click += new EventHandler(b_2); } this.AutoScroll = true; this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.None; this.flowLayoutPanel1.AutoSize = false; this.flowLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.flowLayoutPanel1.Location = new System.Drawing.Point(12, 40); this.flowLayoutPanel1.Name = "kategori"; this.flowLayoutPanel1.Size = new System.Drawing.Size(927, 525); connection.Close(); } public void b_1(object sender, EventArgs e) { connection.Open(); Button btn3 = sender as Button; label1.Text = btn3.Name; if (label1.Text == "MASA 1") { MessageBox.Show("MASA!!"); } else if (label1.Text == "MASA 2") { MessageBox.Show("MASA!!2"); } else if (label1.Text == "MASA 3") { MessageBox.Show("MASA!!3"); } }
public void b_1 deki if komutları çalışmıyor nasıl çözücem bunu
-
-
amacım veritabanından okuduğum satırlarda masa 1 masa 2 masa 3 yazıyor. onları okutarak form açılışında buton olşturuyprum 3 tane. bu butonları kullanmak istyiroum. masa 1 butonuna bastığımda label masa 1 yapıyorum ki label masa 1 se veritabanında masa 1 in coloumuna veri eklemek amacım
-
-
-
-