Dodawanie eventów w kodzie C# WinForms dla wielu controlek jednocześnie
using System;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddEventHandler();
}
void AddEventHandler()
{
foreach (Control c in groupBox1.Controls)
{
c.Click += btnGroup_Click;
}
}
private void btnGroup_Click(object sender, EventArgs e)
{
Control c = sender as Control;
label1.Text = c.Name;
}
}
}
// Wykrywanie kliknięcia prawym przyciskiem myszy
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
label1.Text = "Elo";
}
}