REKLAM ALANI

Forma Eklenen Butonları Dinamik Olarak Kontrol Etme

Forma Eklenen Butonları Dinamik Olarak Kontrol Etme

avatar ^Phoenix^
քɦօɛռɨӼ ɖɨʐǟʏռ
299
#1
Yorumu Paylaş

Kod:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace C_Sharp_Buton_Kontrolleri
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//Form üzerinde groupbox1 içine eklediğimiz bütün butonları bu

//listenin içinde tutacağız

List < Button > formGroupbox1Butonlar = new List< Button >();

private **** Form1_Load(object sender, EventArgs e)
{
//Form load kısmında formdaki groupbox1 içindeki bütün kontrolleri

//listeye ekle

formGroupbox1Butonlar.Clear();

foreach (Control item in groupBox1.Controls)
{
if (item is Button)
{
formGroupbox1Butonlar.Add(item as Button);
}
}

// şimdi bütün butonların click eveti olduğunda aynı metodu çağırmasını

//istiyorum. Çünkü hangi butona basılırsa textbox içine o butonun

//text değerini yazılmasını istiyorum

for (int i = 0; i < formGroupbox1Butonlar.Count; i++)
{
formGroupbox1Butonlar[i].Click += new EventHandler(Button_Click);
}
}

**** Button_Click(object sender, EventArgs e)
{
// Burada hangi buton tıklanırsa onu kontrol edebileceğim

Button clickedButton = sender as Button;

//şimdi tıklanan butonun text değerini textboxa yazdırıyorum

textBox1.Text += clickedButton.Text;

//ayrıca burada tıklanan butonun bütün public özellikleri ile

//oynayabiliriz. Mesela butonun visible özelliğini false yapabiliriz

//clickedButton.Visible = false;


// Ya da butonun arka plan rengini değiştirebiliriz

clickedButton.BackColor = Color.Lime;

// Başka olaylarda bağlayabiliriz. Örneğin

clickedButton.BackColorChanged += new EventHandler(clickedButton_BackColorChanged);
}

**** clickedButton_BackColorChanged(object sender, EventArgs e)
{
MessageBox.Show((sender as Button).Text + " Nolu butonun rengi değişti.");
}


}
}[/i]
deneme
քɦօɛռɨӼ ɖɨʐǟʏռ

Konuda Ara

0 Yorum


Konuyu Okuyanlar:
1 Ziyaretçi