Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Public WithEvents button1 As Button
Public Sub New()
button1 = New Button()
button1.Size = New Size(40, 40)
button1.Location = New Point(30, 30)
button1.Text = "Click me"
Me.Controls.Add(button1)
AddHandler button1.Click, AddressOf button1_Click
End Sub
Sub button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles button1.Click
MessageBox.Show("Hello World")
End Sub
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
End Class
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace FormWithButton
{
public class Form1 : Form
{
public Button button1;
public Form1()
{
button1 = new Button();
button1.Size = new Size(40, 40);
button1.Location = new Point(30, 30);
button1.Text = "Click me";
this.Controls.Add(button1);
button1.Click += new EventHandler(button1_Click);
}
void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}