Open a text editor (like Notepad), and type the code below:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace SampleApp
{
public class MainForm : Form
{
private Button btnHello;
// The form's constructor: this initializes the form and its controls.
public MainForm()
{
// Set the form's caption, which will appear in the title bar.
this.Text = "MainForm";
// Create a button control and set its properties.
btnHello = new Button();
btnHello.Location = new Point(89, 12);
btnHello.Name = "btnHello";
btnHello.Size = new Size(105, 30);
btnHello.Text = "Say Hello";
// Wire up an event handler to the button's "Click" event
// (see the code in the btnHello_Click function below).
btnHello.Click += new EventHandler(btnHello_Click);
// Add the button to the form's control collection,
// so that it will appear on the form.
this.Controls.Add(btnHello);
}
// When the button is clicked, display a message.
private void btnHello_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello, World!");
}
// This is the main entry point for the application.
// All C# applications have one and only one of these methods.
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}
}
X:\MainForm.cs
.Run the C# compiler from the command line, passing the path to the code file as an argument:
%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\csc.exe /target:winexe "X:\MainForm.cs"
Note: To use a version of the C# compiler for other .NET framework versions, take a look in the path, %WINDIR%\Microsoft.NET
and modify the example above accordingly. For more information on compiling C# applications, see Compile and run your first C# program.
MainForm.exe
will be created in the same directory as your code file. You can run this application either from the command line or by double-clicking on it in Explorer.