← Каталог
C# WinForms и WPF — простые окна — WinForms — полный `Program.cs`
Фрагмент из «C# WinForms и WPF — простые окна»: WinForms — полный `Program.cs`.
using System.Drawing;
using System.Windows.Forms;
namespace HelloWinForms;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Привет, WinForms",
ClientSize = new Size(400, 200),
StartPosition = FormStartPosition.CenterScreen
};
var label = new Label
{
Text = "Окно работает!",
AutoSize = true,
Location = new Point(20, 20),
Font = new Font("Segoe UI", 14f)
};
form.Controls.Add(label);
Application.Run(form);
}
} using System.Drawing;
using System.Windows.Forms;
namespace HelloWinForms;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Привет, WinForms",
ClientSize = new Size(400, 200),
StartPosition = FormStartPosition.CenterScreen
};
var label = new Label
{
Text = "Окно работает!",
AutoSize = true,
Location = new Point(20, 20),
Font = new Font("Segoe UI", 14f)
};
form.Controls.Add(label);
Application.Run(form);
}
} using System.Drawing;
using System.Windows.Forms;
namespace ButtonDemo;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Кнопка",
ClientSize = new Size(320, 180),
StartPosition = FormStartPosition.CenterScreen
};
var button = new Button
{
Text = "Нажми меня",
Location = new Point(20, 20),
AutoSize = true
};
// Click += подписка на событие; лямбда вызывается ТОЛЬКО по клику
button.Click += (_, _) => MessageBox.Show(
"Кнопка нажата!",
"Сообщение",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
form.Controls.Add(button);
Application.Run(form);
}
} using System.Drawing;
using System.Windows.Forms;
namespace ButtonDemo;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Кнопка",
ClientSize = new Size(320, 180),
StartPosition = FormStartPosition.CenterScreen
};
var button = new Button
{
Text = "Нажми меня",
Location = new Point(20, 20),
AutoSize = true
};
// Click += подписка на событие; лямбда вызывается ТОЛЬКО по клику
button.Click += (_, _) => MessageBox.Show(
"Кнопка нажата!",
"Сообщение",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
form.Controls.Add(button);
Application.Run(form);
}
} using System.Drawing;
using System.Windows.Forms;
namespace GreetApp;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Приветствие",
ClientSize = new Size(360, 160),
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.FixedDialog,
MaximizeBox = false
};
var nameLabel = new Label
{
Text = "Ваше имя:",
AutoSize = true,
Location = new Point(20, 24)
};
var entry = new TextBox
{
Location = new Point(120, 20),
Width = 200
};
var greetButton = new Button
{
Text = "Приветствовать",
Location = new Point(20, 60),
AutoSize = true
};
greetButton.Click += (_, _) =>
{
var name = entry.Text.Trim();
if (string.IsNullOrEmpty(name))
{
MessageBox.Show("Введите имя", "Пусто",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
MessageBox.Show($"Здравствуй, {name}!", "Привет");
};
form.Controls.Add(nameLabel);
form.Controls.Add(entry);
form.Controls.Add(greetButton);
entry.Focus();
Application.Run(form);
}
} using System.Drawing;
using System.Windows.Forms;
namespace GreetApp;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Приветствие",
ClientSize = new Size(360, 160),
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.FixedDialog,
MaximizeBox = false
};
var nameLabel = new Label
{
Text = "Ваше имя:",
AutoSize = true,
Location = new Point(20, 24)
};
var entry = new TextBox
{
Location = new Point(120, 20),
Width = 200
};
var greetButton = new Button
{
Text = "Приветствовать",
Location = new Point(20, 60),
AutoSize = true
};
greetButton.Click += (_, _) =>
{
var name = entry.Text.Trim();
if (string.IsNullOrEmpty(name))
{
MessageBox.Show("Введите имя", "Пусто",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
MessageBox.Show($"Здравствуй, {name}!", "Привет");
};
form.Controls.Add(nameLabel);
form.Controls.Add(entry);
form.Controls.Add(greetButton);
entry.Focus();
Application.Run(form);
}
} using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
namespace TempConverter;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Конвертер температуры",
ClientSize = new Size(340, 180),
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.FixedDialog,
MaximizeBox = false
};
var tempLabel = new Label
{
Text = "Температура (°C):",
AutoSize = true,
Location = new Point(20, 24)
};
var entry = new TextBox { Location = new Point(160, 20), Width = 80 };
var resultLabel = new Label
{
Text = "—",
AutoSize = true,
Location = new Point(20, 60),
Font = new Font("Segoe UI", 12f)
};
var convertButton = new Button
{
Text = "Перевести",
Location = new Point(20, 100),
AutoSize = true
};
convertButton.Click += (_, _) =>
{
var raw = entry.Text.TrimReplace(',', '.');
if (!double.TryParse(raw, NumberStyles.Any, CultureInfo.InvariantCulture, out var celsius))
{
MessageBox.Show("Введите число, например 25", "Ошибка");
return;
}
var fahrenheit = celsius * 9 / 5 + 32;
resultLabel.Text = $"{celsius:F1} °C = {fahrenheit:F1} °F";
};
entry.KeyDown += (_, e) =>
{
if (e.KeyCode == Keys.Enter)
convertButton.PerformClick();
};
form.Controls.Add(tempLabel);
form.Controls.Add(entry);
form.Controls.Add(resultLabel);
form.Controls.Add(convertButton);
entry.Focus();
Application.Run(form);
}
} using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
namespace TempConverter;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Конвертер температуры",
ClientSize = new Size(340, 180),
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.FixedDialog,
MaximizeBox = false
};
var tempLabel = new Label
{
Text = "Температура (°C):",
AutoSize = true,
Location = new Point(20, 24)
};
var entry = new TextBox { Location = new Point(160, 20), Width = 80 };
var resultLabel = new Label
{
Text = "—",
AutoSize = true,
Location = new Point(20, 60),
Font = new Font("Segoe UI", 12f)
};
var convertButton = new Button
{
Text = "Перевести",
Location = new Point(20, 100),
AutoSize = true
};
convertButton.Click += (_, _) =>
{
var raw = entry.Text.TrimReplace(',', '.');
if (!double.TryParse(raw, NumberStyles.Any, CultureInfo.InvariantCulture, out var celsius))
{
MessageBox.Show("Введите число, например 25", "Ошибка");
return;
}
var fahrenheit = celsius * 9 / 5 + 32;
resultLabel.Text = $"{celsius:F1} °C = {fahrenheit:F1} °F";
};
entry.KeyDown += (_, e) =>
{
if (e.KeyCode == Keys.Enter)
convertButton.PerformClick();
};
form.Controls.Add(tempLabel);
form.Controls.Add(entry);
form.Controls.Add(resultLabel);
form.Controls.Add(convertButton);
entry.Focus();
Application.Run(form);
}
} using System.Drawing;
using System.Windows.Forms;
namespace TodoList;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Список задач",
ClientSize = new Size(320, 280),
StartPosition = FormStartPosition.CenterScreen
};
var entry = new TextBox { Location = new Point(12, 12), Width = 280 };
var addButton = new Button { Text = "Добавить", Location = new Point(12, 44), AutoSize = true };
var removeButton = new Button { Text = "Удалить", Location = new Point(100, 44), AutoSize = true };
var listBox = new ListBox { Location = new Point(12, 80), Size = new Size(280, 160) };
void AddTask()
{
var text = entry.Text.Trim();
if (text.Length == 0) return;
listBox.Items.Add(text);
entry.Clear();
}
void RemoveTask()
{
if (listBox.SelectedIndex >= 0)
listBox.Items.RemoveAt(listBox.SelectedIndex);
}
addButton.Click += (_, _) => AddTask();
removeButton.Click += (_, _) => RemoveTask();
entry.KeyDown += (_, e) => { if (e.KeyCode == Keys.Enter) AddTask(); };
form.Controls.AddRange(new Control[] { entry, addButton, removeButton, listBox });
entry.Focus();
Application.Run(form);
}
} using System.Drawing;
using System.Windows.Forms;
namespace TodoList;
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form
{
Text = "Список задач",
ClientSize = new Size(320, 280),
StartPosition = FormStartPosition.CenterScreen
};
var entry = new TextBox { Location = new Point(12, 12), Width = 280 };
var addButton = new Button { Text = "Добавить", Location = new Point(12, 44), AutoSize = true };
var removeButton = new Button { Text = "Удалить", Location = new Point(100, 44), AutoSize = true };
var listBox = new ListBox { Location = new Point(12, 80), Size = new Size(280, 160) };
void AddTask()
{
var text = entry.Text.Trim();
if (text.Length == 0) return;
listBox.Items.Add(text);
entry.Clear();
}
void RemoveTask()
{
if (listBox.SelectedIndex >= 0)
listBox.Items.RemoveAt(listBox.SelectedIndex);
}
addButton.Click += (_, _) => AddTask();
removeButton.Click += (_, _) => RemoveTask();
entry.KeyDown += (_, e) => { if (e.KeyCode == Keys.Enter) AddTask(); };
form.Controls.AddRange(new Control[] { entry, addButton, removeButton, listBox });
entry.Focus();
Application.Run(form);
}
}