これが実行可能な例です。コンパイル:
csc RunnableForm.cs RunnableForm.Designer.cs
RunnableForm.cs:
using System;
using System.Linq;
using System.Windows.Forms;
namespace Test
{
public partial class RunnableForm : Form
{
public RunnableForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("bang!");
}
[STAThread]
static void Main()
{
string[] args = Environment.GetCommandLineArgs();
// We'll always have one argument (the program's exe is args[0])
if (args.Length == 1)
{
// Run windows forms app
Application.Run(new RunnableForm());
}
else
{
Console.WriteLine("We'll run as a console app now");
Console.WriteLine("Arguments: {0}", String.Join(",", args.Skip(1)));
Console.Write("Enter a string: ");
string str = Console.ReadLine();
Console.WriteLine("You entered: {0}", str);
Console.WriteLine("Bye.");
}
}
}
}
RunnableForm.Designer.cs:
namespace Test
{
partial class RunnableForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(42, 42);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(153, 66);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// RunnableForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.button1);
this.Name = "RunnableForm";
this.Text = "RunnableForm";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}