Вот так в windows forms:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "Привет";
button.Click += button_Click;
Controls.Add(button);
}
void button_Click(object sender, EventArgs e)
{
MessageBox.Show("Пока");
}
}
}
про WPF ничего сказать не могу, не владею
можно короче:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "Привет";
button.Click += (s, ea) => MessageBox.Show("Пока");
Controls.Add(button);
}
}
}
Можно еще вот так:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Button button = new Button {Text = "Привет"};
button.Click += (s, ea) => MessageBox.Show("Пока");
Controls.Add(button);
}
}
}