1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| usingSystem;using System.Windows.Forms;
using System.Drawing;
// Création du formulaire
var form = new Form
{
Text = "Formulaire d'inscription",
Width = 400,
Height = 350,
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.FixedDialog,
MaximizeBox = false,
BackColor = Color.WhiteSmoke
};
// Label et TextBox pour Nom
var lblNom = new Label
{
Text = "Nom :",
Location = new Point(30, 30),
AutoSize = true,
Font = new Font("Segoe UI", 10, FontStyle.Bold)
};
var txtNom = new TextBox
{
Location = new Point(150, 27),
Width = 200,
Font = new Font("Segoe UI", 10)
};
// Label et TextBox pour Prénom
var lblPrenom = new Label
{
Text = "Prénom :",
Location = new Point(30, 70),
AutoSize = true,
Font = new Font("Segoe UI", 10, FontStyle.Bold)
};
var txtPrenom = new TextBox
{
Location = new Point(150, 67),
Width = 200,
Font = new Font("Segoe UI", 10)
};
// Label et TextBox pour Âge
var lblAge = new Label
{
Text = "Âge :",
Location = new Point(30, 110),
AutoSize = true,
Font = new Font("Segoe UI", 10, FontStyle.Bold)
};
var txtAge = new NumericUpDown
{
Location = new Point(150, 107),
Width = 200,
Font = new Font("Segoe UI", 10),
Minimum = 1,
Maximum = 120
};
// Label et TextBox pour Profession
var lblProfession = new Label
{
Text = "Profession :",
Location = new Point(30, 150),
AutoSize = true,
Font = new Font("Segoe UI", 10, FontStyle.Bold)
};
var txtProfession = new TextBox
{
Location = new Point(150, 147),
Width = 200,
Font = new Font("Segoe UI", 10)
};
// Bouton Soumettre
var btnSoumettre = new Button
{
Text = "Soumettre",
Location = new Point(150, 200),
Width = 100,
Height = 35,
Font = new Font("Segoe UI", 10, FontStyle.Bold),
BackColor = Color.FromArgb(102, 126, 234),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat,
Cursor = Cursors.Hand
};
// Label pour afficher le résultat
var lblResultat = new Label
{
Location = new Point(30, 250),
Width = 340,
Height = 50,
Font = new Font("Segoe UI", 9),
ForeColor = Color.DarkGreen,
Text = ""
};
// Événement du bouton
btnSoumettre.Click += (sender, e) =>
{
if (string.IsNullOrWhiteSpace(txtNom.Text) ||
string.IsNullOrWhiteSpace(txtPrenom.Text) ||
string.IsNullOrWhiteSpace(txtProfession.Text))
{
MessageBox.Show("Veuillez remplir tous les champs !", "Erreur",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string message = $"Nom : {txtNom.Text}\n" +
$"Prénom : {txtPrenom.Text}\n" +
$"Âge : {txtAge.Value} ans\n" +
$"Profession : {txtProfession.Text}";
lblResultat.Text = $"✓ Enregistré: {txtPrenom.Text}{txtNom.Text}, " +
$"{txtAge.Value} ans, {txtProfession.Text}";
MessageBox.Show(message, "Informations enregistrées",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Console.WriteLine("\n=== Données du formulaire ===");
Console.WriteLine(message);
};
// Ajout des contrôles au formulaire
form.Controls.Add(lblNom);
form.Controls.Add(txtNom);
form.Controls.Add(lblPrenom);
form.Controls.Add(txtPrenom);
form.Controls.Add(lblAge);
form.Controls.Add(txtAge);
form.Controls.Add(lblProfession);
form.Controls.Add(txtProfession);
form.Controls.Add(btnSoumettre);
form.Controls.Add(lblResultat);
// Affichage du formulaire
Application.EnableVisualStyles();
Application.Run(form); |
Partager